Skip to content

Commit 52a2d66

Browse files
committed
Update HSExp.md
1 parent 1bbec44 commit 52a2d66

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/algorithms/explanations/HSExp.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,26 @@ Heap is a special tree-based data structure. A binary tree is said to follow a h
1515
* It is a complete binary tree.
1616
* 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
1717

18+
## Sorting with the heap
19+
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+
1822
## Complexity
1923

20-
Space complexity is O(1) in all cases. Worst case and average case time
24+
Time complexity:
25+
```
26+
Average Case O(n log n)
27+
Worst Case O(n logn)
28+
Best Case O(n)
29+
```
30+
Note: Best case, when all elements are equal is O(n), although many sources
31+
list best case as O(n log n)
32+
33+
Space complexity is always O(1), that is, no extra space is needed.
34+
35+
[ Previous Background treatment of complexity: Space complexity is O(1) in all cases. Worst case and average case time
2136
complexity is O(n log n). The best case time complexity is O(n), when
2237
all elements are equal (despite many sources listing the best case as
23-
O(n log n)).
38+
O(n log n)).]: #
39+
40+

0 commit comments

Comments
 (0)