This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 15121 FALL 2009 [CORTINA/REIDMILLER]HOMEWORK 9  due Friday, November 20 by 11:59PMELECTRONIC HANDIN AVAILABLE BY MONDAY 5PMPROBLEMS (10 pts)1. (2 pts) 16 values are being sorted using the following sorting algorithms: Selection Sort, Insertion Sort, Merge Sort, Quick Sort. At some point during each algorithm, we take two snapshots of the order of the elements so far and we get the following four pairs of charts below. Identify which sort algorithm is being used in each snapshot pair and explain how you determined your answer. Sorting Algorithm #1:Sorting Algorithm #1 cont'd:Sorting Algorithm #2:Sorting Algorithm #2 cont'd:Sorting Algorithm #3:Sorting Algorithm #3 cont'd:Sorting Algorithm #4:Sorting Algorithm #4 cont'd:2. (1.5 pts) Consider the following alternate implementation of Merge Sort that recursively sorts the array tablefrom index ato b, where a b: public static <T extends Comparable<T>> void sort(T table, int a, int b) {if (b  a > 0) {sort(table, a, (a+b)/2);sort(table, (a+b)/2 + 1, b);merge(table, a, b);}}The mergemethod copies the elements of tablefrom indices ato b(inclusive) into a new array and then merges the two halves of the new array back into table. a. Modify the method above so that it calls the mergemethod only if the array is not sorted after the two recursive calls. You should be able to determine this in constant time. b. Based on your new implementation, determine the best case runtime complexity of Merge Sort using BigO notation assuming the array has n elements in it overall. c. Is Merge Sort stable? Explain. 3. (1.5 pts) Consider the following implementation of Quick Sort that recursively sorts the array table from index ato b, where a b: public static <T extends Comparable<T>> void sort(T table, int a, int b){if (b  a > 0) {T pivot = table[a];int pivotPosition = partition(table, a, b, pivot);sort(table, a, pivotPosition1);sort(table, pivotPosition+1, b);}}The partitionmethod rearranges the array so all the elements less than the pivot come first (not necessarily sorted), followed by the pivot, followed by the elements that are greater than or equal to...
View
Full
Document
This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reidmiller during the Spring '09 term at Carnegie Mellon.
 Spring '09
 ReidMiller
 Algorithms, Insertion Sort, Selection Sort

Click to edit the document details