merge sort: illustrated step-by-step walk through

48
Merge Sort algorithm Illustrated walkthrough

Upload: yoshi-watanabe

Post on 24-May-2015

1.597 views

Category:

Technology


3 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Merge sort: illustrated step-by-step walk through

Merge Sort algorithmIllustrated walkthrough

Page 2: Merge sort: illustrated step-by-step walk through

Reference

“Cracking the coding interview” Fifth edition. Gayle Laakmann McDowell, 2008 - 2013

Page 3: Merge sort: illustrated step-by-step walk through

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

Page 4: Merge sort: illustrated step-by-step walk through

for (int i = begin; i <= last; i++) { helper[i] = array[i];}

int left = begin;int right = middle + 1;int storeIndex = begin;

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

int remainder = middle - left;for (int i = 0; i <= remainder; i++) { array[storeIndex + i] = helper[left + i];}

Part #1: prepare helper

Part #2: pick smaller and copy to the target

Part #3: copy any remainder from left(right not necessary)

Merge Function

Page 5: Merge sort: illustrated step-by-step walk through

10 30 20 40

[0] [1] [2] [3]

array

begin middle last

left sub-arrayalready sorted within the sub-array

right sub-arrayalready sorted within the sub-array

left sub-array{begin..middle}

right sub-array{middle+1..last}

Page 6: Merge sort: illustrated step-by-step walk through

10 30 20 40

[0]

for (int i = begin; i <= last; i++) { helper[i] = array[i];}

[1] [2] [3]

helper

array

Page 7: Merge sort: illustrated step-by-step walk through

10 30 20 40

[0]

for (int i = begin; i <= last; i++) { helper[i] = array[i];}

[1] [2] [3]

helper 10 30 20 40

array

Page 8: Merge sort: illustrated step-by-step walk through

int left = begin;int right = middle + 1;int storeIndex = begin;

10 30 20 40

[0] [1] [2] [3]

helper 10 30 20 40

array

left

begin middle last

Page 9: Merge sort: illustrated step-by-step walk through

int left = begin;int right = middle + 1;int storeIndex = begin;

10 30 20 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

Page 10: Merge sort: illustrated step-by-step walk through

int left = begin;int right = middle + 1;int storeIndex = begin;

10 30 20 40

[0] [1] [2] [3]

helper

store Index

10 30 20 40

right

array

left

Page 11: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 30 20 40

[0] [1] [2] [3]

helper

store Index

10 30 20 40

right

array

left

0 <= 1is true

2 <= 3is true

Page 12: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 30 20 40

[0] [1] [2] [3]

helper

store Index

10 30 20 40

right

array

left

10 <= 20is true

Page 13: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 30 20 40

[0] [1] [2] [3]

helper

store Index

10 30 20 40

right

array

left

Page 14: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 30 20 40

[0] [1] [2] [3]

helper

store Index

10 30 20 40

right

array

left

Page 15: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 30 20 40

[0] [1] [2] [3]

helper

store Index

10 30 20 40

right

array

left

Page 16: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 30 20 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

1 <= 1is true

2 <= 3is truestore

Index

Page 17: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 30 20 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

30 <= 20is false

Page 18: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 20 20 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

Page 19: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 20 20 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

Page 20: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 20 20 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

Page 21: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 20 20 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

1 <= 1is true

3 <= 3is true

Page 22: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 20 20 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

30 <= 40is true

Page 23: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 20 30 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

Page 24: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 20 30 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

Page 25: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 20 30 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

Page 26: Merge sort: illustrated step-by-step walk through

while (left <= middle && right <= last) { if (helper[left] <= helper[right]) { array[storeIndex] = helper[left]; left++; } else { array[storeIndex] = helper[right]; right++; }

storeIndex++;}

10 20 30 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

2 <= 1is false

3 <= 3is true

Exit while loop

Page 27: Merge sort: illustrated step-by-step walk through

int remainder = middle - left;for (int i = 0; i <= remainder; i++) { array[storeIndex + i] = helper[left + i];}

10 20 30 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

-1

remainder

1 - 2 = -1

Page 28: Merge sort: illustrated step-by-step walk through

int remainder = middle - left;for (int i = 0; i <= remainder; i++) { array[storeIndex + i] = helper[left + i];}

10 20 30 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

-1

remainder

0 <= -1is false

Skip over for loop

Page 29: Merge sort: illustrated step-by-step walk through

int remainder = middle - right;for (int i = 0; i <= remainder ; i++) { array[storeIndex + i] = helper[ right + i];}

10 20 30 40

[0] [1] [2] [3]

helper 10 30 20 40

right

array

left

store Index

Not needed

Already there because right sub-array already occupies tail end of the target array

Page 30: Merge sort: illustrated step-by-step walk through

Merge Sort Algorithm

Now we use Merge function

Page 31: Merge sort: illustrated step-by-step walk through

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);

begin last

5 7 3 2

[0] [1] [2] [3]

Page 32: Merge sort: illustrated step-by-step walk through

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);

0 >= 3is false

begin last

5 7 3 2

[0] [1] [2] [3]

Page 33: Merge sort: illustrated step-by-step walk through

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);

begin last

5 7 3 2

[0] [1] [2] [3]

middle

Page 34: Merge sort: illustrated step-by-step walk through

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);

begin last

5 7 3 2

[0] [1] [2] [3]

middle

Call Stack #0

Merge & Sort the left sub-array first

Page 35: Merge sort: illustrated step-by-step walk through

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);

begin last

5 7 3 2

[0] [1] [2] [3]

Call Stack #0

Call Stack #1

Page 36: Merge sort: illustrated step-by-step walk through

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);

begin last

5 7 3 2

[0] [1] [2] [3]

Call Stack #0

Call Stack #1

0 >= 1is false

Page 37: Merge sort: illustrated step-by-step walk through

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);

begin last

5 7 3 2

[0] [1] [2] [3]

Call Stack #0

Call Stack #1

middle

Page 38: Merge sort: illustrated step-by-step walk through

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);

begin last

5 7 3 2

[0] [1] [2] [3]

Call Stack #0

Call Stack #1

middle

Page 39: Merge sort: illustrated step-by-step walk through

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);

beginlast

5 7 3 2

[0] [1] [2] [3]

Call Stack #0

Call Stack #1

Call Stack #3

Page 40: Merge sort: illustrated step-by-step walk through

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);

beginlast

5 7 3 2

[0] [1] [2] [3]

Call Stack #0

Call Stack #1

Call Stack #30 >=0is true

Page 41: Merge sort: illustrated step-by-step walk through

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);

beginlast

5 7 3 2

[0] [1] [2] [3]

Call Stack #0

Call Stack #1

Call Stack #30 >=0is true

Base condition is met!

Page 42: Merge sort: illustrated step-by-step walk through

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);

3 2

[2] [3]

Call Stack #0

Call Stack #1

begin last

5 7

[0] [1]

middle

Page 43: Merge sort: illustrated step-by-step walk through

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);

3 2

[2] [3]

Call Stack #0

Call Stack #1

5 7

[0] [1]

beginlast

Call Stack #31 >=1is true

Page 44: Merge sort: illustrated step-by-step walk through

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);

3 2

[2] [3]

Call Stack #0

Call Stack #1

5 7

[0] [1]

beginlast

Call Stack #3

Base condition is met!

Page 45: Merge sort: illustrated step-by-step walk through

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);

begin last

5 7 3 2

[0] [1] [2] [3]

Call Stack #0

Call Stack #1

middle

Merge

Page 46: Merge sort: illustrated step-by-step walk through

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)

begin last

5 7 3 2

[0] [1] [2] [3]

Call Stack #0

Call Stack #1

middle

Already sorted (unchanged but sorted)

Page 47: Merge sort: illustrated step-by-step walk through

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);

begin last

5 7 3 2

[0] [1] [2] [3]

middle

Call Stack #0

Merge & Sort the right sub-array next

Page 48: Merge sort: illustrated step-by-step walk through

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);

begin last

5 7 3 2

[0] [1] [2] [3]

Call Stack #0

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