sorting sorting
play

Sorting Sorting - re - arranging elements of a sequence 5 st So I - PowerPoint PPT Presentation

Sorting Sorting - re - arranging elements of a sequence 5 st So I S , I Sz E - I Sn - I - - . 5 sorting algorithms look at - We will : - 3 iterative - 2 recursive Theiteratirealgorithn " t maintain a partition : " unsorted t


  1. Sorting

  2. Sorting - re - arranging elements of a sequence 5 st So I S , I Sz E - I Sn - I - - . 5 sorting algorithms look at - We will : - 3 iterative - 2 recursive

  3. Theiteratirealgorithn " t • maintain a partition : " unsorted t " ' short a sequence of • Sort elements in n -1 n stages . at each stage , move 1 element from the unsorted part to the sorted part : " sorted " " unsorted " - - - 1- 1 stage moves 1 element ✓ sort (A) { • initialize - repeat n - i times move 1 element from unsorted to sorted part } - the algorithms differ in how they : - select an element to remove from the unsorted part it into the sorted part - insert

  4. Insertion Sort n -1 elements in unsorted part - - initially : sorted part is just AH - - repeat at times : ~ unsorted Sorted - remove the first element ← from the unsorted part TI t - insert it into the sorted part ( shifting elements as needed ) , 1¥ to the right - - unsorted sorted - sort (A) insertion - 1) { forli -- 1 ton -_ Afilflfirst element in unsorted part pivot whileljtoAUDALjtspir.DE/gssIIteaoYepakrIfthsat j=i - I " - ALJIXshiftg.tt are larger than pivot A- Ljti ] " e- to the right . " J=j - I } ALjttj-pivotkmorepirot.info position . }

  5. insertiohnstExamol.es ④ 1. start . ② b stages . ⑥ ' II : : ① . ③ ] stage 5- I -

  6. Selection Sort unsorted - - - initially , sorted part empty 1- - - repeat n - 1 times Existed - find the smallest element ¥ in the unsorted part swap - move it to the first position , i1 sorted b) - which becomes the new unsorted last position of sorted part . this is its selection - sort (A) 1 ¥nnd [ final location for li=1 ton - 1) { isindexofminfounds-fa.ie/emewtwhileCkan -1 " I I ){ if( ALKIAALJJ )j=k ; in unsorted k=ktt { wap Ali -13 and ACJI } }

  7. selectionsortExamplel.SI#&)stase1 a) stash - 5- 1 stage 3 ## ④ 1st 4 . #¥s , I st.sn . g

  8. 0.cn ) time Selections Heaps takes Initiated part empty ] - - make unsorted part into a heap - repeat n - 1 times ( vs . OLD for the - find the smallest element } heap extract Logan ) scan in selection s takes in the unsorted part sort ) . - move it to the first position time which becomes the new last position of sorted part . Consider the organization of array contents : ¥4 ① 1- I Z if this is the root of the heap , then it is also the smallest element in part , so is in its the usorted correct final position use this To . arrangement , the root of the so we have keeps moving , heap lots of shifting to do .

  9. ¥4 ② T If this is the root of the heap , then everything works : a percolate move the last leaf - down ; root t do D to the - we extract • ; now free is the correct final , and • where which is store is way , E →← ¥ location to • , after have : which we ÷#÷ is at re - code our heap implementation s.tv the root Bet : we must . that the indexing - D , with the result less intuitive A Ln is now . this arrangement max - heap , and ③ Instead use a : , we unsorted sorted ← root of Elas teat . heap I . the heap root is at A Lot Now : - heap extraction removes • , moves D to A Lot , • belongs freeing up the spot where . : -77 Leaving us I a min Heap is just replacing man heap Re - coding into a with and vice < versa . >

  10. Heapsort Sextons unsorted - - - initially , sorted part empty 7- heap with max here - make unsorted part into a max heap - repeat n - 1 times largest ! ! " - find the 'ls#f" element I in the unsorted part unsortejd-hsfted-move.it to the first position last is u ' take last leaf ' end ' of heap from which becomes the new test position of sorted part newest element . × first 7¥ sorted part this is its ¥-4 new root of heap sort (A) { final location heap ( which then buildmaxheap.LA ) gets percolated n - 1) E for LE1 to down ) - extract maxll ALn.is } 1- - unsorted heap , - } of size 1 J sorted has smallest element .

  11. Heap sort with in line - down percolate - heap sort (A) I make Max Heap (A) A more last leaf to root n - 1) { for Ci= I to AL03 and Ahn - i ] 4 and old root to where K last leaf was . swap = size of unsorted part gite ← n - it IN size of heap } I ← 0 a site ){ while ( 2J t t percolate e. hied ← 2J + I A L2J " 3) { down 2 ] if ( 2J t 2 a site L A AND jt ← 2J t 2 child g. asians it In , } else ← size X terminate the while . } j } } }

  12. ↳ HeapsortExampj.FI#fpdmYniEp g) stage ' I z¥ stages * ' " stages [ s I staged - ¥¥#I stages

  13. Heapsortexamptef " Dma . stage ÷÷÷÷:÷:÷±÷ stages . staged 12% stages

  14. TmelompkxityofIterativgAgithms - each algorithm does exactly n -1 stages k input ) ith stage varies with the algorithm ( • the work done at the . # of item comparisons as a measure of work Aim . we take - exactly comparisons to find ' - min . n - i Selection Sort element in unsorted part - - between 1 and i comparisons to find Insertion sort - location for pivot land 26g . Ln - it ' ) H¥t between comparisons : - for - down percolate

  15. Number of comparisons * - We must verify # comparisons for constant some - is an upper bound times # comparisons ) on each algorithm by work done . - # of assignments ( 4 swaps ) also matters in actual time . run

  16. Selection On input of size n , # of comparisons is always ( regardless of input ) : = Iii Etait = Sln - i ) = In - 1) ( n ) 2- = n 2 = Etna )

  17. Insertionsort-worstcesellpper-B.vn = MI- = 0 ( n ' ) d # comparisons i E : . Worst case : initial sequence is in reuerder trowed : . I Eg . the stage In the we have - - -kiT comparisons , because the I This takes sorted part is of size i . =D ( ne ) So , # Comparisons > ① ( n ' ) So , Insertion sort worst case is

  18. Insertion Sort Best Case is fully - case : initial sequence Best ordered . Then : In each stage exactly 1 comparison is made . = ① ( n ) = n - I So : # comparisons .

  19. tteapsortworstcaseupp.es Bound : . 5 2 log . Ln - it 1) # Comparisons I log . Citi ) 2 = I 2 login I 2h Logan = Oln log n ) Lower Bound ? - BestCas ( what input would to lead - down ? movement during percolate case ? ) What if we exclude this

  20. Recorsiredividekconguersorting.pe rtiti on the sequence A into two parts AI , Az . Recursively sort each of A , and Ac - Combine the sorted versions of A , and Az to a sorted version of A obtain T Partition y y \ 1- - ' I I sort → \ 1=1 Combine h - \ - I The algorithms differ in how they choose the and how they combine the partition , sorted parts

  21. Mergesortn : . . Uses the fact that merging two lists is easy sorted * " TT I ¥t 4 P p p • Takes ON time is the total , where size n

  22. Merge sort - : - partition : first half 4 half second . - Combine i merge the parts - ran ;!q ⇐ ¥¥¥¥* merge 2 parts If 3HH • Works with linked - list ⇐ array implementations . in array implementations , A Ln ) extra space uses

  23. MergesortmergesortfA.to , hill if ( lol hi ){ 1 there are 72 items , so work to do mid ← Ll lo thi ) 121 merge sort ( A , Io , mid ) , midt 1 , hi ) ( A merge sort , hi ) merge ( A , low , mid } }

  24. mergeformergesortme.ge/A,loimidihi)l After * , the sorted ( ← to in r ← midt1 is sequence Lo n ← re hi )l while ( llmid AND . . Bfhi ] BIO ] it AG3A AG3H . . BLIND :* :* .am too . htt A. B : swap 3 while ( Lamia )E B43 ← ALL ] temp ← A Htsntt { hilecrahi )E A A- B ' B * temp BED ← AH rttsntt 3*3

  25. tmeLomplexityofHergeSort_ viatreeofrecursireca.tl# n - 041 - → nf \ N12-0LN ) - us . Hoo . Yoo Yoo Hoo Yoo noo Noo Yoo ' : : : : : : : : : 2 I i. A 041 a perfect binary n is a power of 2 , the tree of recursive calls If is - and height login tree with leaves n . , 2 ' calls to merge , each to merge two . At depth i there are of of lists 4241 site " hi into site one . = 0 ( n ' Ii ) otal work at depth i is 246 = ON . T i ) n ) = login . OH =0(n log - OLD otal work is # depths T .

  26. Quiiks " and to partition sequence into " small • Uses a pivot " etemeuts : < large elements " large small elements < p - combining is trivial sorted versions → - # partition choose a r - my If 4 ← two parts . Tip p values > p values IP in order . order in . • choosing pivots performance key to is .

  27. Quickso , lo , hi ){ quick sort ( A if ( lo chi ){ 4 there are 72 items pivot position ← partition ( A. to , hi ) A partition - 1) quick sort ( A , Io , pivot position quick sort 1A , pivot position tt , hi ) } } to partition D Quick sort as long is correct as every call returns variables satisfying the following leaves the and : E pivot position ± hi to I. lo ti E pivot position Eje hi 2. for every i. j with Ali Is A ( pivot position ] I A- Chi ] on choice of pivot Howeve , efficiency relies critically .

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend