Professional Documents
Culture Documents
Merge Sort
Merge Sort
• Solution 1:
• Compare 1 with 2 0 0
02 03
• Compare 1 with 3 1 1
• :
• Compare 1 with 16
• Time taken: 0
16
• 15 comparisons (worst case) 1
Simple Example
• Given 16 balls with one defective (say lighter)
• Identify the defective ball.
0
1 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16
• Solution 2:
• Compare 1 with 2 0 0
02 04
1 3
• Compare 3 with 4
• :
• Compare 15 with 16 k k+1
1
16
5
• Time taken:
• 8 comparisons (worst case)
Simple Example
• Given 16 balls with one defective (say lighter)
• Identify the defective ball.
0
1 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16
• Soltion3: Divide and Conquer
• Divide into 2 sets, each of 8 balls 0 0
…08 …16
• Compare 1-8 with 9-16, and divide the 1 9
lighter set into two parts each of 4.
• :
• Continue the process till lighter ball is
found
• Time taken: 4 comparisons (log216)
Divide-and-Conquer
The most-well known algorithm design strategy:
1. Divide instance of problem into two or more smaller instances of
the same problem ,ideally of same size
• The cost is
• cost of breaking
• cost of solving subproblem
• cost of combining 6
Divide and Conquer Approach
Problem of
Size n
Solution to
Subprob 1
Solution to
Subprob 2
… Solution to
Subprob k
Solution to Original
Problem of Size n
7
Divide-and-Conquer Examples
• Sorting: merge sort and quicksort
• Binary search
Type DAndC(P) {
if Small(P)
return S(P)
else {
Divide P into smaller sets P1, …,Pk, k>=1
Apply DAndC to each subproblems
return Combine(DAndC(P1)…,DAndC(Pk))
}
}
9
Mergesort Example
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
Mergesort Example
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
ALGORITHM Mergesort(A[0.. n -1])
//Sorts array A[0 .. n -1] by recursive merge sort
//Input: An array A[0 .. n -1] of orderable elements
//Output: Array A[0 .. n -1] sorted in nondecreasing order
if n > 1
copy A[0 .. ⌊n/2⌋ - 1] to B[0 .. ⌊n/2⌋ - 1]
copy A[⌊n/2⌋ .. n - 1] to C[0 .. ⌊n/2⌋ - 1]
Mergesort(B[0 .. ⌊n/2⌋ - 1])
Mergesort(C[0 .. ⌊n/2⌋ - 1])
Merge(B, C, A)
ALGORITHM Merge(B[0 .. p-1], C[0 .. q -1], A[0 .. p + q -1])
// Merges two sorted arrays into one sorted array
// Input: Arrays B[0 .. p -1] and C[0 .. q -1] both sorted
// Output: Sorted array A[0 .. p + q -1] of the elements of Band C
i <- 0; j <- 0; k <- 0
while i < p and j < q do
if B[i] ≤ C[j]
A[k] <- B[i];
i <- i + 1
else
A[k] <- C[j];
j <- j + 1
k <- k + 1
if i = p
copy C[j .. q -1] to A[k .. p + q -1]
else
copy B[i..p -1] to A[k .. p + q -1]