Skip to content

Commit e645951

Browse files
committed
Update HSExp.md
1 parent cbd850b commit e645951

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/algorithms/explanations/HSExp.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,34 @@
22

33
---
44

5-
Heap Sort is a popular and efficient sorting algorithm in computer programming. Learning how to write the heap sort algorithm requires knowledge of two types of data structures - arrays and trees.
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.
66

7-
The initial set of numbers that we want to sort is stored in an array e.g. `[10, 3, 76, 34, 23, 32]` and after sorting, we get a sorted array `[3,10,23,32,34,76]`.
7+
### Heap Data Structure
88

9-
Heap sort works by visualizing the elements of the array as a special kind of complete binary tree called a heap.
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.
1011

11-
## Heap Data Structure
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>*.
1213

13-
Heap is a special tree-based data structure. A binary tree is said to follow a heap data structure if
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.
1416

15-
* It is a complete binary tree.
16-
* All nodes in the tree follow the property that they are greater than their children i.e. the largest element is at the root and both its children and smaller than the root and so on. Such a heap is called a max-heap. If instead, all nodes are smaller than their children, it is called a min-heap
17+
PUT IN HERE SOMETHING ABOUT FORMING THE HEAP, I think.
1718

18-
## Sorting with the heap
19+
### Sorting with the heap
1920

20-
Once the heap has been formed, sorting is straightforward. For a max-heap repeatedly swap the root (largest item) with the last item in the array, and reform the heap.
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+
30+
31+
32+
MAYBE PUT SOMETHING in the More information tab about the analysis and why this is O(n).
2133

2234
## Complexity
2335

0 commit comments

Comments
 (0)