sort2 - In C: Merge Sort Uses divide-and conquer...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Merge Sort Uses divide-and –conquer methodology. Main idea: Divide the array into roughly equal sized arrays and sort them separately (it is much easier to sort short lists than long ones). Then merge the two sorted arrays in order to get one sorted array. Trace with: 59 27 80 35 13 75 2 In C: void MergeSort(int List[], int start, int end) { int mid; if (start < end) { mid = (start+end)/2; MergeSort(List, start, mid); MergeSort(List, mid+1, end); Merge(List, start, mid+1, end); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3 Merging Two Sorted Lists Illustration of merging two sorted sublists in one array: List1 List2 27 31 59 80 85 13 17 35 60 75 start mid+1 end Local Array Sorted Array 4 void Merge(int List[], int start1,int start2, int end2) { int hold[maxN]; int index, length, count1, count2, total; length = start2 – start1; /* copy values in first half into local array */ for (index = 0; index < length; index++) hold[index] = List[start1 + index]; /* Counters keep track of the current elements * in each of the two sublists to merge */ count1 = 0; count2 = start2; total = start1; /* Loop until all values in one of the sublists
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 5

sort2 - In C: Merge Sort Uses divide-and conquer...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online