


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



a) If we insert all of the list elements into a min-heap, what would we easily be able to determine?
Generl idea of Heap sort:
myList sorted list with n items
myList unsorted list with n items
heap with n items
b) What is the overall O ( ) for heap sort?
In general, a problem can be solved recursively if it can be broken down into smaller problems that are identical in structure to the original problem.
a) What determines the “size” of a sorting problem?
b) How might we break the original problem down into smaller problems that are identical?
c) What base case(s) (i.e., trival, non-recursive case(s)) might we encounter with recursive sorts?
d) How do you combine the answers to the smaller problems to solve the original sorting problem?
e) Consider why a recursive sort might be more efficient. Assume that I had a simple n^2 sorting algorithm with n = 100, then there is roughly 100^2 / 2 or 5,000 amount of work. Suppose I split the problem down into two smaller sorting problems of size 50. If I run the n^2 algorithm on both smaller problems of size 50, then what would be the approximate amount of work?
If I further solve the problems of size 50 by splitting each of them into two problems of size 25, then what would be the approximate amount of work?
a) Fill in the merged Sorted Part in the diagram.
b) Describe how you filled in the sorted part in the above example?
Sorted size n
Unsorted size n/
Sorted size n/
Unsorted size n/
Sorted size n/
n/
n/
n/
n/
n/
n/
n/
n/
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2
2
2 2
2 2
2
2
2
2
2
2
2
2
2 2
2 2
2
2
a) On each level of the above diagram write the WORST-CASE number of comparisons and moves for that level.
b) What is the WORST-CASE total number of comparisons and moves for the whole algorithm (i.e., add all levels)?
c) What is the big-oh for worst-case?
Lecture 17 Page 2
10
10
10
20
20
20
35
35
35
40
40
40
45
45
45
60
60
60
25
25
25
50
50
50
Unsorted Part
Sorted Part
Unsorted Left Half
Sorted Left Half
Unsorted Right Half
Sorted Right Half
0
0
0
0
0
0
4
4
1
1
1
1
1
1
5
5
2
2
2
2
2
2
6
6
3
3
3
3
3
3
7
7
e) Ideally, the pivot item splits the list into two equal size problems. What would be the big-oh for Quick Sort in the best case?
f) What would be the big-oh for Quick Sort in the average case?
g) The textbook’s partition code (Listing 5.15 on page 225) selects the first item in the list as the pivot item. However, the above partition code selects the middle item of the list to be the pivot. What advantage does selecting the middle item as the pivot have over selecting the first item as the pivot?