Download presentation
Presentation is loading. Please wait.
Published byRosalind Bryan Modified over 9 years ago
1
SortingBigOh ASFA AP Computer Science A
2
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(n 2 ) - 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) SortingBigOh
3
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
4
SortingBigOh 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
5
SortingBigOh 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 (n 2 -n) / 2 ◦ Keep only the item that grows the fastest as n gets really big so this is O(n 2 )
6
SortingBigOh 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
7
SortingBigOh 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 (n 2 -n) / 4 ◦ Keep only the item that grows the fastest as n gets really big so this is O(n 2 )
8
SortingBigOh If the current array length is 1 return ◦ 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
9
SortingBigOh 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 = 2 m Big O ◦ About n * m times and m is log 2 (n) so it is n * log(n) ◦ O(n log(n))
10
Figure 2: Merge Sort Timing (blue) versus Selection Sort (red)
11
Searching and Sorting with Objects
12
Arrays.sort sorts objects of classes that implement Comparable interface 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 public interface Comparable { int compareTo(Object otherObject); }
13
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 {... public int compareTo(Coin otherCoin) { if (value < other.value) return -1; if (value == other.value) return 0; return 1; }... }
14
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
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
Once your class implements Comparable, simply use the Arrays.sort method: Coin[] coins = new Coin[n]; // Add coins... Arrays.sort(coins); Continued
17
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 coins = new ArrayList (); // Add coins... Collections.sort(coins);
18
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.