Upcoming SlideShare
×

# Merge sort: illustrated step-by-step walk through

3,732 views
3,450 views

Published on

A step-by-step illustration of Merge sort to help you walk through a series of operations. Illustration is accompanied by actual code with bold line indicating the current operation.

4 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
3,732
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
123
0
Likes
4
Embeds 0
No embeds

No notes for slide

### Merge sort: illustrated step-by-step walk through

1. 1. Merge Sort algorithm Illustrated walkthrough
2. 2. Reference “Cracking the coding interview” Fifth edition. Gayle Laakmann McDowell, 2008 - 2013
3. 3. Merge function This function does the most of the heavy lifting, so we look at it first, then see it in the context of Merge Sort algorithm
4. 4. Merge Function for (int i = begin; i <= last; i++) { helper[i] = array[i]; } Part #1: prepare helper int left = begin; int right = middle + 1; int storeIndex = begin; while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } && right <= last) { helper[right]) { = helper[left]; Part #2: pick smaller and copy to the target = helper[right]; storeIndex++; } int remainder = middle - left; for (int i = 0; i <= remainder; i++) { array[storeIndex + i] = helper[left + i]; } Part #3: copy any remainder from left (right not necessary)
5. 5. begin middle last [0] array [1] [2] [3] 10 30 20 40 left sub-array already sorted within the sub-array left sub-array {begin..middle} right sub-array already sorted within the sub-array right sub-array {middle+1..last}
6. 6. [0] array [1] [2] [3] 10 30 20 40 helper for (int i = begin; i <= last; i++) { helper[i] = array[i]; }
7. 7. [0] [1] [2] [3] array 10 30 20 40 helper 10 30 20 40 for (int i = begin; i <= last; i++) { helper[i] = array[i]; }
8. 8. begin middle last [0] [2] [3] 10 array [1] 30 20 40 10 30 20 40 left helper int left = begin; int right = middle + 1; int storeIndex = begin;
9. 9. [0] [2] [3] 10 array [1] 30 20 40 20 40 right left helper 10 30 int left = begin; int right = middle + 1; int storeIndex = begin;
10. 10. store Index [0] [2] [3] 10 array [1] 30 20 40 20 40 right left helper 10 30 int left = begin; int right = middle + 1; int storeIndex = begin;
11. 11. 0 <= 1 is true store Index [0] [2] [3] 10 array [1] 30 20 40 20 40 right left helper 10 30 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } && right <= last) { helper[right]) { = helper[left]; = helper[right]; 2 <= 3 is true
12. 12. 10 <= 20 is true store Index [0] [2] [3] 10 array [1] 30 20 40 20 40 right left helper 10 30 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } && right <= last) { helper[right]) { = helper[left]; = helper[right];
13. 13. store Index [0] [2] [3] 10 array [1] 30 20 40 20 40 right left helper 10 30 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } && right <= last) { helper[right]) { = helper[left]; = helper[right];
14. 14. store Index [0] array [1] [2] [3] 10 30 20 40 20 40 right left helper 10 30 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } && right <= last) { helper[right]) { = helper[left]; = helper[right];
15. 15. store Index [0] array [1] [2] [3] 10 30 20 40 20 40 right left helper 10 30 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } && right <= last) { helper[right]) { = helper[left]; = helper[right];
16. 16. 1 <= 1 is true store Index [0] array [1] [2] [3] 10 30 20 40 20 40 right left helper 10 30 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } && right <= last) { helper[right]) { = helper[left]; = helper[right]; 2 <= 3 is true
17. 17. 30 <= 20 is false store Index [0] array [1] [2] [3] 10 30 20 40 20 40 right left helper 10 30 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } && right <= last) { helper[right]) { = helper[left]; = helper[right];
18. 18. store Index [0] array [1] [2] [3] 10 20 20 40 20 40 right left helper 10 30 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } && right <= last) { helper[right]) { = helper[left]; = helper[right];
19. 19. store Index [0] array [1] [2] [3] 10 20 20 40 right left helper 10 30 20 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } 40 && right <= last) { helper[right]) { = helper[left]; = helper[right];
20. 20. store Index [0] array [1] [2] [3] 10 20 20 40 right left helper 10 30 20 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } 40 && right <= last) { helper[right]) { = helper[left]; = helper[right];
21. 21. 1 <= 1 is true store Index [0] array [1] [2] [3] 10 20 20 40 right left helper 10 30 20 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } 40 && right <= last) { helper[right]) { = helper[left]; = helper[right]; 3 <= 3 is true
22. 22. 30 <= 40 is true store Index [0] array [1] [2] [3] 10 20 20 40 right left helper 10 30 20 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } 40 && right <= last) { helper[right]) { = helper[left]; = helper[right];
23. 23. store Index [0] array [1] [2] [3] 10 20 30 40 right left helper 10 30 20 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } 40 && right <= last) { helper[right]) { = helper[left]; = helper[right];
24. 24. store Index [0] array [1] [2] [3] 10 20 30 40 right left helper 10 30 20 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } 40 && right <= last) { helper[right]) { = helper[left]; = helper[right];
25. 25. store Index [0] array [1] [2] [3] 10 20 30 40 right left helper 10 30 20 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } storeIndex++; } 40 && right <= last) { helper[right]) { = helper[left]; = helper[right];
26. 26. 2 <= 1 is false 3 <= 3 is true store Index [0] array [1] [2] [3] 10 20 30 40 right left helper 10 30 20 while (left <= middle if (helper[left] <= array[storeIndex] left++; } else { array[storeIndex] right++; } Exit while loop storeIndex++; } 40 && right <= last) { helper[right]) { = helper[left]; = helper[right];
27. 27. store Index [0] [2] [3] 10 array [1] 20 30 40 right left helper 1 - 2 = -1 remainder -1 10 30 20 40 int remainder = middle - left; for (int i = 0; i <= remainder; i++) { array[storeIndex + i] = helper[left + i]; }
28. 28. store Index [0] [2] [3] 10 array [1] 20 30 40 right left helper 0 <= -1 is false remainder -1 10 30 20 40 int remainder = middle - left; for (int i = 0; i <= remainder; i++) { array[storeIndex + i] = helper[left + i]; } Skip over for loop
29. 29. store Index [0] array [1] [2] [3] 10 20 30 40 right left helper 10 30 Already there because right sub-array already occupies tail end of the target array 20 40 int remainder = middle - right; for (int i = 0; i <= remainder ; i++) { array[storeIndex + i] = helper[ right + i]; } Not needed
30. 30. Merge Sort Algorithm Now we use Merge function
31. 31. begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
32. 32. 0 >= 3 is false begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
33. 33. begin last middle [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
34. 34. Merge & Sort the left sub-array first begin Call Stack #0 last middle [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
35. 35. Call Stack #1 Call Stack #0 begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
36. 36. 0 >= 1 is false Call Stack #1 Call Stack #0 begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
37. 37. Call Stack #1 Call Stack #0 middle begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
38. 38. Call Stack #1 Call Stack #0 middle begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
39. 39. Call Stack #3 Call Stack #1 Call Stack #0 begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
40. 40. 0 >=0 is true Call Stack #3 Call Stack #1 Call Stack #0 begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
41. 41. 0 >=0 is true Call Stack #3 Call Stack #1 Call Stack #0 begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } Base condition is met! int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
42. 42. Call Stack #1 Call Stack #0 middle begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
43. 43. 1 >=1 is true Call Stack #3 Call Stack #1 Call Stack #0 begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
44. 44. Call Stack #3 Call Stack #1 Call Stack #0 begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } Base condition is met! int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
45. 45. Call Stack #1 Call Stack #0 middle begin last [0] [1] [2] [3] 5 7 3 2 Merge if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
46. 46. Already sorted (unchanged but sorted) Call Stack #1 Call Stack #0 middle begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last); (implicit return at the end of function)
47. 47. Merge & Sort the right sub-array next Call Stack #0 begin last middle [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);
48. 48. Walkthrough ends here. The right sub-array is processed the same way as the left sub-array. After that, Merge is called with two already-sorted sub-arrays Call Stack #1 Call Stack #0 begin last [0] [1] [2] [3] 5 7 3 2 if (begin >= last) { return; } int middle = (begin + last) / 2; MergeSort(array, helper, begin, middle); MergeSort(array, helper, middle + 1, last); Merge(array, helper, begin, middle, last);