SlideShare a Scribd company logo
1 of 48
Download to read offline
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);

More Related Content

What's hot

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
 
Algebra 6
Algebra 6Algebra 6
Algebra 6Mang Oha
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 

What's hot (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
 

Viewers also liked

Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained Mohit Tare
 
Presentation-Merge Sort
Presentation-Merge SortPresentation-Merge Sort
Presentation-Merge SortMd Showrov Ahmed
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 
Mergesort
MergesortMergesort
Mergesortluzenith_g
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sortPreetham Devisetty
 
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 applicationsyazad dumasia
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sortShakila Mahjabin
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithmShubham Dwivedi
 
Merge sort
Merge sortMerge sort
Merge sortKumar
 
Implementing Merge Sort
Implementing Merge SortImplementing Merge Sort
Implementing Merge Sortsmita gupta
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort AlgorithmGail Carmichael
 
Heap sort
Heap sortHeap sort
Heap sortkaushiklv
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Heap sort
Heap sortHeap sort
Heap sortMohd Arif
 

Viewers also liked (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 to 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
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques JayeshGadhave1
 
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
 
Ds sorting
Ds sortingDs sorting
Ds sortingHamza Khan
 

Similar to 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
 

More from 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
 
Git ăƒȘăƒ™ăƒŒă‚č
Git ăƒȘăƒ™ăƒŒă‚čGit ăƒȘăƒ™ăƒŒă‚č
Git ăƒȘăƒ™ăƒŒă‚čYoshi 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
 

More from 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
 

Recently uploaded

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
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
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
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
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
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
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"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
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo GarcĂ­a Lavilla
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"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
 
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
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 

Recently uploaded (20)

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic 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
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
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?
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
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!
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
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
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"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
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"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
 
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
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 

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