Merge Sort
Merge Sort
Presentation by:
S.V.Jansi Rani
AP/CSE
SSN College of Engineering
Outline
S.V.JANSI RANI/AP/CSE/SSNCE
Divide and Conquer Method
S.V.JANSI RANI/AP/CSE/SSNCE
Divide and Conquer Method
S.V.JANSI RANI/AP/CSE/SSNCE
Algorithm DAndC(P)
{
if Small(P) then return S(P);
else
{
divide P into smaller instances P1, P2, …,Pk, K≥1;
Apply DAndC to each of these sub problems;
return Combine(DAndC(p1), DAndC(p2),… DAndC(pk));
}
}
S.V.JANSI RANI/AP/CSE/SSNCE
S.V.JANSI RANI/AP/CSE/SSNCE
MERGE SORT
S.V.JANSI RANI/AP/CSE/SSNCE
Merge Sort
Basic Strategy:
1.Divide the array A[1 .. n] into two sub arrays A[1 ..m] and A[m+1 .. n],
where m = floor(n/2)
2. Recursively mergesort the sub arrays A[1 ..m] and A[m+1 .. n].
3. Merge the newly-sorted sub arrays A[1 ..m] and A[m+1 .. n] into a
single sorted list.
S.V.JANSI RANI/AP/CSE/SSNCE
• The first step is completely trivial—we only need to compute the
median index m—and we can delegate the second step to the
Recursive sub routine
• All the real work is done in the final step; the two sorted sub arrays
A[1 ..m] and A[m+1 .. n] can be merged using a simple linear-time
algorithm.
S.V.JANSI RANI/AP/CSE/SSNCE
Example
Example
S.V.JANSI RANI/AP/CSE/SSNCE
ALGORITHM Merge(A[],l,mid,h)
ht=l, j = mid+1, i=1, k
MERGESORT(A[], l,h): while ((ht<=mid)&&(j<=h))
If (l<h) { if(A[ht]<=A[j)
mid =(l+h)/2 B[i] = A[ht]; ht++;
MERGESORT(l,mid) else
MERGESORT(mid+1,h) B[i] = A[j]; j++;
Merge(A,l,mid,h) i++;
Else
}
return
if(ht>md) /* copy remaining second
array elements
for(k=j; k<=h; k++)
B[i] =A[k]; i++;
else
for(k=ht; k<=md; k++)
B[i] =A[k]; i++;
for(k=l; k<=h; k++)
S.V.JANSI RANI/AP/CSE/SSNCE A[k] =B[k];
TRACE ?
S.V.JANSI RANI/AP/CSE/SSNCE
Complexity
• Input size (list length) to the original MergeSort call is n. Input
size to each recursive call is n
• If the running time of the original call is T(n), then the running
time each recursive call is T( n/2)
• Each call to MergeSort makes two recursive calls.
• Each call to MergeSort calls Merge. Merge scans the items of
the input list once. Therefore, the running time of Merge is n.
• Running time of a MergeSort call is
n n
T ( n) T T n
2 2
• When the input size is 1 or 0, MergeSort sorts the list trivially.
Therefore, the best case is
T(n) = 1
S.V.JANSI RANI/AP/CSE/SSNCE
Use Master’s theorm and find out the order ???????????????
S.V.JANSI RANI/AP/CSE/SSNCE
S.V.JANSI RANI/AP/CSE/SSNCE
Quick Sort
S.V.JANSI RANI/AP/CSE/SSNCE
ALGORITHM
S.V.JANSI RANI/AP/CSE/SSNCE
Example
S.V.JANSI RANI/AP/CSE/SSNCE
Time Complexity of Quick Sort
PARTITION runs in O(n) time: j - i = n at the beginning,
j - i = 0 at the end, and we do a constant amount of work each time
we increment i or decrement j.
S.V.JANSI RANI/AP/CSE/SSNCE
Worst Case: Sorted array as input
• The worst-case is when the pivot always ends up in the first or last
element. That is, partitions the array as unequally as possible.
• In this case
– T(n) T(n1) T(11) n T(n1) n
n (n1) … + 1
n(n 1)/2 (n2)
Average case : randomly ordered array of size n
• It is rather complex, but is where the algorithm earns its name.
• Assume the partition split can happen in each position s(0 <= s <=
n-1)
1 n 1
T (n) [(n T ( s ) T (n 1 s )] for n>1
n s 0
T(0) =0 , T(1) =0
T(n) ≈ 2nln n ≈ 1.38 n log n
S.V.JANSI RANI/AP/CSE/SSNCE
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 1
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 2
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 3
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 4
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 5
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 6
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 7
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 8
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 9
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 10
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 11
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 12
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 13
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 14
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 15
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 16
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 17
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 18
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 19
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 20
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 21
S.V.JANSI RANI/AP/CSE/SSNCE
MergeSort (Example) - 22
S.V.JANSI RANI/AP/CSE/SSNCE
14 23 45 98 6 33 42 67
S.V.JANSI RANI/AP/CSE/SSNCE
14 23 45 98 6 33 42 67
Merge
S.V.JANSI RANI/AP/CSE/SSNCE
14 23 45 98 6 33 42 67
Merge
S.V.JANSI RANI/AP/CSE/SSNCE
14 23 45 98 6 33 42 67
6 14
Merge
S.V.JANSI RANI/AP/CSE/SSNCE
14 23 45 98 6 33 42 67
6 14 23
Merge
S.V.JANSI RANI/AP/CSE/SSNCE
14 23 45 98 6 33 42 67
6 14 23 33
Merge
S.V.JANSI RANI/AP/CSE/SSNCE
14 23 45 98 6 33 42 67
6 14 23 33 42
Merge
S.V.JANSI RANI/AP/CSE/SSNCE
14 23 45 98 6 33 42 67
6 14 23 33 42 45
Merge
S.V.JANSI RANI/AP/CSE/SSNCE
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
Merge
S.V.JANSI RANI/AP/CSE/SSNCE
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
Merge
S.V.JANSI RANI/AP/CSE/SSNCE
S.V.JANSI RANI/AP/CSE/SSNCE
S.V.JANSI RANI/AP/CSE/SSNCE 55
Quick-Sort
• To understand quick-sort, let’s look at a high-level description of
the algorithm
1) Divide / Partition : If the sequence S has 2 or more elements,
select an element x from S to be your pivot. Any arbitrary
element, like the last, will do. Remove all the elements of S and
divide them into 3 sequences:
L, holds S’s elements less than x
E, holds S’s elements equal to x
G, holds S’s elements greater than x
2) Recurse: Recursively sort L and G
3) Conquer: Finally, to put elements back into S in order, first
inserts the elements of L, then those of E, and those of G.
Here are some pretty diagrams....
S.V.JANSI RANI/AP/CSE/SSNCE 56
Idea of Quick Sort
2) Divide/ Partition:
Rearrange elements so
that x goes to itsnal
position E
S.V.JANSI RANI/AP/CSE/SSNCE 57
Partition
S.V.JANSI RANI/AP/CSE/SSNCE 58
Partition
Divide step: l scans the sequence from the left, and r from the right.
S.V.JANSI RANI/AP/CSE/SSNCE 59
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 60
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 61
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 62
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 63
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 64
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 65
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 66
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 67
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 68
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 69
Quick-Sort Tree
S.V.JANSI RANI/AP/CSE/SSNCE 70
Quick-Sort Tree
Skipping ...
S.V.JANSI RANI/AP/CSE/SSNCE 71
... Finally
S.V.JANSI RANI/AP/CSE/SSNCE 72
S.V.JANSI RANI/AP/CSE/SSNCE
Binary Search
Presentation by:
S.V.Jansi Rani
AP/CSE
SSN College of Engineering
Searching Arrays
• Linear search
small arrays
unsorted arrays
• Binary search
large arrays
sorted arrays
S.V.JANSI RANI/AP/CSE/SSNCE
Linear Search Algorithm
Continue with next element of the array until you find a match or
reach the last element in the array.
Note: On the average you will have to compare the search key
with half the elements in the array.
S.V.JANSI RANI/AP/CSE/SSNCE
Binary Search Algorithm
May only be used on a sorted array.
Eliminates one half of the elements after each comparison.
S.V.JANSI RANI/AP/CSE/SSNCE
Binary Search Example
a
search key = 19
0 1
1 5
2 15
3
19
4
5 25
6 27
29 middle of the array
7 compare a[6] and 19
31
8 19 is smaller than 29 so the next
9 33
search will use the lower half of the array
45
10 55
11 88
12
100
S.V.JANSI RANI/AP/CSE/SSNCE
Binary Search Pass 2
a
search key = 19
0 1
1 5
2 15 use this as the middle of the array
3 Compare a[2] with 19
19
4
5 25 15 is smaller than 19 so use the top
27 half for the next pass
S.V.JANSI RANI/AP/CSE/SSNCE
Binary Search Pass 3
search key = 19
3
19
4
use this as the middle of the array
5 25
Compare a[4] with 19
27
25 is bigger than 19 so use the bottom
half
S.V.JANSI RANI/AP/CSE/SSNCE
Binary Search Pass 4
search key = 19
3
19 use this as the middle of the array
Compare a[3] with 19
Found!!
S.V.JANSI RANI/AP/CSE/SSNCE
Binary Search Example
a
search key = 18
0 1
1 5
2 15
3
19
4
5 25
6 27
7 29 middle of the array
8 compare a[6] and 18
31
9 18 is smaller than 29 so the next
10 33
search will use the lower half of the array
11 45
12 55
88
100
S.V.JANSI RANI/AP/CSE/SSNCE
Binary Search Pass 2
a
search key = 18
0 1
1 5
2 15 use this as the middle of the array
3 Compare a[2] with 18
19
4
5 25 15 is smaller than 18 so use the top
27 half for the next pass
S.V.JANSI RANI/AP/CSE/SSNCE
Binary Search Pass 3
search key = 18
3
19
4
use this as the middle of the array
5 25
Compare a[4] with 18
27
25 is bigger than 18 so use the bottom
half
S.V.JANSI RANI/AP/CSE/SSNCE
Binary Search Pass 4
search key = 18
3
19 use this as the middle of the array
Compare a[3] with 18
Does not match and no more elements
to compare.
Not Found!!
S.V.JANSI RANI/AP/CSE/SSNCE
Algorithm: Binary_Search( A, low, high, x)
Input: sorted array A[ ], low, high, key
Output: Index or 0 if element not found
if( high == low)
{ if (x == a[low]) return low ;
else return 0;
}
else
{ mid = (low+high)/2
if (x==a[mid])
return mid
else if (x<a[mid])
return Binary_Search( A,low,mid-1,x)
else
return Binary_Search( A,mid+1,high,x)
}
S.V.JANSI RANI/AP/CSE/SSNCE
Example
-15 -6 0 7 9 23 54 82 101 112 125 131 142 151
1 2 3 4 5 6 7 8 9 10 11 12 13 14
142
S.V.JANSI RANI/AP/CSE/SSNCE
Example
-15 -6 0 7 9 23 54 82 101 112 125 131 142 151
1 2 3 4 5 6 7 8 9 10 11 12 13 14
142 1 14 7
8 14 11
12 14 13 found
S.V.JANSI RANI/AP/CSE/SSNCE
Example
-15 -6 0 7 9 23 54 82 101 112 125 131 142 151
1 2 3 4 5 6 7 8 9 10 11 12 13 14
151
S.V.JANSI RANI/AP/CSE/SSNCE
Example
-15 -6 0 7 9 23 54 82 101 112 125 131 142 151
1 2 3 4 5 6 7 8 9 10 11 12 13 14
151 1 14 7
8 14 11
12 14 13
14 14 14 found
S.V.JANSI RANI/AP/CSE/SSNCE
Example
-14
S.V.JANSI RANI/AP/CSE/SSNCE
Example
-15 -6 0 7 9 23 54 82 101 112 125 131 142 151
1 2 3 4 5 6 7 8 9 10 11 12 13 14
-14 1 14 7
1 6 3
1 2 1
1 2 1
What happens
2 2 2 [Return 0] if (l==r)
terminating
2 1 1 condition is not
Algorithm there
does not halt 2 1 1
2 1 1
S.V.JANSI RANI/AP/CSE/SSNCE
Example
-15 -6 0 7 9 23 54 82 101 112 125 131 142 151
1 2 3 4 5 6 7 8 9 10 11 12 13 14
149 1 14 7
8 14 11
12 14 13
14 14 14 return 0
What happens
14 13 13 if (l==r)
terminating
14 13 13 condition is not
Algorithm there
does not halt 14 13 13
.. .. ..
S.V.JANSI RANI/AP/CSE/SSNCE
Theorem
S.V.JANSI RANI/AP/CSE/SSNCE
Time Complexity of Binary Search
Worst Case:
1. Arrays without search key
2. Search element in the first / last position
S.V.JANSI RANI/AP/CSE/SSNCE
Time Complexity of Binary Search
Worst Case:
1. Arrays without search key
2. Search element in the first / last position
T(n) = log2n +1
Average Case:
T(n) = log2n
Best Case:
T(n) =1
S.V.JANSI RANI/AP/CSE/SSNCE
Efficiency
Searching an array of 1024 elements will take at most 10
passes to find a match or determine that the element does
not exist in the array.
512, 256, 128, 64, 32, 16, 8, 4, 2, 1
S.V.JANSI RANI/AP/CSE/SSNCE
THANK YOU
THANK YOU