# day12 - COP 3503 Computer Science II CLASS NOTES DAY#12...

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

COP 3503 – Computer Science II CLASS NOTES - DAY #12 Mergesort The mergesort sorting algorithm uses the divide and conquer strategy in which the original problem is split into two half-size, recursively solved problems. If the overhead of the base case was linear [O(N)] then the overall running time of the algorithm was O(N log 2 N). The mergesort is such an algorithm and is commonly employed for external sorting. The mergesort algorithm is a recursive, subquadratic algorithm as follows: 1. if the number of items to sort is 0 or 1, return. 2. recursively sort the first and second halves separately. 3. merge the two sorted halves into a single sorted group. Since this algorithm uses the divide and conquer strategy and employs the halving principle, we know that the sorting is done in O(log 2 N) and thus we need only to show that merging two sorted groups into a single sorted group can be performed in linear time to prove the running time is O(N log 2 N). A linear merge algorithm A linear merge algorithm requires three separate arrays A, B, and C (two input and one output) plus an index counter per array (actr, bctr, and cctr). The index counters are initially set to the first position in each of their arrays with incrementation as follows: if A[actr] < B[bctr] C[cctr] = A[actr]; cctr++; actr++; } else { C[cctr] = B[bctr]; cctr++; bctr++; } Day 12 - 1

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

View Full Document
Example: Linear Merge A B C actr bctr cctr Day 12 - 2 1 3 4 7 2 5 9 11 1 3 4 7 2 5 9 11 1 1 3 4 7 2 5 9 11 1 2 1 3 4 7 2 5 9 11 1 2 3 1 3 4 7 2 5 9 11 1 2 3 4 1 3 4 7 2 5 9 11 1 2 3 4 5 1 3 4 7 2 5 9 11 1 2 3 4 5 7 1 3 4 7 2 5 9 11 1 2 3 4 5 7 9 1 3 4 7 2 5 9 11 1 2 3 4 5 7 9 11
The mergesort is not often used as an internal sorting method. The reason is that the output array represents a linear increase in the memory requirements and additional work is required to copy the components of the arrays. Quicksort

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

day12 - COP 3503 Computer Science II CLASS NOTES DAY#12...

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

View Full Document
Ask a homework question - tutors are online