Upcoming SlideShare
×

Like this presentation? Why not share!

# Merge sort

## on Jan 28, 2014

• 298 views

### Views

Total Views
298
Views on SlideShare
298
Embed Views
0

Likes
0
10
0

No embeds

### Categories

Uploaded via as Microsoft PowerPoint

### Report content

• Comment goes here.
Are you sure you want to
Your message goes here

## Merge sortPresentation Transcript

• Merge Sort
• Merge Sort: Idea Divide into two halves A FirstPart SecondPart Recursively sort SecondPart FirstPart Merge A is sorted! 2
• Merge Sort: Algorithm Merge-Sort (A, left, right) if left ≥ right return else middle ← b(left+right)/2 Merge-Sort(A, left, middle) Recursive Call Merge-Sort(A, middle+1, right) Merge(A, left, middle, right) 3
• Merge-Sort: Merge Sorted A: merge Sorted FirstPart Sorted SecondPart A: A[left] A[middle] A[right] 4
• Merge-Sort: Merge Example A: 2 5 3 5 7 8 1 15 28 30 4 6 5 6 10 14 R: L: 3 5 15 28 6 10 14 22 Temporary Arrays 5
• Merge-Sort: Merge Example A: 3 1 5 15 28 30 6 10 14 k=0 R: L: 2 3 i=0 15 28 30 3 7 8 1 6 10 14 22 4 5 6 j=0 6
• Merge-Sort: Merge Example A: 1 2 5 15 28 30 6 10 14 k=1 R: L: 2 3 i=0 3 5 15 28 7 8 1 6 10 14 22 4 5 6 j=1 7
• Merge-Sort: Merge Example A: 1 2 3 15 28 30 6 10 14 k=2 R: L: 2 3 i=1 7 8 1 6 10 14 22 4 5 6 j=1 8
• Merge-Sort: Merge Example A: 1 2 3 4 6 10 14 k=3 R: L: 2 3 7 i=2 8 1 6 10 14 22 4 5 6 j=1 9
• Merge-Sort: Merge Example A: 1 2 3 4 5 6 10 14 k=4 R: L: 2 3 7 i=2 8 1 6 10 14 22 4 5 6 j=2 10
• Merge-Sort: Merge Example A: 1 2 3 4 5 6 10 14 k=5 R: L: 2 3 7 i=2 8 1 6 10 14 22 4 5 6 j=3 11
• Merge-Sort: Merge Example A: 1 2 3 4 5 7 6 14 k=6 R: L: 2 3 7 i=2 8 1 6 10 14 22 4 5 6 j=4 12
• Merge-Sort: Merge Example A: 1 2 3 4 5 6 7 8 14 k=7 R: L: 2 3 3 5 15 28 7 8 i=3 1 6 10 14 22 4 5 6 j=4 13
• Merge-Sort: Merge Example A: 1 2 3 4 5 6 7 8 k=8 R: L: 2 3 3 5 15 28 7 8 1 6 i=4 10 14 22 4 5 6 j=4 14
• Merge(A, left, middle, right) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. n1 ← middle – left + 1 n2 ← right – middle create array L[n1], R[n2] for i ← 0 to n1-1 do L[i] ← A[left +i] for j ← 0 to n2-1 do R[j] ← A[middle+j] k ← i ← j ← 0 while i < n1 & j < n2 if L[i] < R[j] A[k++] ← L[i++] else A[k++] ← R[j++] while i < n1 A[k++] ← L[i++] while j < n2 A[k++] ← R[j++] n = n +n 1 2 Space: n Time : cn for some constant c 15
• Merge-Sort(A, 0, 7) Divide A: 6 2 8 4 33 7 7 55 11 16
• Merge-Sort(A, 0, 7) , divide Merge-Sort(A, 0, 3) 3 A: 6 2 88 7 5 1 44 17
• Merge-Sort(A, 0, 7) , divide Merge-Sort(A, 0, 1) 3 A: 8 6 7 5 1 4 2 18
• Merge-Sort(A, 0, 7) , base case Merge-Sort(A, 0, 0) 3 A: 8 7 5 1 4 2 6 19
• Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 0), return 3 A: 8 6 7 5 1 4 2 20
• Merge-Sort(A, 0, 7) , base case Merge-Sort(A, 1, 1) 3 A: 8 7 5 1 4 6 2 21
• Merge-Sort(A, 0, 7) Merge-Sort(A, 1, 1), return 3 A: 8 6 7 5 1 4 2 22
• Merge-Sort(A, 0, 7) Merge(A, 0, 0, 1) 3 A: 8 2 7 5 1 4 6 23
• Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 1), return 3 A: 2 6 8 7 5 1 4 24
• Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 3) , divide 3 A: 2 7 5 1 6 8 4 25
• Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 2), base case 3 A: 2 7 5 1 6 4 8 26
• Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 2), return 3 A: 2 7 5 1 6 8 4 27
• Merge-Sort(A, 0, 7) Merge-Sort(A, 3, 3), base case A: 2 6 8 4 28
• Merge-Sort(A, 0, 7) Merge-Sort(A, 3, 3), return 3 A: 2 7 5 1 6 8 4 29
• Merge-Sort(A, 0, 7) Merge(A, 2, 2, 3) 3 A: 2 7 5 1 6 4 8 30
• Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 3), return 3 A: 2 6 4 7 5 1 8 31
• Merge-Sort(A, 0, 7) Merge(A, 0, 1, 3) 3 A: 2 4 6 7 5 1 8 32
• Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 3), return A: 2 4 6 8 3 7 5 1 33
• Merge-Sort(A, 0, 7) Merge-Sort(A, 4, 7) A: 2 4 6 8 3 7 5 1 34
• Merge-Sort(A, 0, 7) Merge (A, 4, 5, 7) A: 2 4 6 8 1 3 5 7 35
• Merge-Sort(A, 0, 7) Merge-Sort(A, 4, 7), return A: 2 4 6 8 1 3 5 7 36
• Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 7), done! Merge(A, 0, 3, 7) A: 1 2 3 4 5 6 7 8 37
• Merge-Sort Analysis cn n n/4 2 2 × cn/2 = cn n/2 n/2 n/4 2 n/4 n/4 2 4 × cn/4 = cn log n levels n/2 × 2c = cn Total: cn log n • Total running time: (nlogn) • Total Space: (n) 38
• • Let T(n) be the time taken by the merge sort to sort an array of n elements. • Divide the T into U and V takes linear time. • Hence, T ( n) T n 2 T n 2 g ( n) where g ( n) ( n) This recurrenc e w hic h becomes , T(n) n 2T 2 g ( n)
• n T(n) 2T g ( n) 2 a 2 b 2 f(n) g(n) n log b a n log 2 2 n f ( n) ( n) case 2 of Master's theorm applied then T(n) (n log b a log n) (n log n)
• Merge-Sort Summary Approach: divide and conquer Time – Most of the work is in the merging – Total time: (n log n) Space: – (n), more space than other sorts. 41