Presentation is loading. Please wait.

Presentation is loading. Please wait.

Workshop for CS-AP Teachers

Similar presentations


Presentation on theme: "Workshop for CS-AP Teachers"— Presentation transcript:

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

2 Georgia Institute of Technology
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 Georgia Institute of Technology
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 Georgia Institute of Technology
Selection Sort Code 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(); Georgia Institute of Technology

6 Running Selection Sort
Before sort after loop body when i = 0 after loop body when i = 1 after loop body when i = 2 after loop body when i = 3 after loop body when i = 4 after loop body when i = 5 After sort 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) + … 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 Georgia Institute of Technology
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 after loop body when i = 1 after loop body when i = 2 after loop body when i = 3 after loop body when i = 4 after loop body when i = 5 after loop body when i = 6 After sort 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 Georgia Institute of Technology
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 Georgia Institute of Technology
Merge Sort Code 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"); } Georgia Institute of Technology

15 Georgia Institute of Technology
Merge Code /** * Method to merge back into the current array left sorted left array 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]; // copy any remaining in right for (int j = rightIndex; j < right.length; j++) a[i] = right[j]; Georgia Institute of Technology

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

17 Georgia Institute of Technology
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 Georgia Institute of Technology
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 Georgia Institute of Technology
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 Georgia Institute of Technology
Summary See animations of algorithms at 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


Download ppt "Workshop for CS-AP Teachers"

Similar presentations


Ads by Google