Algorithms Elemetary Sortig Dog Kyue Kim Hayag Uiversity dqkim@hayag.a.kr
Cotets Sortig problem Elemetary sortig algorithms Isertio sort Merge sort Seletio sort Bubble sort
Sortig problem Iput A sequee of umber <a 1, a,..., a >. keys Output A permutatio reorderig) <a 1, a,..., a > of the iput sequee suh that a 1 a a. Ex> Iput: < 5,, 4, 6, 1, 3> Output: < 1,, 3, 4, 5, 6> 3
Isertio Sort Strategy Algorithm Aalysis Corretess Performae 4
Sortig Algorithms Seletio sort Exerises.- page 7) Bubble sort Problems - page 38) 5
Seletio Sort: Algorithms void seletiosortit *array, it legth) { it max, i, temp; whilelegth > 0) { max = 0; fori = 1; i <legth; i++) ifarray[i] > array[max]) max = i; temp = array[legth-1]; array[legth-1] = array[max]; array[max] = temp; legth--; } } 6
Bubble Sort: Algorithms void bubblesortit *array, it legth) { it i, j, temp; fori = legth-1; i > 0; i--) forj = 0; j < i ; j++) /* ompare eighborig elemets */ ifarray[j] > array[j+1]) { /* swap array[j] ad array[j+1] */ temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } 7
Isertio Sort What is isertio sort? A sortig algorithm usig isertio. What is isertio? Give a key ad a sorted list of keys, isert a key ito a sorted list of keys preservig the sorted order. ex> Isert 3 ito <1,, 4, 5, 6> 8
Isertio Sort Strategy Isertio sort is a iremetal approah. Let A[1..] deote the array storig keys. Isert A[] ito A[1]. Isert A[3] ito A[1..]. Isert A[4] ito A[1..3].... Isert A[] ito A[1..-1]. 9
Example: Isertio Sort 5 4 6 1 3 5 4 6 1 3 5 4 6 1 3 4 5 6 1 3 1 4 5 6 3 1 3 4 5 6 4 5 6 1 3 10
Isertio Sort Algorithm INSERTION-SORTA) for j to legth[a] do key A[j] i j - 1 while i > 0 ad A[i] > key do A[i + 1] A[i] i i - 1 A[i + 1] key Pseudoode ovetios are give i p.19-0 of the textbook. -1 iteratios of isertio. Fid a plae to put A[j]. Isert A[j] ito A[1..j-1]. Put A[j]. 11
Isertio Sort Strategy Algorithm Aalysis Corretess Performae Ruig time Spae osumptio 1
Ruig Time How to aalyze the ruig time of a algorithm? Cosider ruig the algorithm o a speifi mahie ad measure the ruig time. We aot ompare the ruig time of a algorithm o a mahie with the ruig time of aother algorithm o aother mahie. So, we have to measure the ruig time of every algorithm o a speifi mahie, whih is impossible. Hee, we out the umber of istrutios used by the algorithm. Basi Operatios 13
Istrutios Arithmeti Add, Subtrat, Multiply, Divide, remaider, floor, eilig Data movemet Load, store, opy Cotrol Coditioal brah Uoditioal brah Subroutie all ad retur 14
Ruig Time The ruig time of a algorithm grows with the iput size, whih is the umber of items i the iput. For example, sortig 10 keys is faster tha sortig 100 keys. So the ruig time of a algorithm is desribed as a futio of iput size, for example, T). 15
Ruig Time of Isertio Sort INSERTION-SORTA) ost times for j to legth[a] 1 do key A[j] - 1 i j-1 4-1 while i>0 ad A[i]>key 5 do A[i+1] A[i] 6 tj j i i-1 7 tj A[i+1] key 8 j - 1 T): The sum of produt of ost ad times of eah lie. j tj 16
Ruig Time of Isertio Sort INSERTION-SORTA) ost times for j to legth[a] 1 T ) do key A[j] - 1 1 5 7 i j-1 4-1 t while j i>0 6 ad j A[i]>key 5 j t t j j do A[i+1] A[i] 6 8 4 j i i-1 7 tj j A[i+1] key 8-1 T): The sum of produt of ost ad times of eah lie. j j tj tj 17
Ruig Time of Isertio Sort t i : The umber of times the while loop test is exeuted for j. Note that for, while loop test is exeuted oe time more tha the loop body. 18
Ruig Time of Isertio Sort Although the size of the iput is the same, we have best ase average ase, ad worst ase. 19 ) 8 7 6 5 4 1 t t t T j j j j j j
Ruig time Best Case) Best ase If A[1..] is already sorted, t j = 1 for j =, 3,,. This ruig time a be expressed as A+B for ostats A ad B; it is thus a liear futio of. 0 ) ) ) 8 5 4 8 5 4 1 8 5 4 1 8 7 6 5 4 1 t t t T j j j j j j
Ruig Time Worst Case) Worst ase If A[1..] is sorted i reverse order, tj = j for j =, 3,,. This ruig time a be expressed as A + B + C for ostats A, B, ad C ; it is thus a quadrati futio of. 1 1 j j ad j j ) ) ) ) ) ) 8 5 4 8 7 6 5 4 1 7 6 5 8 7 6 5 4 1 T
Ruig Time Complexity Oly the degree of leadig term is importat. Beause we are oly iterested i the rate of growth or order of growth. For example, a quadrati futio grows faster tha ay liear futio. The degree of leadig term is expressed as O otatio. The worst-ase ruig time of isertio sort is O ).
Spae Cosumptio Θ) spae. Moreover, the iput umbers are sorted i plae. + spae for some ostat. 3
Merge Sort What is merge sort? A sortig algorithm usig merge. What is merge? Give two sorted lists of keys, geerate a sorted list of the keys i the give sorted lists. <1, 5, 6, 8> <, 4, 7, 9> < 1,, 4, 5, 6, 7, 8, 9> 4
Merge Mergig example <1, 5, 6, 8> <, 4, 7, 9> < 5, 6, 8> <, 4, 7, 9> < 5, 6, 8> < 4, 7, 9> < 5, 6, 8> < 7, 9> < 6, 8> < 7, 9> < 8> < 7, 9> < 8> < 9> < > < 9> < 1 > < 1, > < 1,, 4 > < 1,, 4, 5 > < 1,, 4, 5, 6 > < 1,, 4, 5, 6, 7 > < 1,, 4, 5, 6, 7, 8 > < 1,, 4, 5, 6, 7, 8, 9> 5
Merge Ruig time of merge Let 1 ad deote the legths of two sorted lists. Θ 1 + ) time. Mai operatios: ompare ad move #ompariso #movemet Obviously, #movemet = 1 + So, #ompariso 1 + Hee, #ompariso + #movemet 1 + ) whih meas Θ 1 + ). 6
Merge Sort A divide-ad-oquer approah Divide: Divide the keys ito two lists of / keys. Coquer: Sort the two lists reursively usig merge sort. Combie: Merge the two sorted lists. 7
Merge Sort 5 4 7 1 3 6 5 4 7 1 3 6 divide divide 5 4 7 1 3 6 5 4 7 1 3 6 8
Merge Sort 1 3 4 5 6 7 merge 4 5 7 1 3 6 merge merge 5 4 7 1 3 6 5 4 7 1 3 6 9
P seudo ode MERGE-SORTA, p, r) 1 if p < r the q p + r)/ 3 MERGE-SORTA, p, q) 4 MERGE-SORTA, q + 1, r) 5 MERGEA, p, q, r) 30
Ruig Time Divide: Θ The divide step just omputes the middle of the subarray, whih takes ostat time. Coquer: T /) We reursively solve two sub-problems, eah of size /. Combie: Θ) We already showed that mergig two sorted lists of size / takes Θ) time. 31
Ruig Time Reurree) T) a be represeted as a reurree. T ) T / ) ) if =1, if >1 3
Reurree where the ostat represets the time required to solve problems of size 1 as well as the time per array elemet of the divide ad ombie steps. T ) T / ) ) if =1, if >1 T ) T / ) if =1, if >1 33
Reursio tree T) T /) T /) / / T/4) T/4) T/4) T/4) 34
Reursio tree / / /4 /4 /4 /4 35
Reursio tree / / /4 /4 /4 /4 36
Reursio tree / / /4 /4 /4 /4 lg + 1 Total : lg + = Θ lg) 37
Divide ad oquer Suppose that our divisio of the problem yields a subproblems, eah of whih is 1/b the size of the origial. We shall see may divide-ad-oquer algorithms i whih a b. Let D) deote time to divide the problem ito subproblems. Let C) deote time to ombie the solutios to the subproblems ito the solutio to the origial problem. We get the reurree T ) at / b) D ) C ) if, otherwise. 38
Divide ad oquer For merge sort, a = b =. D ) = Θ. C ) = Θ). The worst-ase ruig time T ) of merge sort: T ) T / ) ) if =1, if >1 39
Review Sortig problem Sortig algorithms Isertio sort - O ) Seletio sort - O ) Bubble sort - O ) Merge sort - O lg). 40