UNIT 4C Itera,o: Correctess ad Efficiecy 1 L Geeral Idea: Ay oe Itera,o SORTED i i L SORTED 2 1
Look Closer at Iser,o Sort Give a list L of legth, > 0. 1. Set i = 1. 2. While i is ot equal to, do the followig: L[0..i) meas: List L from idex 0 up to but ot icludig i Precodi)o for each itera)o: L[0..i) is sorted a. Isert L[i] ito its correct posi,o i L betwee idex 0 ad idex i iclusive. b. Add 1 to i. Postcodi)o for each itera)o: L[0..i) is sorted 3. Retur the list L which will ow be sorted. 3 Look Closer at Iser,o Sort Give a list L of legth, > 0. 1. Set i = 1. 2. While i is ot equal to, do the followig: Loop ivariat: L[0..i) is sorted a. Isert L[i] ito its correct posi,o i L betwee idex 0 ad idex i iclusive. b. Add 1 to i. 3. Retur the list L which will ow be sorted. A loop ivariat is a codi)o that is true at the start ad ed of each itera)o of a loop. 4 2
Reasoig with the Loop Ivariat The loop ivariat is true at the ed of each itera,o, icludig the last itera,o. AWer the last itera,o, whe we go to step 3: L[0..i) is sorted (from the last itera,o) AND i is equal to (due to the while loop termia,g) These 2 codi,os imply that L[0..) is sorted, but this rage is the e,re list, so the list must always be sorted whe we retur our fial aswer! 5 Cou,g Opera,os We measure,me efficiecy by cou,g the umber of opera,os performed by the algorithm. But what is a opera,o? assigmet statemets comparisos retur statemets... 6 3
Liear Search: Worst Case # let = the legth of datalist. def search(datalist, key): idex = 0 1 while idex < le(datalist): +1 if datalist[idex] == key: retur idex idex = idex + 1 retur Noe 1 Total: 3+3 7 Cou,g Opera,os How do we kow that each opera,o we cout takes the same amout of,me? (We do t.) So geerally, we look at the process more abstractly ad cout whatever opera,o depeds o the amout or size of the data we re processig. We do't cosider what machie we're usig, what compiler we use, what laguage we use, etc. For liear search, we would cout the umber of,mes we compare elemets i the list to the key. 8 4
Liear Search: Worst Case Simplified # let = the legth of datalist. def search(datalist, key): idex = 0 while idex < le(datalist): if datalist[idex] == key: retur idex idex = idex + 1 retur Noe Total: 9 Order of Complexity For very large, we express the umber of opera,os as the (,me) order of complexity. Order of complexity is owe expressed usig Big-O ota,o: Number of opera,os Order of Complexity O() 3+3 O() 2+8 O() Usually does't matter what the costats are... we are oly cocered about the highest power of. 10 5
O() ( Liear ) 2 + 8 3+3 Number of Operatios (amout of data) 11 O() Number of Operatios 40 20 10 For a liear algorithm, if you double the amout of data, the amout of work you do doubles (approximately). Put aother way: The amout of work doe is liearly proportioal to the amout of data. 10 20 40 (amout of data) 12 6
Liear Search: Best Case # let = the legth of datalist. def search(datalist, key): idex = 0 1 while idex < le(datalist): 1 if datalist[idex] == key: 1 retur idex 1 idex = idex + 1 retur Noe Total: 4 13 Liear Search: Best Case Simplified # let = the legth of datalist. def search(datalist, key): idex = 0 while idex < le(datalist): if datalist[idex] == key: 1 retur idex idex = idex + 1 retur Noe Total: 1 14 7
O(1) ( Costat-Time ) Number of Operatios For a costat-time algorithm, if you double the amout of data, the amout of work you do stays the same. 4 1 4 = O(1) 1 = O(1) (amout of data) 15 Liear Search Worst Case: O() Best Case: O(1) Average Case: 16 8
Iser,o Sort: Worst Case j i SORTED O itera,o i, we eed to examie j elemets ad the shiw i-j elemets to the right, so we have to do j + (i-j) = i uits of work. 17 Iser,o Sort: Worst Case Whe i = 1, we have 1 uit of work. Whe i = 2, we have 2 uits of work.... Whe i = -1, we have -1 uits of work. The total amout of work doe is: 1 + 2 +... + (-1) = (-1)/2 = ( 2 - )/2 (a quadra,c fuc,o) = O( 2 ) 18 9
Order of Complexity Number of opera,os Order of Complexity 2 O( 2 ) 2 /2 + 3/2-1 O( 2 ) 2 2 + 7 O( 2 ) Usually does't matter what the costats are... we are oly cocered about the highest power of. 19 O( 2 ) ( Quadra,c ) 2 2 + 7 2 2 /2 + 3/2 1 Number of Operatios (amout of data) 20 10
Number of Operatios O( 2 ) 1600 N 2 400 100 10 20 40 For a quadratic algorithm, if you double the amout of data, the amout of work you do quadruples (approximately). Put aother way: The amout of work you do is proportioal to the square of the amout of data. N (amout of data) 21 Our Iser,o Sort Worst Case: O( 2 ) Best Case: I our iser,o sort implemeta,o, the worst case ad best case are the same! It does't maier where we do the iserts. If we isert ear the frot, we have fewer elemets to compare, but more shiws. If we isert ear the ed, we have more elemets to compare, but fewer shiws. But we ca get the best case to be O(). (See PS4!) 22 11