Download presentation
Presentation is loading. Please wait.
Published byErnest Andrews Modified over 6 years ago
1
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh
2
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
3
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
4
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 SortingBigOh
5
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
6
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
7
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
8
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
9
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
10
Merge Sort Timing vs. Selection Sort
Figure 2: Merge Sort Timing (blue) versus Selection Sort (red)
11
Searching and Sorting with Objects
12
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); }
13
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; } }
14
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
15
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
16
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
17
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);
18
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.