CS 202 Fall 2010: Assignment 8
Homework Assignment #8 : Due Friday 12/3 Before Lecture
The purpose of this assignment is to explore the intuitive sorting algorithms and their complexity.
Part 1: Operations
Consider the following scripts; assume each is run independently.
A) For each of the scripts, how many times will a statement in the (largest) innermost loop execute? (that is, what will be the value of the counter variable when the script terminates?)
B) What is the order of magnitude for the number of operations in each algorithm as a function of the variable N? Remember that when calculating the order of magnitude, one disregards all constant factors. Hint: If an algorithm runs in an amount of time completely independent of the size of N, then that algorithm is O(1).
C) Imagine that you must choose between two different algorithms, A and B, that perform the same task (i.e., the solve the same problem). Algorithm A executes 3 * N * N instructions; algorithm B executes 243000*N instructions. For what values of N is Algorithm A faster? For what values of N is Algorithm B faster? As N gets larger, which Algorithm is the better choice?
Part 2: Sorting
A) Use an in-place selection sort on the following three lists. Show the contents of each list at the end of each iteration of the outermost loop (where the variable i is increased).
i) 573, 839, 102, 550, 593, 996, 678, 111, 264, 266
ii) 95, 36, 683, 92784, 73856, 3811, 392, 38, 258, 399
iii) 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100
B) Given the items in List i), how many times will the inner loop be executed during a given iteration of the outer loop? Fill in the following table.
Outerloop # | Inner loop iterations |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 |
C) Use an insertion sort (such as the one shown above) on the following lists. Show the contents of the list at the end of each iteration of the outermost loop.
i) 573, 839, 102, 550, 593, 996, 678, 111, 264, 266
ii) 95, 36, 683, 92784, 73856, 3811, 392, 38, 258, 399
iii) 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100
D) Imagine that the numbers in the lists above are high scores; associated with each high score list is another list called "Names". The contents of the Names list is initially Alice, Bob, Charles, Doug, Ethan, Frank, George, Harriot, Ian, Janice. When this Names list is associated with the high score list from part i), then Alice has the score 573, Bob has the score 839, and so forth.
Imagine that each of the sorting algorithms (whether selection or insertion) is modified to shuffle the elements of the Names list at the same time as the elements of the High Scores list; that is, when a score that was initially at position i of the High Scores list is moved to position k, then the Name that was previoiusly at position i of the Names list should be moved to position k in the Names list as well.
What will be the final order of the Names list when the sorting algorithm runs over the List in example i)?
Turning in Your Work
You may either turn in a written version of your homework before lecture on Friday or upload a copy to Folder Homework 8 through Learn@UW.
Menu
Fall 2010Time: MWF 9:55-10:45
Room: 105 Psychology
Lab: 1370 CS (1st floor)
Instructor:
Prof Andrea Arpaci-Dusseau
Office Hours Tue 2:30-3:30, Wed 11-12
Office: 7375 Computer Sciences
Email: dusseau "at" cs.wisc.edu