Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.

Slides:



Advertisements
Similar presentations
Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search.
Advertisements

Garfield AP Computer Science
Sorting Algorithms: Merge and Quick. Lecture Objectives Learn how to implement the simple advanced sorting algorithms (merge and quick) Learn how to implement.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Abstract Data Types (ADTs) Data Structures The Java Collections API
1 MT258 Computer Programming and Problem Solving Unit 9.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SORTING AND SEARCHING CHAPTER Slides by Rick Giles 14.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Sorting and Searching.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 12 Recursion, Complexity, and Searching and Sorting
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Sorting. RHS – SWC 2 Sorting Searching in sorted data is much faster than searching in unsorted data Being able to sort data efficiently is thus a quite.
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.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Sorting. RHS – SOC 2 Sorting Searching in sorted data is much faster (O(log(n)), than searching in unsorted data (O(n)). Being able to sort data efficiently.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Sorting.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Searching Topics Sequential Search Binary Search.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithm Analysis 1.
Prof. U V THETE Dept. of Computer Science YMA
Sorts, CompareTo Method and Strings
16 Searching and Sorting.
19 Searching and Sorting.
CSC 427: Data Structures and Algorithm Analysis
May 17th – Comparison Sorts
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Searching – Linear and Binary Searches
CSC 222: Object-Oriented Programming
Introduction to complexity
Big O notation Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case.
CSC 421: Algorithm Design & Analysis
Introduction to Algorithms
Sorting by Tammy Bailey
Priority Queues Chuan-Ming Liu
Design and Analysis of Algorithms
Lecture 14: binary search and complexity reading:
Algorithm design and Analysis
CSC212 Data Structure - Section RS
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
ITEC 2620M Introduction to Data Structures
Lecture 15: binary search reading:
MSIS 655 Advanced Business Applications Programming
Algorithm Efficiency and Sorting
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
14 SORTING AND SEARCHING CHAPTER
Sub-Quadratic Sorting Algorithms
CSC 421: Algorithm Design & Analysis
CSC 427: Data Structures and Algorithm Analysis
Revision of C++.
8. Comparison of Algorithms
Amortized Analysis and Heaps Intro
CSE 373 Data Structures and Algorithms
Workshop for CS-AP Teachers
CSC 421: Algorithm Design & Analysis
Presentation transcript:

Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh

What is Big-O? runtime growth in relation to the number of items Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant time (Push and pop elements on a stack) II. O(n) - linear time The algorithm requires a number of steps proportional to the size of the task. (Finding the minimum of a list) III. O(n2) - quadratic time The number of operations is proportional to the size of the task squared. (Selection and Insertion sort) IV. O(log n) - logarithmic time (Binary search on a sorted list) V. O(n log n) - "n log n " time (Merge sort and quicksort) I. O(l) - constant time This means that the algorithm requires the same fixed number of steps regardless of the size of the task. Examples (assuming a reasonable implementation of the task): A. Push and Pop operations for a stack (containing n elements); B. Insert and Remove operations for a queue. II. O(n) - linear time This means that the algorithm requires a number of steps proportional to the size of the task. A. Traversal of a list (a linked list or an array) with n elements; B. Finding the maximum or minimum element in a list, or sequential search in an unsorted list of n elements; C. Traversal of a tree with n nodes; D. Calculating iteratively n-factorial; finding iteratively the nth Fibonacci number. III. O(n2) - quadratic time The number of operations is proportional to the size of the task squared. Examples: A. Some more simplistic sorting algorithms, for instance a selection sort of n elements; B. Comparing two two-dimensional arrays of size n by n; C. Finding duplicates in an unsorted list of n elements (implemented with two nested loops). IV. O(log n) - logarithmic time A. Binary search in a sorted list of n elements; B. Insert and Find operations for a binary search tree with n nodes; C. Insert and Remove operations for a heap with n nodes. V. O(n log n) - "n log n " time A. More advanced sorting algorithms - quicksort, mergesort SortingBigOh

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 SortingBigOh

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 SortingBigOh

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 (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) SortingBigOh

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 SortingBigOh

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 comparisons Big O (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) SortingBigOh

Merge Sort Algorithm If the current array length is 1 return Else Base case on the recursion 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 SortingBigOh

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 About n * m times and m is log2(n) so it is n * log(n) O(n log(n)) SortingBigOh

Merge Sort Timing vs. Selection Sort Figure 2: Merge Sort Timing (blue) versus Selection Sort (red)

Searching and Sorting with Objects

Sorting Real Data Arrays.sort sorts objects of classes that implement Comparable interface The call a.compareTo(b) returns A negative number if a should come before b 0 if a and b are the same A positive number otherwise public interface Comparable<Object> { int compareTo(Object otherObject); }

Sorting Real Data Several classes in Java (e.g. String and Date) implement Comparable You can implement Comparable interface for your own classes public class Coin implements Comparable { . . . public int compareTo(Object otherObject) { Coin other = (Coin) otherObject; if (value < other.value) return -1; if (value == other.value) return 0; return 1; } . . . } public class Coin implements Comparable <Coin> { . . . public int compareTo(Coin otherCoin) { if (value < other.value) return -1; if (value == other.value) return 0; return 1; } . . . }

CompareTo Method The implementation must define a total ordering relationship Antisymmetric If a.compareTo(b) = 0, then b.compareTo(a) = 0   Reflexive a.compareTo(a) = 0 Continued

CompareTo Method The implementation must define a total ordering relationship   Transitive If a.compareTo(b) = 0 and b.compareTo(c) = 0, then a.compareTo(c) = 0

Sorting Real Data Once your class implements Comparable, simply use the Arrays.sort method: Coin[] coins = new Coin[n]; // Add coins . . . Arrays.sort(coins); Continued

Sorting Real Data If the objects are stored in an ArrayList, use Collections.sort uses the merge sort algorithm Collections.sort uses the merge sort algorithm Collections.sort: ArrayList<Coin> coins = new ArrayList<Coin>(); // Add coins . . . Collections.sort(coins);

Assignment: Make a comparable class and a tester for it The call a.compareTo(b) returns A negative number is a should come before b 0 if a and b are the same A positive number otherwise Create a class with several attributes that implements the Comparable interface Make it creative and entertaining Must compare on multiple attributes Create a test class that sorts and array and an ArrayList of your class