Insertion Sort Bubble Sort Selection Sort

31
Analysis of Algorithms CS 477/677 Sorting – Part A Instructor: George Bebis (Chapter 2)

description

sort

Transcript of Insertion Sort Bubble Sort Selection Sort

Analysis of AlgorithmsCS 477/677Sorting Part AInstructor: George Bebis(Chapter 2)

!he Sorting Problem"Input: A se#uence of n numbers a1, a2, . . . , an"Output: A $ermutation %reor&ering' a1, a2, . . . , an of the in$ut se#uence such that a1 a2 an(Structure of &ata

4)hy Stu&y Sorting Algorithms*"!here are a +ariety of situations that ,e can encounter-o ,e ha+e ran&omly or&ere& .eys*Are all .eys &istinct*/o, large is the set of .eys to be or&ere&*0ee& guarantee& $erformance*"1arious algorithms are better suite& to some of these situations2Some -efinitions"Internal Sort!he &ata to be sorte& is all store& in the com$uter3s main memory4"56ternal SortSome of the &ata to be sorte& might be store& in some e6ternal7 slo,er7 &e+ice4"In Place Sort!he amount of e6tra s$ace re#uire& to sort the &ata is constant ,ith the in$ut si8e46Stability"A S!AB95 sort$reser+es relati+e or&er of recor&s ,ith e#ual .eysSorte& on first .ey:Sort file on secon& .ey::ecor&s ,ith .ey +alue ( are not in or&er on first .ey;;7Insertion Sort"I&ea: li.e sorting a han& of $laying car&sStart ,ith an em$ty left han& an& the car&s facing &o,n on the table4:emo+e one car& at a time from the table7 an& insert it into the correct $osition in the left han&" com$are it ,ith each of the car&s alrea&y in the han&7 from right to left!he car&s hel& in the left han& are sorte&" these car&s ,ere originally the to$ car&s of the $ile on the table>Insertion Sort246>(in$ut array left sub@arrayright sub@arrayat each iteration7 the array is &i+i&e& in t,o sub@arrays:sorte&unsorte&>Insertion Sort>(I0S5:!IA0@SA:!Alg.: I0S5:!IA0@SA:!(A)for j 2 to ndo key B A[ j ]Insert A[ j ] into the sorte& se#uence A[1 . . j -1] i j - 1 whi%e i > 0 an& A[i] > keydo A[i + 1] A[i] i i 1 A[i + 1] key "Insertion sort sorts the elements in $lacea>( 4 2 6 7 49oo$ In+ariant for Insertion SortAlg.: I0S5:!IA0@SA:!(A)for j 2 to ndo key B A[ j ]Insert A[ j ] into the sorte& se#uence A[1 . . j -1] i j - 1 whi%e i > 0 an& A[i] > keydo A[i + 1] A[i] i i 1 A[i + 1] key Invariant: at the start of the for loo$ the elements in A[1 . . j-1] are in sorte& or&er>2Pro+ing 9oo$ In+ariants"Pro+ing loo$ in+ariants ,or.s li.e in&uction"Initia%i&ation (base 'ase): It is true $rior to the first iteration of the loo$"(aintenan'e (indu'tive step): If it is true before an iteration of the loo$7 it remains true before the ne6t iteration"Termination: )hen the loo$ terminates7 the in+ariant gi+es us a useful $ro$erty that hel$s sho, that the algorithm is correct Sto$ the in&uction ,hen the loo$ terminates>69oo$ In+ariant for Insertion Sort"Initia%i&ation: Cust before the first iteration7 j = 2:the subarray A[1 . . j-1]D A[1], %the element originally in A[1]' is sorte&>79oo$ In+ariant for Insertion Sort"(aintenan'e: the whi%e inner loo$ mo+es A[j -1], A[j -2], A[j -3], an& so on7 by one $osition to the right until the $ro$er $osition for key %,hich has the +alue that starte& out in A[j]' is foun&At that $oint7 the +alue of key is $lace& into this $osition4>' tj D > !"n# = c1n + c2"n -1# + c4"n -1# + c5"n -1# + c8"n-1# = "c1 + c2 + c4 + c5 + c8#n + "c2 + c4 + c5 + c8#= an + $ = "n#)whi%e i H ? an& AFiG H .eyK( ) ( ) ) 1 ( 1 1 ) 1 ( ) 1 ( ) (8272625 4 2 1 + + + + + + = = = =n c t c t c t c n c n c n c n Tnjjnjjnjj>)orst Case Analysis"!he array is in re+erse sorte& or&er Al,ays A[i] > key in whi%e loo$ test /a+e to com$are key ,ith all elements to the left of the j-th $osition com$are ,ith j-1 elements tj = j a #ua&ratic function of n !"n# = "n2# or&er of gro,th in n21 2 2( 1) ( 1) ( 1)1 ( 1)2 2 2n n nj j jn n n n n nj j j= = =+ + = => = => = ) 1 (2) 1 (2) 1 (12) 1 () 1 ( ) 1 ( ) (8 7 6 5 4 2 1 +++++ + + = n cn ncn ncn nc n c n c n c n Tc bn an + + =2)whi%e i H ? an& AFiG H .eyK( ) ( ) ) 1 ( 1 1 ) 1 ( ) 1 ( ) (8272625 4 2 1 + + + + + + = = = =n c t c t c t c n c n c n c n Tnjjnjjnjjusing,e ha+e:

Com$arisons an& 56changes in Insertion SortI0S5:!IA0@SA:!(A)for E Bto ndo .ey B AF E GInsert AF E G into the sorte& se#uence AF> 4 4 E @>G i B E @ > whi%e i H ? an& AFiG H .eydo AFi I >G B AFiGi B i > AFi I >G B .eycosttimesc1nc2 n-10n-1c4 n-1c5c6 c7 c8n-1 =njjt2=njjt2) 1 (=njjt2) 1 ( n2%2 c&'(a)i*&n* n2%2 e+c,an-e*(Insertion Sort @ Summary"A&+antagesGoo& running time for Lalmost sorte&K arrays %n'"-isa&+antages"n2# running time in ,orst an& a+erage case n

/ com$arisons an& e6changes4Bubble Sort %564 @7 $age ( (= 6 4 Ei7Bubble@Sort :unning !ime!,1*,!"n# = "n2#221 1 1( 1)( )2 2 2n n ni i in n n nwhere n i n i n= = =+ = = = Alg.: BMBB95SA:!%A'for i 1 to .en-/,[A]do for j .en-/,[A] downto i + 1do if A[j] 0 A[j -1]then e6change A[j] A[j-1]!"n# =c1"n+1# ++ + =nii n1) 1 (c2c3=+ nii n1) (c4 =nii n1) (= "n# + "c2 + c2 + c4#=nii n1) (Com$arisons: n2%256changes: n2%2c1

c2 c3 c4