|
2 | 2 |
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -Heapsort is a general purpose sorting algorithm that has *<verbatim>O(n log n)</verbatim>* behavior on every input. It achieves this by processing the data using a priority queue known as a heap. |
6 |
| - |
7 |
| -### Heap Data Structure |
8 |
| - |
9 |
| -The heap data structure is a priority queue that avoids the worst case behavior of lists and arrays, where unsorted lists or arrays are |
10 |
| -*<verbatim>O(n)</verbatim>* for finding and removing the maximum (or minimum for a min-priority queue) item and sorted lists or arrays are *<verbatim>O(n)</verbatim>* for inserting a new item into the priority queue. By contrast, heaps exhibit *<verbatim>O(log n)</verbatim>* behavior for both insertion and removal from the priority queue. |
11 |
| - |
12 |
| -The items in a heap are stored in an array. For ease of conceptualization, the array can be considered as a binary tree, where array element *<verbatim>A[i]</verbatim>* is a parent node with two children that are array elements *<verbatim>A[2i]</verbatim>* and *<verbatim>A[2i+1]</verbatim>*. |
13 |
| - |
14 |
| -Since there are no gaps in data in the array, the heap tree is complete. |
15 |
| -In a *max*-heap each node is larger or equal to both of its children. This property means that the root node, or the element *A[i]* is the largest item. |
16 |
| - |
17 |
| -PUT IN HERE SOMETHING ABOUT FORMING THE HEAP, I think. |
18 |
| - |
19 |
| -### Sorting with the heap |
20 |
| - |
21 |
| -Once the heap has been formed, sorting is straightforward. Using a *max*-heap, repeatedly swap the root (largest item) with the last available item in the array and make this array element no longer available for swapping. At this point, the root no longer contains the largest element, so the heap order must be restored. |
22 |
| - |
23 |
| -The heap is restored by checking every "parent node" in the array, to make sure it is larger than either of its "children". Parent nodes are at positions 1 - *n/2* in the array, because the last *n/2* nodes are leaves, *i.e.* have no children. The parent node is checked to make sure it is larger than either of its two children. If it is, then no action is necessary. If it isn't, then is it swapped with the largest of its two children. Initially we will be making a mini-heap of three items. Then |
24 |
| - |
25 |
| - |
26 |
| -CONTINUE HERE |
27 |
| - |
28 |
| - |
29 |
| - |
| 5 | +Heapsort is a general purpose in-place sorting algorithm that has |
| 6 | +*<verbatim>O(n log n)</verbatim>* behavior in the worst case. It achieves |
| 7 | +this by first rearranging the array so it is a *heap* (which has some |
| 8 | +ordering maintained; see below) then converting the heap into a sorted |
| 9 | +array. |
| 10 | + |
| 11 | +## The Heap Data Structure |
| 12 | + |
| 13 | +A heap is a complete binary tree represented by an array, with |
| 14 | +the root in *<verbatim>A[1]</verbatim>* and the children of |
| 15 | +*<verbatim>A[i]</verbatim>* being *<verbatim>A[2i]</verbatim>* and |
| 16 | +*<verbatim>A[2i+1]</verbatim>*. Each node is greater than or equal to |
| 17 | +it's children (this is called the *heap condition*), thus the root is |
| 18 | +the maximum (heap sort uses a "max" heap; there are also "min" heaps |
| 19 | +where the ordering is reversed). Note that there are no pointers etc - |
| 20 | +we can view the array as a tree so as to understand the ordering. |
| 21 | + |
| 22 | +## Building a heap "bottom up" |
| 23 | + |
| 24 | +The best way to build a heap from an unordered array is to first note that |
| 25 | +all the leaf nodes in the tree view are already heaps (they have no cildren |
| 26 | +so the heap condition is satisfied), and work up the tree (backwards |
| 27 | +through the array) to the root. Each step combines two existing heaps |
| 28 | +plus their parent node to form a new heap (some rearrangement may be |
| 29 | +needed; this is done by the *DownHeap* operation). |
| 30 | + |
| 31 | +## Sorting with a heap |
| 32 | + |
| 33 | +Sorting proceeds by repeating the following steps: |
| 34 | + |
| 35 | +- swap the largest item (the root) with the last item in the array |
| 36 | +- remove this largest item from further consideration (it is no longer |
| 37 | + considered part of the heap) |
| 38 | +- reform the remaining data items into heap order by performing |
| 39 | + *DownHeap* on the new root (note the two children of the root remain |
| 40 | + heaps) |
| 41 | + |
| 42 | +## The DownHeap operation |
| 43 | + |
| 44 | +*DownHeap* traverses down the tree, swapping the data in the node with |
| 45 | +the maximum child of the node. It stops as soon as the data in the node |
| 46 | +is greater than or equal to the maximum child (or the node is a leaf). |
| 47 | +The time complexity is *O(log n)* |
30 | 48 |
|
| 49 | +## Complexity |
31 | 50 |
|
32 |
| -MAYBE PUT SOMETHING in the More information tab about the analysis and why this is O(n). |
| 51 | +The worst case and nearly all other cases have time complexity *O(n log n)*. |
| 52 | +Space complexity is O(1). |
33 | 53 |
|
34 |
| -## Complexity |
35 | 54 |
|
36 |
| -Time complexity: |
37 |
| -``` |
38 |
| - Average Case O(n log n) |
39 |
| - Worst Case O(n logn) |
40 |
| - Best Case O(n) |
41 |
| - ``` |
42 |
| - Note: Best case, when all elements are equal is O(n), although many sources |
43 |
| - list best case as O(n log n) |
44 |
| - |
45 |
| -Space complexity is always O(1), that is, no extra space is needed. |
46 |
| - |
47 |
| -[ Previous Background treatment of complexity: Space complexity is O(1) in all cases. Worst case and average case time |
48 |
| -complexity is O(n log n). The best case time complexity is O(n), when |
49 |
| -all elements are equal (despite many sources listing the best case as |
50 |
| -O(n log n)).]: # |
51 | 55 |
|
52 | 56 |
|
0 commit comments