|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Merge Sort: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- Assume that procedure Merge1(a, i, j, k) takes two sorted arrays a[i..j] and a[j+1..k] as an input and puts their merged output in the array a[i..k]. That is, in the same locations. Pseudocode of the merge sort is given below.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Algorithm Merge-Sort (a, p, q) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Step 1: |
|
if (p < q) { |
|
|
|
|
|
|
|
|
|
Step 2: |
|
|
mid = (p+q)/2 ; |
|
|
|
|
|
|
|
|
|
|
Step 3: |
|
|
|
Merge-Sort(a, p, mid) ; |
|
|
|
|
|
|
|
|
|
Step 4: |
|
|
|
|
Merge-Sort(a, mid+1, q) ; |
|
|
|
|
|
|
|
|
Step 5: |
|
|
|
|
|
Merge(a,p,mid, q) ; |
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- The above procedure sorts the elements in the sub array a[p..q].
- To sort given any a[n] invoke the algorithm with parameters (a, 0, n -1).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Prev |
|
|