Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| courses:cs211:winter2018:journals:bairdc:chapter2 [2018/01/30 04:42] – [2.4 – A Survey of Common Running Times] bairdc | courses:cs211:winter2018:journals:bairdc:chapter2 [2018/01/30 04:54] (current) – [2.5 – A More Complex Data Structure: Priority Queues] bairdc | ||
|---|---|---|---|
| Line 38: | Line 38: | ||
| Overall, this section was pretty readable and interesting. I'd give it a 7/10. The one nagging question I have is why the total number of subsets of an n-element set is 2^n. This is probably a very simple question and basic answer, but for some reason I'm having a hard time visualizing it. | Overall, this section was pretty readable and interesting. I'd give it a 7/10. The one nagging question I have is why the total number of subsets of an n-element set is 2^n. This is probably a very simple question and basic answer, but for some reason I'm having a hard time visualizing it. | ||
| + | ===== 2.5 – A More Complex Data Structure: Priority Queues ===== | ||
| + | |||
| + | The goal with a priority queue is to implement a data structure with n elements so that elements can be added and deleted, and the minimum selected, all in O(logn) time. You could use a list, but after removing the minimum, there needs to be a scan of all elements in O(n) time to find the new minimum. Also you couldn' | ||
| + | |||
| + | Balancing algorithms are shown below: | ||
| + | |||
| + | < | ||
| + | Heapify-down(H, | ||
| + | n = length(H) | ||
| + | if 2i > n then | ||
| + | Terminate with H unchanged | ||
| + | else if 2i < n then | ||
| + | left=2i and right=2i+1 | ||
| + | j be index that minimizes key[H[left]] and key[[H[right]] | ||
| + | else if 2i = n then | ||
| + | j=2i | ||
| + | if key[H[j]] < key[H[i]] then | ||
| + | swap array entries H[i] and H[j] | ||
| + | Heapify-down(H, | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | Heapify-up(H, | ||
| + | if i > 1 then | ||
| + | j=parent(i)=floor(i/ | ||
| + | if key[H[i]] < key[H[j]] then | ||
| + | swap array entries H[i] and H[j] | ||
| + | Heapify-up(H, | ||
| + | </ | ||
| + | |||
| + | This section was very readable and I would give it a 10/10 on readability and interestingness. I have no lingering questions after reading through the section. | ||
