Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| courses:cs211:winter2018:journals:devlinn:chapter2 [2018/01/21 18:52] – devlinn | courses:cs211:winter2018:journals:devlinn:chapter2 [2018/01/28 02:17] (current) – devlinn | ||
|---|---|---|---|
| Line 28: | Line 28: | ||
| I did not find this section as easy to follow as our discussion in class about the same decisions. If we had not worked through these questions prior to this reading I would have had a very hard time understanding their logic, since it was weakly justified. I rate this section a 5 for readability. I would also have liked a more in-depth explanation of the method to ensure O(// | I did not find this section as easy to follow as our discussion in class about the same decisions. If we had not worked through these questions prior to this reading I would have had a very hard time understanding their logic, since it was weakly justified. I rate this section a 5 for readability. I would also have liked a more in-depth explanation of the method to ensure O(// | ||
| + | |||
| + | ===== 2.4 A Survey of Common Running Times ===== | ||
| + | As we learned earlier, when creating algorithms a goal is to have a more efficient running time than a brute-force algorithm would have. There are several common run times that appear frequently in algorithms, including linear time, //nlogn// time, quadratic time, cubic time, and // | ||
| + | |||
| + | There are some running times that are worse than polynomial running time, including 2//< | ||
| + | |||
| + | ===== 2.5 A More Complex Data Structure: Priority Queues ===== | ||
| + | A priority queue is considered one of the most applicable data structures for use in algorithms. These structures are useful when a group of elements have a priority, which we call a key, and we seek to obtain the element with the highest priority. Ideally, we would hope to implement a priority queue which can perform insertion and deletion operations with O(//logn//) running time and can sort //n// numbers in O(// | ||
| + | |||
| + | In order to maintain a heap's structure after the insertion of an element, the algorithm Heapify-up will be useful. This algorithm can be roughly sketched as follows: | ||
| + | Heapify-up(// | ||
| + | if i > 1 then | ||
| + | let j = parent(i) = ⌊i/ | ||
| + | if key[// | ||
| + | swap array entries //H//[i] and //H//[j] | ||
| + | Heapify-up(// | ||
| + | endif | ||
| + | endif | ||
| + | |||
| + | Deleting an element must also consider how to maintain the order in the heap. The algorithm of Heapify-down can be useful for this, which is sketched as follows: | ||
| + | Heapify-down(// | ||
| + | let n = length(// | ||
| + | if 2i > //n// then | ||
| + | terminate with //H// unchanged | ||
| + | else if 2i < //n// then | ||
| + | let left = 2i and right = 2i + 1 | ||
| + | let j be the index that minimizes key[// | ||
| + | else if 2i = //n// then | ||
| + | let j = 2i | ||
| + | endif | ||
| + | if key[// | ||
| + | swap array entries //H//[i] and //H//[j] | ||
| + | Heapify-down(// | ||
| + | endif | ||
