Workshop for CS-AP Teachers

Slides:



Advertisements
Similar presentations
Visual C++ Programming: Concepts and Projects
Advertisements

CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Sorting Part 3 CS221 – 3/6/09. Sort Matrix NameWorst Time Complexity Average Time Complexity Best Time Complexity Worst Space (Auxiliary) Selection SortO(n^2)
Simple Sorting Algorithms
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
EFFICIENCY & SORTING II CITS Scope of this lecture Quicksort and mergesort Performance comparison.
Sort Algorithms.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting: why?  We do it A LOT!  Makes finding the largest and smallest value easier  Makes finding how many of a certain value are in a list easier.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Prof. U V THETE Dept. of Computer Science YMA
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Sorting Mr. Jacobs.
Lecture 14 Searching and Sorting Richard Gesick.
Simple Sorting Algorithms
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Quicksort
Chapter 7 Sorting Spring 14
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Sorting Algorithms The following pages present several sorting algorithms: Two implementations of insertion sort: one that uses 2 stacks and the other.
Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems."
Description Given a linear collection of items x1, x2, x3,….,xn
Quicksort 1.
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
SORTING AND SEARCHING.
Selection sort Given an array of length n,
Lecture 11 Searching and Sorting Richard Gesick.
Sorting … and Insertion Sort.
Searching and Sorting 1-D Arrays
slides created by Marty Stepp
24 Searching and Sorting.
Yan Shi CS/SE 2630 Lecture Notes
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
slides created by Marty Stepp and Ethan Apter
slides created by Marty Stepp
Topic 17 Faster Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical.
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
EE 312 Software Design and Implementation I
slides created by Marty Stepp
CSE 143 Sorting reading: 13.3, 13.4.
Quicksort.
Simple Sorting Algorithms
CSE 373 Data Structures and Algorithms
slides adapted from Marty Stepp
Data Structures & Algorithms
Quicksort.
Searching/Sorting/Searching
Simple Sorting Algorithms
Simple Sorting Algorithms
Sorting.
Module 8 – Searching & Sorting Algorithms
Quicksort.
Presentation transcript:

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

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

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

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

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

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 After sort Georgia Institute of Technology

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

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

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

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

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 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 Georgia Institute of Technology

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

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

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

Georgia Institute of Technology Merge Code /** * 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]; // copy any remaining in right for (int j = rightIndex; j < right.length; j++) a[i] = right[j]; Georgia Institute of Technology

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

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

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

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

Georgia Institute of Technology 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