Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
45 slides
-
Memory Upload • Insertion sort, merge sort and quick sort
algorithms • Sequential search and binary search • Overloaded methods -
Insertion Sort • Algorithm for insertion sort: 1. Split the
array into sorted and unsorted portions 2. Take the next item in the unsorted portion and insert it in correct order in the sorted portion -
Big-O Break • Efficiency of insertion sort: • Best case:
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 -
Example Code and Output • The outer for loop for
insertion sort a) 9, 3, 7, 5, 2, 8, b) 3, 9, 7, 5, 2, 8, c) 3, 7, 9, 5, 2, 8, d) 3, 5, 7, 9, 2, 8, e) 2, 3, 5, 7, 9, 8, f) 2, 3, 5, 7, 8, 9, -
Example Code and Output • Details on shifting of insertion
sort a) 9, 3, 7, 5, 2, 8, inserted item = 3 9, 9, b) 3, 9, 7, 5, 2, 8, inserted item = 7 9, 9, c) 3, 7, 9, 5, 2, 8, -
Example Code and Output c) 3, 7, 9, 5, 2,
8, inserted item = 5 9, 9, 7, 7, d) 3, 5, 7, 9, 2, 8, • Details on shifting of insertion sort -
Example Code and Output d) 3, 5, 7, 9, 2,
8, inserted item = 2 9, 9, 7, 7, 5, 5, 3, 3, e) 2, 3, 5, 7, 9, 8, • Details on shifting of insertion sort -
Example Code and Output e) 2, 3, 5, 7, 9,
8, inserted item = 8 9, 9, f) 2, 3, 5, 7, 8, 9, • Details on shifting of insertion sort -
Merge Sort • Algorithm for merge sort, which is a
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 -
Big-O Break • Efficiency of merge sort: • Worst and
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 -
Merge Sort Idealized a) 5, 3, 7, 9, 2, 8,
4, 6 b) 5, 3, 7, 9 2, 8, 4, 6 c) 5, 3 7, 9 2, 8 4, 6 d) 5 3 7 9 2 8 4 6 e) 3, 5 7, 9 2, 8 4, 6 f) 3, 5, 7, 9 2, 4, 6, 8 g) 2, 3, 4, 5, 6, 7, 8, 9 -
Example Merge Sort Output a)5, 3, 7, 9, 2, 8,
4, 6, b)3, 5, c) 7, 9, d)3, 5, 7, 9, e) 2, 8, f) 4, 6, g) 2, 4, 6, 8, h)2, 3, 4, 5, 6, 7, 8, 9, -
Detailed Merge Sort Output a)5, 3, 7, 9, 2, 8,
4, 6, SPLIT: start=0, half=3, end=7 SPLIT: start=0, half=1, end=3 SPLIT: start=0, half=0, end=1 5, 3, b)3, 5, -
Detailed Merge Sort Output b)3, 5, SPLIT: start=2, half=2, end=3
7, 9, c) 7, 9, 3, 5, 7, 9, d)3, 5, 7, 9, -
Detailed Merge Sort Output d)3, 5, 7, 9, SPLIT: start=4,
half=5, end=7 SPLIT: start=4, half=4, end=5 2, 8, e) 2, 8, -
Example Merge Sort Output e) 2, 8, SPLIT: start=6, half=6,
end=7 4, 6, f) 4, 6, 2, 8, 4, 6, g) 2, 4, 6, 8, -
Example Merge Sort Output g) 2, 4, 6, 8, 3,
5, 7, 9, 2, 4, 6, 8, h)2, 3, 4, 5, 6, 7, 8, 9, -
Quick Sort • Algorithm for quick sort, which is a
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 -
Big-O Break • Efficiency of quick sort: • Worst case:
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 -
Example Quick Sort Output a)5, 4, 8, 7, 2, 9,
3, b)3, 4, 2, 5, 8, 9, 7, c)3, 4, 2, d)2, 3, 4, e) 8, 9, 7, f) 7, 8, 9, g)2, 3, 4, 5, 7, 8, 9, -
Detailed Quick Sort Output a)5, 4, 8, 7, 2, 9,
3, pivot=5 swap 4 and 4 swap 2 and 8 swap 3 and 7 swap 5 and 3 b)3, 4, 2, 5, 8, 9, 7, -
Detailed Quick Sort Output c)3, 4, 2, pivot=3 swap 2
and 4 swap 3 and 2 d)2, 3, 4, -
Detailed Quick Sort Output e) 8, 9, 7, pivot=8 swap
7 and 9 swap 8 and 7 f) 7, 8, 9, g)2, 3, 4, 5, 7, 8, 9, -
Sequential Search • Review of sequential search, O(n): 1. Start
at the 1st item in the data collection 2. If that is not correct item, examine next item 3. Stop when find item, or come to end of data collection -
Binary Search • Review of binary search, O(log2 n): 1.Start
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 -
Example Code • See Searching.java program with methods sequentialSearch() and
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 -
Which Sorting Algorithm? • Since we have 5 sorting algorithms,
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 -
Which Sorting Algorithm? • However, we also have to consider
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 -
Which Sorting Algorithm? • For example, in the Searching.java program,
the integers are randomized, so quick sort or merge sort are the most appropriate algorithms to use, as the processing speed is O(n*log2 n) -
Grocery List • We can use our searching and sorting
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 -
Grocery List • The String class already implements interface Comparable,
so our sorting algorithms can use the compareTo() method to sort the data in the program GroceryListProgram5.java -
Grocery List • As for choosing a sorting algorithm, merge
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) -
Method add() • We also need to sort the data
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 -
Method add() • The add() method adds a grocery item
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 -
Method add() • Bubble sort is O(n2) if unsorted and
O(n) if sorted, but it will take O(n2) if the last element is the smallest element, so this is possibly a slow choice -
Method add() • Insertion sort is O(n2) if unsorted and
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 -
Method add() • Merge sort is always O(n*log2 n), so
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 -
Method add() • In conclusion, insertion sort was selected, as
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 -
Overloading • In Java, we can overload methods • Overloading
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 -
Overloading Examples • In the Sorting.java program, most of the
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 -
Overloading Examples • Overloaded Sorting.print() method public static void print(Comparable[]
array){ Sorting.print(array, 0, array.length-1); } public static void print(Comparable[] array, int start, int end) { //code to display an array... -
Overloading Examples • The overloaded sorting methods are particularly useful
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 -
Memory Defragmenter • Insertion sort, merge sort and quick sort
algorithms • Sequential search and binary search • Overloaded methods -
Task Manager • Before the next class, you need to:
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