SlideShare una empresa de Scribd logo
1 de 48
Descargar para leer sin conexión
Merge Sort algorithm
Illustrated walkthrough
Reference
“Cracking the coding interview” Fifth edition.
Gayle Laakmann McDowell, 2008 - 2013
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
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)
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}
[0]
array

[1]

[2]

[3]

10

30

20

40

helper

for (int i = begin; i <= last; i++) {
helper[i] = array[i];
}
[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];
}
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;
[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;
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;
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
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];
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];
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];
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];
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
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];
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];
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];
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];
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
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];
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];
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];
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];
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];
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];
}
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
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
Merge Sort Algorithm
Now we use Merge function
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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)
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);
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);

Más contenido relacionado

La actualidad más candente

Maths vegas functions_review
Maths vegas functions_reviewMaths vegas functions_review
Maths vegas functions_reviewJJkedst
 
Program Language - Fall 2013
Program Language - Fall 2013 Program Language - Fall 2013
Program Language - Fall 2013 Yun-Yan Chi
 
ゲーム理論BASIC 演習5 -カーネルを求める-
ゲーム理論BASIC 演習5 -カーネルを求める-ゲーム理論BASIC 演習5 -カーネルを求める-
ゲーム理論BASIC 演習5 -カーネルを求める-ssusere0a682
 
ゲーム理論BASIC 演習4 -交渉集合を求める-
ゲーム理論BASIC 演習4 -交渉集合を求める-ゲーム理論BASIC 演習4 -交渉集合を求める-
ゲーム理論BASIC 演習4 -交渉集合を求める-ssusere0a682
 
23 order of operations
23 order of operations23 order of operations
23 order of operationsalg1testreview
 
The Ring programming language version 1.5.3 book - Part 69 of 184
The Ring programming language version 1.5.3 book - Part 69 of 184The Ring programming language version 1.5.3 book - Part 69 of 184
The Ring programming language version 1.5.3 book - Part 69 of 184Mahmoud Samir Fayed
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Kevin Munc
 
ゲーム理論BASIC 演習6 -仁を求める-
ゲーム理論BASIC 演習6 -仁を求める-ゲーム理論BASIC 演習6 -仁を求める-
ゲーム理論BASIC 演習6 -仁を求める-ssusere0a682
 
solucionario de purcell 3
solucionario de purcell 3solucionario de purcell 3
solucionario de purcell 3José Encalada
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmitabeasiswa
 
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202Mahmoud Samir Fayed
 
ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-
ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-
ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-ssusere0a682
 
A Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoA Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoChung Hua Universit
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 

La actualidad más candente (17)

Maths vegas functions_review
Maths vegas functions_reviewMaths vegas functions_review
Maths vegas functions_review
 
Program Language - Fall 2013
Program Language - Fall 2013 Program Language - Fall 2013
Program Language - Fall 2013
 
ゲーム理論BASIC 演習5 -カーネルを求める-
ゲーム理論BASIC 演習5 -カーネルを求める-ゲーム理論BASIC 演習5 -カーネルを求める-
ゲーム理論BASIC 演習5 -カーネルを求める-
 
ゲーム理論BASIC 演習4 -交渉集合を求める-
ゲーム理論BASIC 演習4 -交渉集合を求める-ゲーム理論BASIC 演習4 -交渉集合を求める-
ゲーム理論BASIC 演習4 -交渉集合を求める-
 
23 order of operations
23 order of operations23 order of operations
23 order of operations
 
The Ring programming language version 1.5.3 book - Part 69 of 184
The Ring programming language version 1.5.3 book - Part 69 of 184The Ring programming language version 1.5.3 book - Part 69 of 184
The Ring programming language version 1.5.3 book - Part 69 of 184
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)
 
ゲーム理論BASIC 演習6 -仁を求める-
ゲーム理論BASIC 演習6 -仁を求める-ゲーム理論BASIC 演習6 -仁を求める-
ゲーム理論BASIC 演習6 -仁を求める-
 
solucionario de purcell 3
solucionario de purcell 3solucionario de purcell 3
solucionario de purcell 3
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmita
 
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202
 
GANs
GANsGANs
GANs
 
ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-
ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-
ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-
 
Estructura Discreta I
Estructura Discreta IEstructura Discreta I
Estructura Discreta I
 
A Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoA Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter two
 
Algebra 6
Algebra 6Algebra 6
Algebra 6
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 

Destacado (20)

Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained
 
Presentation-Merge Sort
Presentation-Merge SortPresentation-Merge Sort
Presentation-Merge Sort
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Mergesort
MergesortMergesort
Mergesort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Implementing Merge Sort
Implementing Merge SortImplementing Merge Sort
Implementing Merge Sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Heap sort
Heap sortHeap sort
Heap sort
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Heap sort
Heap sort Heap sort
Heap sort
 
Heap sort
Heap sortHeap sort
Heap sort
 

Similar a Merge sort: illustrated step-by-step walk through

Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfStep 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfaloeplusint
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdfAshaWankar1
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring PatternsNaresha K
 
A Skeptics guide to functional style javascript
A Skeptics guide to functional style javascriptA Skeptics guide to functional style javascript
A Skeptics guide to functional style javascriptjonathanfmills
 
Maxima Finding problem In the 2-dimension space, we shall say that .pdf
Maxima Finding problem In the 2-dimension space, we shall say that .pdfMaxima Finding problem In the 2-dimension space, we shall say that .pdf
Maxima Finding problem In the 2-dimension space, we shall say that .pdfarrowit1
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptxYagna15
 
The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210Mahmoud Samir Fayed
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfRahul04August
 
#includeiostream #includefstreamusing namespace std; glo.pdf
#includeiostream #includefstreamusing namespace std; glo.pdf#includeiostream #includefstreamusing namespace std; glo.pdf
#includeiostream #includefstreamusing namespace std; glo.pdfkrram1989
 
The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202Mahmoud Samir Fayed
 
#include fstream#include iostream#include cstdlib#includ.docx
#include fstream#include iostream#include cstdlib#includ.docx#include fstream#include iostream#include cstdlib#includ.docx
#include fstream#include iostream#include cstdlib#includ.docxajoy21
 

Similar a Merge sort: illustrated step-by-step walk through (20)

Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfStep 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring Patterns
 
A Skeptics guide to functional style javascript
A Skeptics guide to functional style javascriptA Skeptics guide to functional style javascript
A Skeptics guide to functional style javascript
 
Maxima Finding problem In the 2-dimension space, we shall say that .pdf
Maxima Finding problem In the 2-dimension space, we shall say that .pdfMaxima Finding problem In the 2-dimension space, we shall say that .pdf
Maxima Finding problem In the 2-dimension space, we shall say that .pdf
 
Code
CodeCode
Code
 
Promise
PromisePromise
Promise
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
 
Slicing in Python - What is It?
Slicing in Python - What is It?Slicing in Python - What is It?
Slicing in Python - What is It?
 
The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
#includeiostream #includefstreamusing namespace std; glo.pdf
#includeiostream #includefstreamusing namespace std; glo.pdf#includeiostream #includefstreamusing namespace std; glo.pdf
#includeiostream #includefstreamusing namespace std; glo.pdf
 
The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184
 
The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202
 
#include fstream#include iostream#include cstdlib#includ.docx
#include fstream#include iostream#include cstdlib#includ.docx#include fstream#include iostream#include cstdlib#includ.docx
#include fstream#include iostream#include cstdlib#includ.docx
 
Ds sorting
Ds sortingDs sorting
Ds sorting
 
Python list
Python listPython list
Python list
 
6. list
6. list6. list
6. list
 

Más de Yoshi Watanabe

Create images with AI models.pptx
Create images with AI models.pptxCreate images with AI models.pptx
Create images with AI models.pptxYoshi Watanabe
 
Git コンフリクト
Git コンフリクトGit コンフリクト
Git コンフリクトYoshi Watanabe
 
Find n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthroughFind n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthroughYoshi Watanabe
 
Binary search tree exact match - illustrated walkthrough
Binary search tree   exact match - illustrated walkthroughBinary search tree   exact match - illustrated walkthrough
Binary search tree exact match - illustrated walkthroughYoshi Watanabe
 
Quicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughQuicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughYoshi Watanabe
 

Más de Yoshi Watanabe (7)

Create images with AI models.pptx
Create images with AI models.pptxCreate images with AI models.pptx
Create images with AI models.pptx
 
Git フェッチ
Git フェッチGit フェッチ
Git フェッチ
 
Git リベース
Git リベースGit リベース
Git リベース
 
Git コンフリクト
Git コンフリクトGit コンフリクト
Git コンフリクト
 
Find n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthroughFind n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthrough
 
Binary search tree exact match - illustrated walkthrough
Binary search tree   exact match - illustrated walkthroughBinary search tree   exact match - illustrated walkthrough
Binary search tree exact match - illustrated walkthrough
 
Quicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughQuicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk through
 

Último

From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Último (20)

From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

Merge sort: illustrated step-by-step walk through

  • 2. Reference “Cracking the coding interview” Fifth edition. Gayle Laakmann McDowell, 2008 - 2013
  • 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. 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. 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. [0] array [1] [2] [3] 10 30 20 40 helper for (int i = begin; i <= last; i++) { helper[i] = array[i]; }
  • 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. 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. [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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Merge Sort Algorithm Now we use Merge function
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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);