Lecture 2.2 Merge Sort Algorithms
Lecture 2.2 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
7
Execution Example (cont.)
7 29 4
8
Execution Example (cont.)
7 29 4
72
9
Execution Example (cont.)
7 29 4
72
77
10
Execution Example (cont.)
7 29 4
72
77 22
11
Execution Example (cont.)
Merge
7 2 9 43 8 6 1
7 29 4
722 7
77 22
12
Execution Example (cont.)
7 29 4
722 7 9 4 4 9
13
Execution Example (cont.)
Merge
7 2 9 43 8 6 1
7 29 4 2 4 7 9
722 7 9 4 4 9
14
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
15
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
16
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
17
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 loops
k= k+1
}
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;
}
}
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
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
𝒏 𝒏 =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/