160 CHAPTER b} DIVIDE-AND-coy,
Sf ee Qh,
nt high) i an
eS MergeSort (int low, }
atlow J ign) gis s atouel O=787 to be sorted.
// Smal1(P) is true if there is only one element to
// sort. In this case the list is already sorted,
If there are more than one
subproblems- el
to split the set.
if Cow < high) { //
|
int mid = (low + high) /2; |
// Divide P into
Sep,
// Solve the subproblems.
MergeSort (low, mid);
MergeSort (mid + 1, high);
// Combine the solutions.
Merge (low, mid, high);
} \
Program 3.8 Merge sort3.5. MERGE SORT
161
y + wr
void Merge(int low, int mid, int high)
// allow:high] is a global array containing two sorted
// subsets in allow:mid] and in a(mid+1:high]. The goal
// is to merge these two sets into a single set residing
tf in allow:high]. b{] is an auxiliary global array.
int h = low, i = low, j = mid+t, k;
while ((h <= mid) g& (j <= high)) {
if (afb) <= alj}) { bla) = afn); net) 3
else { bli] = alj]; j+#; } i++;
a
if (h > mid) for (k=j; k TU) = 490%) + DLEw =
aa
Tn) = £0) 4 hse Z
Keep My as St <
Rif Baden witha
The rays Rew 7 2
Tl) = +
T= Fa ekew =
T= a ot+ ker Wee
ME os el
ee | Re ee
vs yee 5 Rly - 0
si ") a(n) 4 dence
"\ NG w O(a ‘i
bv
2
pa&erbrersrddod
aa