Download presentation
Presentation is loading. Please wait.
Published bySusanti Halim Modified over 6 years ago
1
Insertion Sort 6 7 9 15 16 5 10 11 9 7 6 15 16 5 10 11 7 9 6 15 16 5 10 11 6 7 9 15 16 5 10 11 5 6 7 9 10 15 16 11 5 6 7 9 10 11 15 16 5 6 7 9 10 11 15 16 5 6 7 9 15 16 10 11 5 6 7 9 15 16 10 11 5 6 7 9 10 15 16 11 5 6 7 9 10 15 16 11 6 7 9 15 16 5 10 11 7 9 6 15 16 5 10 11 9 7 6 15 16 5 10 11 7 9 6 15 16 5 10 11 6 7 9 15 16 5 10 11 6 7 9 15 16 5 10 11 9 7 6 15 16 5 10 11 5 6 7 9 15 16 10 11
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
Merge Sort 285 179 652 351 423 310 861 254 450 520 285 310 351 652 423 179 861 254 450 520 285 310 351 652 423 179 861 254 450 520 254 285 310 351 423 179 450 520 652 861 285 310 351 652 254 179 423 450 520 861 285 310 351 652 423 179 861 254 450 520 285 310 351 652 423 179 861 254 450 520 285 310 351 652 423 179 861 254 450 520 285 310 351 652 423 179 861 254 450 520 285 310 351 652 423 179 861 254 450 520 310 179 351 652 423 285 861 254 450 520 285 179 652 351 423 310 861 254 450 520 285 179 652 351 423 310 861 254 450 520 310 179 652 351 423 285 861 254 450 520 310 179 652 351 423 285 861 254 450 520 310 179 351 652 423 285 861 254 450 520 310 179 652 351 423 285 861 254 450 520 285 310 351 652 423 179 861 254 450 520 285 179 652 351 423 310 861 254 450 520
5
Recursion Tree 1 5 3 8 5 1 4 7 2 6 7 4 6 2 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)
Divide and conquer Merge Recurrence Strassen’s recurrence Strassen’s merge Best
8
Quicksort (8.1-8.2/7.1-7.2) Quicksort(A,p,q) then q Partition(A,p,r)
Sorts in place like insertion unlike merge Divide into 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
How It Works 10 5 7 6 15 16 9 11 left right j i 10 5 7 6 15 16 9 11 left right j i 10 5 7 6 15 16 9 11 left right j i 10 5 7 6 15 16 9 11 left right j i 10 5 6 7 15 16 9 11 left right 10 5 7 6 15 16 9 11 left right 10 5 7 6 15 16 9 11 left right 10 5 7 6 15 16 9 11 left right 10 5 7 6 15 16 9 11 left right 10 5 7 6 15 16 9 11 left right j i 10 5 6 7 15 16 9 11 left right 10 5 6 7 15 16 9 11 left right 16 5 6 7 11 10 9 15 left right j i 16 5 6 7 11 10 9 15 left right 16 5 6 7 9 10 11 15 left right 15 5 6 7 9 10 11 16 left right 16 5 6 7 11 10 9 15 left right j i 10 5 6 7 11 16 9 15 left right j i 10 5 6 7 15 16 9 11 left right j i 10 5 6 7 11* 16 9 15* left right j i 10 5 6 7 11 16 9 15 left right j i 10 5 6 7 15 16 9 11 left right j 10 5 7 6 15 16 9 11 left right 10 9 7 6 15 16 5 i j 11 10 9 7 6 15 16 5 i j 11 10 9 7 6 15 16 5 i j 11 9 7 6 15 16 5 10 i j 11 9 7 6 15 16 5 10 i j 11 9 7 6 15 16 5 10 11 i j 9 7 6 15 16 5 10 11 i j 9 7 6 15 16 5 10 11 i j 10 5* 7 6 15 16 9* i j 11 10 9 7 6 15 16 5 i j 11 15 5 6 7 9 10 11 16 10 5 7 6 15 16 9 i j 11 10 9 7 6 15 16 5 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11 10 5 7 6 15 16 9 i j 11
11
Runtime of Quicksort Worst case: 0123456789 123456789 8 9 89 n
every time nothing to move pivot = left (right) end of subarray O(n^2) 8 9 89 n
12
Runtime of Quicksort Average case Best case:
every time partition in (almost) equal parts no worse than in given proportion O(n log n) Average case
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.