Heapsort Note: Heapsort is not a divide-and-conquer algorithms. Instead, it is an algorithm based on a data structure, a max-heap. 1) Define the "binary tree" data structure. 2) A "nearly complete binary tree" is a binary tree in which every level of the tree, except possibly the bottom level, is completely filled in and all the nodes in the bottom level are as far to the left as possible. 3) A nearly complete binary tree can be stored efficiently in an array (in breadth-first order). Let i be the index in the array of any node of the binary tree. Then its parent, left child, and right child nodes have the following indices in the array. parent(i) = (i - 1) / 2 // integer division left(i) = 2 * i + 1 right(i) = 2 * i + 2 4) Every array can be thought of as representing a nearly complete binary tree. 5) A "max-heap" is a nearly complete binary tree in which the value stored in every node is at least as large as the values in its two child nodes. 6) Since a max-heap is a nearly complete binary tree, we can, if we want, store a max-heap in an array. 7) An array that holds a max-heap is not sorted, but it is "kind of" sorted because of the way a max-heap is organized. 8) The heapify() method takes a nearly complete binary tree in which each child of the root is a max-heap, and converts the binary tree into a max-heap. You should visualize the heapify() method as acting on a real binary tree, not the array storage of the binary tree. 9) You convert an arbitrary array into a max-heap by starting at the last parent node and then heapifying every sub tree all the way up to the root. Again, you are better off visualizing this algorithm as acting on a real btree, rather than on the array storage of the btree. 10) You sort an array of length n that holds a max-heap by a) swapping the last element of the max-heap with the root of the max-heap, b) decrease the size of the max-heap by 1, c) re-heapifying the (now smaller) nearly complete binary tree, d) repeat n-1 times 11) The heapsort algorithm starts with an arbitrary array, then uses (9) to convert the array into a max-heap, then uses (10) to sort the max-heap.