Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 1 An Introduction to Sorting.

Similar presentations


Presentation on theme: "Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 1 An Introduction to Sorting."— Presentation transcript:

1 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 1 An Introduction to Sorting

2 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 2 Mergesort A recursive divide-and-conquer algorithm A merge is a common data processing operation that is performed on two sequences of data with the following characteristics –Both sequences contain items with a common compareTo method –The objects in both sequences are ordered in accordance with this compareTo method

3 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 3 Merge Algorithm 1.Access the first item from both sequences 2.While not finished with either sequence Compare the current items from the two sequences, copy the smaller current item to the output sequence, and access the next item from the input sequence whose item was copied 3.Copy any remaining items from the first sequence to the output sequence 4.Copy any remaining items from the second sequence to the output sequence

4 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 4 Analysis of Merge For two input sequences that contain a total of n elements, we need to move each element’s input sequence to its output sequence –Merge time is O(n) We need to be able to store both initial sequences and the output sequence –The array cannot be merged in place –Additional space usage is O(n) The number of splitting operations is O(log n) The number of merge operations is O(n) The total number of operations is O(n log n) As we do recursion a copy of local variables is stored on the run-time stack. Thus MergeSort requires an additional storage of size n

5 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 5 Algorithm Conceptually, merge sort works as follows: 1.Divide the unsorted list into two sublists of about half the size 2.Sort each of the two sublists recursively until we have list sizes of length 1, in which case the list itself is returned 3.Merge the two sorted sublists back into one sorted list. Mergesort incorporates two main ideas to improve its runtime: 1.A small list will take fewer steps to sort than a large list. 2.Fewer steps are required to construct a sorted list from two sorted lists than two unsorted lists. For example, you only have to traverse each list once if they're already sorted

6 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 6

7 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 7 Pseudocode function mergesort(m) var list left, right, result if length(m) ≤ 1 return m else middle = length(m) / 2 for each x in m up to middle add x to left for each x in m after middle add x to right left = mergesort(left) right = mergesort(right) result = merge(left, right) return result

8 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 8 function merge(left,right) var list result while length(left) > 0 and length(right) > 0 if first(left) ≤ first(right) append first(left) to result left = rest(left) else append first(right) to result right = rest(right) if length(left) > 0 append left to result if length(right) > 0 append right to result return result

9 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 9 MergeSort in Java public static void mergeSort (Comparable[] a) { if (a.length == 1) // base case return; else { // split array into two halves Comparable[] left = new // left half Comparable[a.length/2]; System.arraycopy(a,0,left,0,left.length) Comparable[] right = new // right half Comparable[a.length-left.length]; System.arraycopy(a, left.length, right, 0, right.length); // sort both halves of the array mergeSort(left); mergeSort(right); // Merge the results back together merge(left, right, a); }

10 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 10 private static void merge(Comparable[] left, Comparable[] right, Comparable[] whole) { int leftIx = 0, rightIx = 0, wholeIx = 0; // while both arrays still have elements while (leftIx < left.length && rightIx < right.length) { if(left[leftIndex].compareTo( right[rightIndex]) < 0) { whole[wholeIndex] = left[leftIndex]; leftIndex++; } else { whole[wholeIndex] = right[rightIndex]; rightIndex++; } wholeIndex++; }

11 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 11 // when one array has been used up… Comparable[] rest; int restIx; if (leftIx >= left.length) { // left used up rest = right; restIx = rightIx; } else { // right used up rest = left; restIx = leftIx; } // copy remainder for (int i=restIx; i<rest.length; i++) { whole[wholeIx] = rest[i]; wholeIx++; }


Download ppt "Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting II 1 An Introduction to Sorting."

Similar presentations


Ads by Google