Lecture 2.1 Merge Sort Algorithms
Lecture 2.1 Merge Sort Algorithms
Algorithms
Course Code: CSC2211 Course Title: Algorithms
1. Sorting Algorithms
Merge Sort
Divide and Conquer
Recursive in structure
Partition
7 2 9 43 8 6 1
6
Execution Example (cont.)
7 29 4
7
Execution Example (cont.)
7 29 4
72
8
Execution Example (cont.)
7 29 4
72
77
9
Execution Example (cont.)
7 29 4
72
77 22
10
Execution Example (cont.)
Merge
7 2 9 43 8 6 1
7 29 4
722 7
77 22
11
Execution Example (cont.)
7 29 4
722 7 9 4 4 9
12
Execution Example (cont.)
Merge
7 2 9 43 8 6 1
7 29 4 2 4 7 9
722 7 9 4 4 9
13
Execution Example (cont.)
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
14
Execution Example (cont.)
Merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
15
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
16
Merge Sort Algorithm
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
Merge Sort Algorithm – Simulation understanding in Lab
Merge Sort Algorithm
Merge(L,R,A) {
nL = length(L)
Mergesort(A) nR = length(R)
{ i= j = k = 0
n = length(A) while(i< nL && j< nR) {
if(n<2) if(L[i] <= R[j]) {
return A[k] = L[i]
mid = n/2 i= i+1
L = array of size(mid) }
R = array of size(n-mid) else {
for(i=0; i < mid; i++) A[k] = R[j]
L[i] = A[i] j= j+1
for(i=mid; i < n; i++) }
R[i-mid] = A[i] k= k+1
Mergesort(L) }
Mergesort(R) while( i<nL) {
Merge(L , R , A) A[k] = L[i]; i=i+1;
} k=k+1;
}
while (j<nR) {
A[k] = R[j]; j=j+1;
k=k+1;
}
}
Merge(L,R,A) {
nL = length(L)
nR = length(R)
i= j = k = 0
while(i< nL && j< nR) {
if(L[i] <= R[j]) {
A[k] = L[i] n*c0+ n*c1+n*c2+ c4
}
i= i+1
=n*(c0+c1+c2)+ c4
else { =n*c3 + c4
A[k] = R[j]
j= j+1
=n (Linear time)
} Because of having no nested
k= k+1
} loops
while( i<nL) {
A[k] = L[i]; i=i+1;
k=k+1;
}
while (j<nR) {
A[k] = R[j]; j=j+1;
k=k+1;
}
}
nL = length(L)
nR = length(R)
i= j = k = 0
i=0 j=0
L 14 23 45 98 R 6 33 42 67
while(i< nL && j< nR) {
If(L[i] <= R[j]) {
A[k] = L[i]
i= i+1
}
else {
A[k] = R[j]
j= j+1
}
k= k+1
}
i=0 j=0
L 14 23 45 98 R 6 33 42 67
A
while(i< nL && j< nR) {
If(L[i] <= R[j]) {
A[k] = L[i]
i= i+1
}
else {
A[k] = R[j]
j= j+1
}
k= k+1
}
i=0 j=1
L 14 23 45 98 R 6 33 42 67
A 6
k=0
while(i< nL && j< nR) {
If(L[i] <= R[j]) {
A[k] = L[i]
i= i+1
}
else {
A[k] = R[j]
j= j+1
}
k= k+1
}
i=1 j=1
L 14 23 45 98 R 6 33 42 67
A 6 14
k=1
while(i< nL && j< nR) {
If(L[i] <= R[j]) {
A[k] = L[i]
i= i+1
}
else {
A[k] = R[j]
j= j+1
}
k= k+1
}
i=2 j=1
L 14 23 45 98 R 6 33 42 67
A 6 14 23
k=2
while(i< nL && j< nR) {
If(L[i] <= R[j]) {
A[k] = L[i]
i= i+1;
}
else {
A[k] = R[j]
j= j+1
}
k= k+1
}
i=2 j=2
L 14 23 45 98 R 6 33 42 67
A 6 14 23 33
k=3
while(i< nL && j< nR) {
If(L[i] <= R[j]) {
A[k] = L[i]
i= i+1
}
else {
A[k] = R[j]
j= j+1
}
k= k+1
}
i=2 j=3
L 14 23 45 98 R 6 33 42 67
A 6 14 23 33 42
k=4
while(i< nL && j< nR) {
If(L[i] <= R[j]) {
A[k] = L[i]
i= i+1
}
else {
A[k] = R[j]
j= j+1
}
k= k+1
}
i=3 j=3
L 14 23 45 98 R 6 33 42 67
A 6 14 23 33 42 45
k=5
while(i< nL && j< nR) {
If(L[i] <= R[j]) {
A[k] = L[i]
i= i+1
}
else {
A[k] = R[j]
j= j+1
}
k= k+1
}
i=3 j=4
L 14 23 45 98 R 6 33 42 67
A 6 14 23 33 42 45 67
k=6
while(i< nL && j< nR) {
If(L[i] <= R[j]) {
A[k] = L[i]
i= i+1
}
else {
A[k] = R[j]
j= j+1
}
k= k+1
}
i=3 j=4
L 14 23 45 98 R 6 33 42 67
A 6 14 23 33 42 45 67
k=6
while( i<nL) {
A[k] = L[i];
i=i+1;
k=k+1;
}
while (j<nR) {
A[k] = R[i];
j=j+1;
k=k+1;
}
i=3 j=4
L 14 23 45 98 R 6 33 42 67
A 6 14 23 33 42 45 67
k=6
while( i<nL) {
A[k] = L[i];
i=i+1;
k=k+1;
}
while (j<nR) {
A[k] = R[i];
j=j+1;
k=k+1;
}
i=4 j=4
L 14 23 45 98 R 6 33 42 67
A 6 14 23 33 42 45 67 98
k=7
Exercise
98 23 45 14 6 67 33 42
Merge Sort Analysis for Time Complexity
Mergesort(L) T(n/2)
Mergesort(R) T(n/2)
Merge(L , R , A) c3.n+c4
}
[= n]
Merge Sort Analysis
Recursion Tree Method
Recursion-tree Method
cn
Cost of divide
and merge.
cn/2 cn/2
T(n/2) T(n/2)
T(n/4) T(n/4) T(n/4) T(n/4)
Cost of sorting
subproblems.
Recursion Tree for
Merge Sort
Continue expanding until the problem size reduces to 1.
cn •Each level has total cost cn.
𝒏 𝒏 =c.n
𝟐 𝟐
𝒏 𝒏 𝒏 𝒏
𝟒 𝟒 𝟒 𝟒 =c.n
𝒏 𝒏 𝒏 𝒏 𝒏 𝒏 𝒏 𝒏
𝟖 𝟖 𝟖 𝟖 𝟖 𝟖 𝟖 𝟖 =c.n
Recursion Tree for
Merge Sort
n=8
𝒏 𝒏
c.n
𝟐 𝟐
𝒏 𝒏 𝒏 𝒏
=c.n
𝟒 𝟒 𝟒 𝟒
𝟖 𝟖 𝟖 𝟖 𝟖 𝟖 𝟖 𝟖
=1 =1 =1 =1 =1 =1 =1 =1
𝟖 𝟖 𝟖 𝟖 𝟖 𝟖 𝟖 𝟖 =c.n
Þ and
Examples:
Examples:
Best Case Time Complexity: 𝛀(n*log n)
Worst Case Time Complexity: O(n*log n)
Average Time Complexity: ϴ(n*log n)
https://www.google.com/search?q=bubble+sort+step+by+step&sxsrf=AL
eKk01uxzgfT3Oy6k1Q3WxVnSpiIN8_4g:1587999728942&tbm=isch&sourc
e=iu&ictx=1&fir=vRwFsGwVfJ6pJM%253A%252CSzhhze6MPQr4cM%252C
_&vet=1&usg=AI4_-kSrEEXqwRL-PkHhVUtn7jNfF9dB6g&sa=X&ved=2ahU
KEwje0Pz974jpAhXRAnIKHWhMD2UQ_h0wAXoECAcQBg#imgrc=EN4Sdu7
veOWVoM&imgdii=eOqvCu85p9-eBM
https://www.interviewcake.com/concept/java/counting-sort
https://www.geeksforgeeks.org/counting-sort/
https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/tut
orial/