1 / 20

Workshop for CS-AP Teachers

Workshop for CS-AP Teachers. Sorting and "Big Oh" Barb Ericson July 2006. Learning Goals. Understand several sorting algorithms A: Selection Sort, Insertion Sort, Merge Sort AB: Also Quicksort Understand how to evaluate the efficiency of an algorithm

grazia
Download Presentation

Workshop for CS-AP Teachers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Workshop for CS-AP Teachers Sorting and "Big Oh" Barb Ericson July 2006 Georgia Institute of Technology

  2. Learning Goals • Understand several sorting algorithms • A: Selection Sort, Insertion Sort, Merge Sort • AB: Also Quicksort • Understand how to evaluate the efficiency of an algorithm • A: Best case, worst case, average case, and a general idea of which is faster • AB: Also "Big Oh" Georgia Institute of Technology

  3. Sorting • We often want to put data into some order • Ascending or descending • Sorting is often shown with array data • But you can sort anything that implements the Comparable Interface • Sort Pictures by size • So that the SlideShow class shows the smallest first • Sort Sounds by length • So that a PlayList plays the shortest sound first Georgia Institute of Technology

  4. Selection Sort Algorithm • Search the entire array for the smallest element and then swap the smallest with the first element (at index 0) • Continue through the rest of the array doing the same thing (second time with index 1) • This uses a nested loop • The outer loop runs from i = 0 to i < a.length – 1 • Inner loop runs from j = i+1 to j < a.length Georgia Institute of Technology

  5. public void selectionSort() { int maxCompare = a.length - 1; int smallestIndex = 0; // loop from 0 to one before last item for (int i = 0; i < maxCompare; i++) { // set smallest index to the one at i smallestIndex = i; // loop from i+1 to end of the array for (int j = i + 1; j < a.length; j++) { if (a[j] < a[smallestIndex]) { smallestIndex = j; } } // swap the one at i with the one at smallest index swap(i,smallestIndex); this.printArray("after loop body when i = " + i); } } private void swap(int i, int j) { if (i != j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } private void printArray(String message) { System.out.println(message); for (int i : a) System.out.print(i + " "); System.out.println(); } Selection Sort Code Georgia Institute of Technology

  6. Running Selection Sort Before sort 23 14 1 89 68 32 6 after loop body when i = 0 1 14 23 89 68 32 6 after loop body when i = 1 1 6 23 89 68 32 14 after loop body when i = 2 1 6 14 89 68 32 23 after loop body when i = 3 1 6 14 23 68 32 89 after loop body when i = 4 1 6 14 23 32 68 89 after loop body when i = 5 1 6 14 23 32 68 89 After sort 1 6 14 23 32 68 89 Georgia Institute of Technology

  7. How Efficient is Selection Sort? • We make n-1 comparisons the first time • Then n-2 comparisons • Then n-3 comparisons • And so on till • 3, 2, 1 • This is a total of • (n-1) + (n-2) + (n-3) + … + 3 + 2 + 1 • The equation for the number of steps in an array of n elements is • (n * (n-1)) / 2 Georgia Institute of Technology

  8. Selection Sort Efficiency • It doesn’t matter if the array was sorted or not when we start • Best case == worst case == average case • This algorithm will always take this long! • Big O (AB only) • (n * (n-1)) / 2 is (n2-n) / 2 • Keep only the item that grows the fastest as n gets really big so this is O(n2) Georgia Institute of Technology

  9. Insertion Sort Algorithm • Loop through the array and insert the next element in the array into the sorted portion in the correct position • Moving larger values to the right to make room • Start with the second item in the array • At index 1 • Use a temporary variable to hold the value at the current index Georgia Institute of Technology

  10. Insertion Sort Code public void insertionSort() { int temp = 0; int pos = 0; // loop from second element on for (int i = 1; i < a.length; i++) { // save current value at i and set position to i temp = a[i]; pos = i; // shift right any larger elements while (0 < pos && temp < a[pos - 1]) { a[pos] = a[pos - 1]; pos--; } a[pos] = temp; this.printArray("after loop body when i = " + i); } } Georgia Institute of Technology

  11. Running Insertion Sort Before sort 23 14 1 89 68 32 6 after loop body when i = 1 14 23 1 89 68 32 6 after loop body when i = 2 1 14 23 89 68 32 6 after loop body when i = 3 1 14 23 89 68 32 6 after loop body when i = 4 1 14 23 68 89 32 6 after loop body when i = 5 1 14 23 32 68 89 6 after loop body when i = 6 1 6 14 23 32 68 89 After sort 1 6 14 23 32 68 89 Georgia Institute of Technology

  12. Insertion Sort Efficiency • Best case • The array is in sorted order when you start • Only n-1 comparisons with no swapping • Worst case • The array is sorted in decreasing order • Need (n * (n – 1)) / 2 comparisons and lots of swapping • Average case • On average need (n * (n-1)) / 4 comparisions • Big O (AB only) • (n * (n-1)) / 4 is (n2-n) / 4 • Keep only the item that grows the fastest as n gets really big so this is O(n2) Georgia Institute of Technology

  13. Merge Sort Algorithm • If the current array length is 1 return • Base case on the recusion • Else • Break the array into two arrays • Copy the elements from the original into the new arrays • Create new ArraySorter objects with the new arrays • Do a recursive call to mergeSort on the new ArraySorter objects • Merge the sorted arrays into the original array Georgia Institute of Technology

  14. public void mergeSort() { // check if there is only 1 element if (a.length == 1) return; // otherwise create two new arrays int[] left = new int[a.length / 2]; for (int i = 0; i < left.length; i++) left[i] = a[i]; int[] right = new int[a.length - left.length]; for (int i = left.length, j=0; i < a.length; i++, j++) right[j] = a[i]; // create new ArraySorter objects ArraySorter sorter1 = new ArraySorter(left); sorter1.printArray("sorter1"); ArraySorter sorter2 = new ArraySorter(right); sorter2.printArray("sorter2"); // do the recursive call sorter1.mergeSort(); sorter2.mergeSort(); // merge the resulting arrays merge(left,right); this.printArray("After merge"); } Merge Sort Code Georgia Institute of Technology

  15. /** * Method to merge back into the current array * @param left sorted left array * @param right the sorted right array */ private void merge(int[] left, int[] right) { int leftIndex = 0; // current left index int rightIndex = 0; // current right index int i = 0; // current index in a // merge the left and right arrays into a while (leftIndex < left.length && rightIndex < right.length) { if (left[leftIndex] < right[rightIndex]) { a[i] = left[leftIndex]; leftIndex++; } else { a[i] = right[rightIndex]; rightIndex++; } i++; } // copy any remaining in left for (int j = leftIndex; j < left.length; j++) { a[i] = left[j]; i++; } // copy any remaining in right for (int j = rightIndex; j < right.length; j++) { a[i] = right[j]; i++; } } Merge Code Georgia Institute of Technology

  16. Before merge sort 23 14 1 89 68 32 6 sorter1 23 14 1 sorter2 89 68 32 6 sorter1 23 sorter2 14 1 sorter1 14 sorter2 1 After merge 1 14 After merge 1 14 23 sorter1 89 68 sorter2 32 6 sorter1 89 sorter2 68 After merge 68 89 sorter1 32 sorter2 6 After merge 6 32 After merge 6 32 68 89 After merge 1 6 14 23 32 68 89 After merge sort 1 6 14 23 32 68 89 Testing Merge Sort Georgia Institute of Technology

  17. Merge Sort Efficiency • Merge sort is usually more efficient than insertion sort and always more efficient than selection sort • Best case == Worst Case == Average Case • Merge n elements m times where n = 2m • Big O (AB only) • About n * m times and m is log2(n) so it is n * log(n) • O(n log(n)) Georgia Institute of Technology

  18. Quicksort (AB only) • Pick a pivot point (an element from the array) • Partition the array around the pivot and put all elements that are less than the pivot point in the left array and all elements greater than the pivot in the right array • Then do a recursive call on the left and right array • Stop the recursion when there is only one element in the array Georgia Institute of Technology

  19. Quicksort Efficiency • Usually the fastest algorithm • Best case when pivot point breaks the original array into two about equal sized subarrays • Worst case when the pivot point leaves all values in one array • O(n log(n)) Georgia Institute of Technology

  20. Summary • See animations of algorithms at • http://www.cs.ubc.ca/~harrison/Java/sorting-demo.html • Students needs to understand how to sort data • A: Selection Sort, Insertion Sort, Merge Sort • AB: Also Quicksort • All students should have some idea of the efficiency of each algorithm • A should understand best, average, and worst case • AB need to know "Big Oh" Georgia Institute of Technology

More Related