1 / 12

Insertion Sort

16. 5. 10. 7. 9. 6. 15. 11. 9. 9. 7. 7. 6. 6. 15. 15. 16. 5. 10. 11. 11. 16. 5. 10. 16. 16. 5. 5. 10. 10. 6. 6. 7. 7. 9. 9. 15. 15. 11. 11. 10. 10. 16. 16. 16. 11. 11. 5. 5. 5. 15. 15. 10. 10. 10. 5. 5. 6. 7. 7. 6. 6. 9. 7. 9. 7. 7.

carlosfitch
Télécharger la présentation

Insertion Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 16 5 10 7 9 6 15 11 9 9 7 7 6 6 15 15 16 5 10 11 11 16 5 10 16 16 5 5 10 10 6 6 7 7 9 9 15 15 11 11 10 10 16 16 16 11 11 5 5 5 15 15 10 10 10 5 5 6 7 7 6 6 9 7 9 7 7 6 9 6 9 9 15 15 15 16 16 11 11 11 15 15 16 16 10 10 5 5 6 6 7 7 9 9 11 11 10 16 16 5 15 5 10 16 10 6 5 9 7 6 7 7 9 6 15 9 15 11 11 11 16 5 10 6 7 9 15 11 15 16 10 5 6 7 9 11 10 10 15 15 16 16 5 5 6 6 7 7 9 9 11 11 Insertion Sort

  2. Divide and Conquer (1.3/2.3) • Divide (into two equal parts) • Conquer (solve for each part separately) • Combine separate solutions • Merge sort • Divide into two equal parts • Sort each part using merge-sort (recursion!!!) • Merge two sorted subsequences

  3. Merge Two Subsequences x[1]-x[2]- … - x[k] y[1]-y[2]- … - y[l] if y[i] > x[j]  y[i+1] > x[j] < k+l-1 edges = # (comparisons)

  4. 179 310 351 652 285 179 179 310 310 351 351 652 652 423 423 423 861 861 861 254 254 254 450 450 450 520 520 520 179 179 310 285 285 285 310 179 310 351 652 351 652 351 423 423 423 423 423 450 861 861 861 861 520 254 254 254 254 652 450 450 450 450 861 520 520 520 520 179 179 179 254 285 285 310 310 310 351 351 351 652 652 652 423 423 423 861 861 861 254 254 254 450 450 450 520 520 520 179 285 285 285 310 351 652 285 254 450 520 423 861 254 450 520 254 450 520 310 179 179 652 652 351 351 423 423 423 861 861 861 254 450 520 179 285 310 179 351 652 351 652 423 861 285 179 652 351 423 254 861 423 450 520 861 254 285 285 254 450 520 310 285 310 310 450 520 285 285 310 310 285 423 861 254 285 310 310 179 351 652 179 179 179 351 652 652 351 351 450 520 652 Merge Sort

  5. 1 2 3 4 5 6 7 8 1 3 5 8 2 4 6 7 1 5 3 8 4 7 2 6 5 1 5 1 7 4 6 2 Recursion Tree log n • n comparisons per level • log n levels • total runtime = n log n

  6. Asymptotic Notations (2.1/3.1) • BIG O: O • f = O(g) if f is no faster then g • f / g < some constant • BIG OMEGA:  • f = (g) if f is no slower then g • f / g > some constant • BIG Theta:  • f = (g) if f has the same growth rate as g • some constant < f / g < some constant

  7. Strassen’s Algorithm: Matrix Muliplication (31.2/28.2) • Strassen’s recurrence • Strassen’s merge • Best • Divide and conquer • Merge • Recurrence

  8. Quicksort (8.1-8.2/7.1-7.2) • Sorts inplacelike insertion unlike merge • Divideinto two parts such that • elements of left part < elements of right part • Conquer: recursively solve for each part separately • Combine: trivial - do not do anything • Quicksort(A,p,r) • if p <r • then q  Partition(A,p,r) • Quicksort(A,p,q) • Quicksort(A,q+1,r) //divide //conquer left //conquer right

  9. Divide = Partition PARTITION(A,p,r) //Partition array from A[p] to A[r] with pivot A[p] //Result: All elements  original A[p] has index  i x = A[p] i = p - 1 j = r + 1 repeat forever repeat j = j - 1 until A[j]  x repeat i = i +1 until A[i]  x if i < j then exchange A[i]  A[j] else return j

  10. 9 9 7 7 6 6 15 15 16 16 5 5 10 10 11 11 i i j j 9 7 6 15 16 5 10 11 5 7 6 15 16 9 10 11 9 7 6 15 16 5 10 11 left left left left left right right right right right i i j 5* 7 6 15 16 j 9* 10 11 9 7 6 15 16 5 10 11 i 5 7 6 15 16 9 10 j 11 9 7 6 15 16 5 10 11 5 5 5 7 7 7 6 6 6 15 15 15 16 16 16 9 9 9 10 10 10 11 11 11 5 5 7 7 6 6 15 15 16 16 9 9 10 10 11 11 5 7 6 15 16 9 10 11 5 5 5 i 7 7 7 6 6 6 15 15 15 16 16 16 9 9 9 10 10 10 11 11 11 j i left right j i i j j i i 5 7 6 j j 15 16 j 9 10 11 left right i i 5 5 5 5 5 9 5 7 7 7 7 7 7 7 6 6 6 6 6 6 6 15 15 15 15 15 15 15 j 16 16 16 16 16 16 16 9 9 9 5 9 9 9 j 10 10 10 10 10 10 10 11 11 11 11 11 11 11 j i i i left j j j right 9 7 6 15 16 5 10 11 left right 9 9 left 7 7 6 6 15 15 16 16 5 5 right 10 10 11 11 i j left left right right i i j i j left right i i j left right 5 7 6 15 16 9 10 11 i i left right left right j j left left left right right right left right 5 6 7 15 16 9 10 11 5 6 7 11* 16 9 10 15* 5 left left 7 6 15 16 right right 9 10 11 5 6 7 11 10 9 16 15 j j 5 5 7 6 6 7 15 15 16 16 9 9 10 10 11 11 i j 5 6 7 11 10 9 16 15 left right 5 6 7 15 16 9 10 11 i 5 j 7 6 15 16 9 10 11 5 6 7 11 10 9 16 15 5 5 5 7 6 6 6 7 7 15 9 11 16 10 16 9 11 9 10 15 10 11 16 15 i 5 5 6 6 7 7 j 9 9 10 10 11 11 16 15 15 16 j i i j i j 5 5 6 6 7 7 15 11 16 16 9 9 10 10 15 11 i j j i j 5 6 7 15 16 9 10 11 i i j j i How It Works

  11. 0123456789 0 123456789 n 89 8 9 Runtime of Quicksort • Worst case: • every time nothing to move • pivot = left (right) end of subarray • O(n^2)

  12. Runtime of Quicksort • Best case: • every time partition in (almost) equal parts • no worse than in given proportion • O(n log n) • Average case • O(n log n)

More Related