Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.

Similar presentations


Presentation on theme: "CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III."— Presentation transcript:

1 CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

2 2 Topics Sorting Merge Sort Quick Sort

3 3 Merge Sort - General Description Recursively divide list in half until each piece is of size one: then merge adjacent one element lists into a sorted two element list. each sorted part is merged with adjacent parts to give a larger sorted list. eventually the final two pieces are merged to give a sorted version of the original list.

4 4 Merge Sort Example: Divide array in two halves: 8 1 4 3 2 8 1 4 3 2 Sort the halves:1 4 8 2 3 Merge the halves into temporary array: 1 < 2  Put 1 into Temporary array 1 2 < 4  Put 2 into TemArr 1 2 3 < 4  Put 3 into TemArr 1 2 3 Right half finished; add remaing elements 1 2 3 4 8 Copy Temporary array back into original array 1 2 3 4 8

5 5 mergeSort -method public static void mergeSort(Comparable[ ] theArray, int first, int last) { // --------------------------------------------------------- // Sorts the items in an array into ascending order. // Calls: merge. // --------------------------------------------------------- if (first < last) { // sort each half int mid = (first + last)/2; // index of midpoint // sort left half theArray[first..mid] mergeSort(theArray, first, mid); // sort right half theArray[mid+1..last] mergeSort(theArray, mid+1, last); // merge the two halves merge(theArray, first, mid, last); } // end if } // end mergesort

6 6 merge - method private static void merge(Comparable[ ] theArray, int first, int mid, int last) { // --------------------------------------------------------- // Merges two sorted array segments theArray[first..mid] and // theArray[mid+1..last] into one sorted array. // Implementation note: This method merges the two subarrays into a temporary array // and copies the result into the original array anArray. // --------------------------------------------------------- int maxSize = theArray.length; // temporary array Comparable[ ] tempArray = new Comparable[maxSize]; // initialize the local indexes to indicate the subarrays int first1 = first; // beginning of first subarray int last1 = mid; // end of first subarray int first2 = mid + 1; // beginning of second subarray int last2 = last; // end of second subarray

7 7 merge - method (2) // while both subarrays are not empty, copy the // smaller item into the temporary array int index = first1; // next available location in tempArray while ((first1 <= last1) && (first2 <= last2)) { // Invariant: tempArray[first1..index-1] is in order if (theArray[first1].compareTo(theArray[first2])<0) { tempArray[index] = theArray[first1]; first1++; } else { tempArray[index] = theArray[first2]; first2++; } // end if index++; } // end while

8 8 merge - method (3) // finish off the nonempty subarray // finish off the first subarray, if necessary while (first1 <= last1) { // Invariant: tempArray[first1..index-1] is in order tempArray[index] = theArray[first1]; first1++; index++; } // end while // finish off the second subarray, if necessary while (first2 <= last2) { // Invariant: tempArray[first1..index-1] is in order tempArray[index] = theArray[first2]; first2++; index++; } // end while // copy the result back into the original array for (index = first; index <= last; ++index) { theArray[index] = tempArray[index]; } // end for } // end merge

9 9 mergeSort -driver public static void main(String[ ] args) { Integer[ ] values = new Integer[10]; values[0] = new Integer(9); values[1] = new Integer(15); values[2] = new Integer(13); values[3] = new Integer(20); values[4] = new Integer(5); values[5] = new Integer(0); values[6] = new Integer(7); values[7] = new Integer(10); values[8] = new Integer(3); values[9] = new Integer(2); mergeSort(values, 0, values.length -1); }

10 10 Merge Sort Analysis: O(n * log 2 n) Self study

11 11 Quicksort Overview A recursive sorting algorithm which divides an array into two smaller partitions. All small elements in one partition and large elements in another Calls itself recursively with each partition - a divide and conquer algorithm Requires use of an array since random access is necessary.

12 12 Quicksort Partitioning Scheme Choose the median element in the array as the "pivot" element (ideally). All elements smaller than the pivot go into the part of the array below the pivot. All elements larger than the pivot go into the part of the array above the pivot.

13 13 Quicksort Choosing the "pivot": Finding the true median element is more time consuming than quicksort itself so - Choose (guess) the pivot element quickly. Commonly used methods are to choose the first or the middle element as pivot. More on pivot choosing in the analysis section.

14 14 Quick Sort Choosing the pivot: < PP  P FPivot Index L S1 S2 P< P  P ? F Pivot L S1S2Unknown LastS1 FirstUnknown

15 15 Quick Sort Swapping elements:

16 16 Quick Sort Example: Original array27 38 12 39 27 16 27 38 12 39 27 16 27 12 38 39 27 16 27 12 16 39 27 38 First partition12 16 27 39 27 38 Pivot Unknown S2 S1 S2 Unknown Swap 16 & 38 Swap Pivot & 16

17 17 Quicksort Method public static void quicksort (Comparable[] theArray, int first, int last) { // --------------------------------------------------------- // sorting the items in an array into ascending order // Precondition: theArray[first..last] is an array; first <= last. // Postcondition: theArray[first..last] is an sorted array. // --------------------------------------------------------- int pivotIndex; if (first<last) { pivotIndex = partition (theArray, first, last); // sort region S1 and S2 quicksort(theArray, first, pivotIndex-1); quicksort(theArray, pivotIndex+1, last); } // end of if } //end quicksort

18 18 Partition Method private static int partition(Comparable[] theArray, int first, int last) { // --------------------------------------------------------- // Partitions an array for quicksort. // Precondition: theArray[first..last] is an array; first <= last. // Postcondition: Returns the index of the pivot element of theArray [first..last]. // Upon completion of the method, this will be the index value lastS1 such that // S1 = theArray[first..lastS1-1] < pivot // theArray[lastS1] == pivot // S2 = theArray[lastS1+1..last] >= pivot // --------------------------------------------------------- // tempItem is used to swap elements in the array Comparable tempItem; // place pivot in theArray[first] // choosePivot(theArray, first, last); Comparable pivot = theArray[first]; // reference pivot // initially, everything but pivot is in unknown int lastS1 = first; // index of last item in S1

19 19 Partition Method (2) // move one item at a time until unknown region is empty for (int firstUnknown = first + 1; firstUnknown <= last; ++firstUnknown) { // move item from unknown to proper region if (theArray[firstUnknown].compareTo(pivot) < 0) { // item from unknown belongs in S1 ++lastS1; tempItem = theArray[firstUnknown]; theArray[firstUnknown] = theArray[lastS1]; theArray[lastS1] = tempItem; } // end if // else item from unknown belongs in S2 } // end for // place pivot in proper position and mark its location tempItem = theArray[first]; theArray[first] = theArray[lastS1]; theArray[lastS1] = tempItem; return lastS1; } // end partition

20 20 Quicksort Analysis Best Case Assumptions: All array elements are in random order. We can choose a perfect median value. thus the array is cut exactly in half on each recursive call. Partition takes O(n) time since the loop runs from the second to the last element. for (int firstUnknown = first + 1; firstUnknown <= last; ++firstUnknown)

21 21 Quicksort Analysis (2) Determine the total number of calls to quickSort. A call to quicksort with an array partition size (n) of 1 (first == last) generates no further recursive calls. Each call to quicksort cuts the array size in half so partition deals with 1/2 the previous number of elements. n = 8 n = 4 n = 2 n = 1 n = 2

22 22 Quicksort Analysis (3) Find the growth rate function f(n): On each level of this tree the partition call(s) deal with a sum total of n elements. So the question becomes: how many levels are there in the tree? Since the number of elements is cut in half on each level there are log 2 n + 1 levels. So f(n) = n ( log 2 n + 1 ) = n log 2 n + n The time complexity of quickSort is O(nlog 2 n)

23 23 Quicksort Analysis (4) Worst case: ?

24 24 Quicksort Analysis (4) Worst case: data is already sorted. the first (rather than middle) element is chosen as the pivot. only one element (the first) gets partitioned off each call to partition has to deal with one less element and there are n recursive calls f(n) = n + (n-1) + (n-2) + (n-3) +...+ 1 = n(n+1)/2 = n 2 /2 + n/2 The time complexity becomes O(n 2 ) which is the same as bubblesort or insertion sort.

25 25 Quicksort Analysis (4) Worst case: Insertion sort has a time complexity which approaches O(n) for nearly sorted (or small arrays) while quicksort approaches O(n 2 ) Thus, insertion sort is better than quicksort for small or nearly sorted arrays.

26 26 Review Each merge step of the mergesort requires ______ major operations. 3 * n – 1 4 * n – 1 (n – 1)/2 n – 1

27 27 Review The quicksort is ______ in the worst case. O(n 2 ) O(n 3 ) O(n * log 2 n) O(log 2 n)

28 28 Review A bubble sort requires at most ______ passes to sort an array of n items. n/2 n – 2 n – 1 n

29 29 Review Assuming a linked list of n nodes, the code fragment: Node curr = head; while (curr != null) { System.out.println(curr.getItem()); curr.setNext(curr.getNext()); } // end while requires ______ write operations. n n – 1 n + 1 1

30 30 Review An algorithm’s execution time is related to the number of ______ it requires. parameters test data sets data fields operations


Download ppt "CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III."

Similar presentations


Ads by Google