Skip to content

Commit f7580b7

Browse files
author
Beej Jorgensen
committed
More clarification
1 parent b9c157c commit f7580b7

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

Analysis_Answers.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ Add your answers to the questions below.
77
2. Could you make your algorithm run in better time? If so, how?
88

99
3. What is the space complexity of the `heapsort` function? Recall that your
10-
implementation should return a new array with the sorted data. What would be
11-
the space complexity if your function instead altered the input array?
10+
implementation should return a new array with the sorted data. (Also remember
11+
that the size of the input array passed to the `heapsort()` function does
12+
_not_ contribute to the size complexity.)
13+
14+
Most online sources say that the space complexity of heapsort is `O(1)`. What
15+
would we have to change in our code to get there?

README.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,26 @@ questions in the `Algorithms_Answers.md` file.
5252
#### Task 1. Implement Heapsort
5353

5454
Inside the `task1` directory you'll find the `heap.py` file with a working
55-
implementation of the heap class. Your second task is to implement a sorting
56-
method called [heapsort](https://en.wikipedia.org/wiki/Heapsort) that uses the
57-
heap data structure in order to sort an array of numbers. Your `heapsort`
58-
function should return a new list containing all of the sorted data.
55+
implementation of the heap class--the heap is already written for you.
56+
57+
You have to figure out how to use the heap to implement heapsort.
58+
59+
Some hints:
60+
61+
* Initially when you make a new `Heap` data structure, it is empty.
62+
63+
* You can insert values into the heap with its `insert()` method.
64+
65+
* This is a _max heap_. That is, the `get_max()` and `delete()` methods will
66+
return the maximum value stored in the heap. In addition `delete()` also
67+
removes the value from the heap.
68+
69+
* Pseudocode in Wikipedia or elsewhere will be of little use here. Think
70+
conceptually at first; how could you use this information to sort a list of
71+
numbers? Then code.
72+
73+
Your `heapsort` function should return a new list containing all of the sorted
74+
data.
5975

6076
Run `python test_heap.py` to run the tests for your `heapsort` function to
6177
ensure that your implementation is correct.
@@ -65,17 +81,16 @@ ensure that your implementation is correct.
6581
Open up the `Analysis_Answers.md` file. This is where you'll jot down your
6682
answers for the runtimes of the functions you just implemented.
6783

68-
### Stretch Problems
69-
70-
#### Min versus Max
84+
### Stretch Problem: Min versus Max
7185

7286
The heap presented in the code is called a _max heap_, because `delete()` always
7387
returns the maximum value in the heap.
7488

7589
Modify it to be a _min heap_, so that `delete()` always returns the _minimum_
76-
value in the heap.
90+
value in the heap. Also change `get_max()` to `get_min()`.
7791

78-
Fix your heapsort after you do this. What did you have to modify?
92+
Your heapsort probably broke after you did this. What did you have to modify to
93+
fix it?
7994

8095
How does the time complexity change?
8196

0 commit comments

Comments
 (0)