{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# sorting2 - Merge Sort Uses divide-and conquer methodology...

This preview shows pages 1–5. Sign up to view the full content.

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 1

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

View Full Document
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); } } 2
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 3

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

View Full Document
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;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 10

sorting2 - Merge Sort Uses divide-and conquer methodology...

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

View Full Document
Ask a homework question - tutors are online