homework9 - 15-121 FALL 2009 [CORTINA/REID-MILLER]HOMEWORK...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-121 FALL 2009 [CORTINA/REID-MILLER]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 Big-O 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, pivotPosition-1);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 Reid-miller during the Spring '09 term at Carnegie Mellon.

Page1 / 5

homework9 - 15-121 FALL 2009 [CORTINA/REID-MILLER]HOMEWORK...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online