O(n), already sorted • Worse case: O(n2), so does depend on initial order of elements • This algorithm has nested for loops • Outer for loop repeats N-1 times • Nested while loop shifts elements to the left into correct place
divide-and-conquer recursive algorithm: 1. Split the array into two halves, split the halves into two halves (fourths), etc. until have N arrays of only 1 element 2. Merge these ordered mini-arrays two at a time into larger, ordered arrays until arrive at one sorted array
best cases: O(n*log2 n) • Splits the problem into halves O(log2 n) and merges the two halves O(n), so O(n)*O(log2 n)=O(n*log2 n) • Not dependent on initial order of elements, but requires 2*N storage
divide-and-conquer recursive algorithm: 1. Split the array into two (possible unequal) halves (1st part is less than the pivot, and 2nd part is greater than or equal to the pivot) 2. Repeat for left and right halves
O(n2), if array is sorted (in ascending or descending order) and pivot is smallest or largest element • Average case: O(n * log2 n), when two parts that keep splitting have an equal number of elements
at middle item in sorted data collection 2.If middle item is not the correct item, examine either 1st half or 2nd half of sorted data collection for the item 3.Stop when find item, or find that the item does not exist
binarySearch() • Program requires user to enter two integers for numerator and denominator on command line, and then searches through an array of randomly generated Fractions for the user’s Fraction
how can we decide which one to use? • Obviously, the O(n*log2 n) algorithms (merge and quick sort) run faster than the O(n2) algorithms (selection, bubble and insertion sort) for random data
the initial conditions of the array • For example, if the array is already sorted, then O(n2) quick sort will take longer than O(n) bubble sort • Since we have tradeoffs, we must think before choosing an appropriate sorting algorithm
methods on any array as long as interface Comparable has been implemented for the array’s class • For example, we can sort the data in our GroceryListProgram5.java, because its uses an array of Strings
sort is used in the method readFromFile() because it always has a Big-O of O(n*log2 n) • If quick sort is used, the program will be quite slow if the input file has already been sorted, as Big-O is O(n2)
after an item has been added to the array • We could add the grocery items in alphabetical order into the array • However, we will save programming time and avoid possible bugs if we simply use the sorting methods we have just created
to the end of the array, so the array is already sorted, except for the last element • For this problem, which sorting algorithm is best? • Selection sort is always O(n2), so it is a guaranteed slow choice
O(n) if sorted, but it will only need to loop twice if the smallest element is at the end of the array • This will be O(n) + O(n) = O(n), so this is a fast choice
it is a reliable choice, but with average processing speed • Quick sort is O(n*log2 n) if unsorted and O(n2) if sorted • If an array is mostly sorted, then this makes quick sort “lopsided”, which will be close to O(n2), which is slow
it will have the quickest processing speed for method add() • Note that we are thinking about large sets of data for Big-O • If the grocery list only has 10 items, then the 5 sorting algorithms will run with roughly the same speed
means that we use the same method name, but different number of parameters or different parameter types • Overloading is used to make method calls more flexible, so that we can call the same method with different parameters
methods are overloaded • For each of these methods, the overloaded method with only 1 parameter calls its overloaded method with 3 parameters • Overloading makes our code easier to use and read
in the program GroceryListProgram5.java • If we use Sorting.mergeSort(array), the program will crash because of a NullPointerException, since the array has many null elements
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture