day11 - COP 3503 Computer Science II CLASS NOTES - DAY #11...

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

View Full Document Right Arrow Icon
COP 3503 – Computer Science II CLASS NOTES - DAY #11 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 11 - 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Example: Linear Merge A B C actr bctr cctr Day 11 - 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
Background image of page 2
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 With an average running time of O(N log 2 N), quicksort is the fastest-known sorting algorithm. Quicksort has a worst case running time of O(N 2 ) which can be made statistically impossible to achieve. Quicksort is a recursive algorithm whose performance is easy to prove yet has a tricky implementation since slight variations in the code can make significant differences in the running time. The quicksort algorithm is as follows: 1. Call Quicksort(A). 2. if the number of elements to be sorted is 0 or 1, return. 3. pick any element v in the array A. (this is the pivot element) 4. partition A- {v} into two disjoint groups: Left = {x A-{v}| x v} and Right = {x A-{v}| x v}. 5. Return the result of Quicksort(Left), followed by v , followed by Quicksort(Right). Notes: base case includes possibility that the number of elements is 0 since the recursive calls may generate empty subsets. Any element can theoretically be the pivot element, although in reality the pivot element is not randomly chosen. The partitioning must be performed in place so that no additional arrays are required. Quicksort outperforms mergesort because the time
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

day11 - COP 3503 Computer Science II CLASS NOTES - DAY #11...

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

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