Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fca0a4d

Browse files
committed
feat($sorting): add <heap-sort> and refs on <quick-sort>
add <heap-sort> section, and a reference link of `sorting comparison` in <quick-sort>
1 parent bb93abd commit fca0a4d

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

sorting/heap-sort.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Heap Sort
2+
3+
Based on a typical [heap](../tree/heap.md) data structure, Heap Sort is an [in-place, not stable comparison-baed sorting](overview.md) algorithm and can be thought of as a improved version of [selection sort](selection-sort.md) algorithm.
4+
5+
## Fundamental Ideas
6+
7+
Initially, a [BUILD-MAX-HEAP](../tree/heap.md) is called upon a randomly distributed inputs to setup a [max-heap](../tree/heap.md). Now that the topmost root key must be the largest among the heap, extract that key to a sorted array and restore the heap property using [MAX-HEAPIFY](../tree/heap.md) operation. Repeat the procedures of extraction and adjustment till the heap is empty, the sorted array is formed.
8+
9+
In each heap restoration process, instead of [selection sort](selection-sort.md) method of locating the maximum or minimum key within the inputs in linear time, Heap Sort uses [MAX-HEAPIFY](../tree/heap.md) operation to reduce time complexity to logarithmic bound.
10+
11+
## Pseudocode
12+
13+
The following code transforms a randomly distributed array into an ascending entry array.
14+
15+
```
16+
HEAP_SORT(A)
17+
BUILD_MAX_HEAP(A)
18+
for i = length(A) to 2
19+
swap A[1] and A[i]
20+
A.heap_size = A.heap_size - 1
21+
MAX_HEAPIFY(A, i)
22+
```
23+
24+
## Algorithm Analysis
25+
26+
Given a n-size inputs, there is a &Omicron;(n) estimate on [BUILD_MAX_HEAP](../tree/heap.md) operation; And for each input entry, a [MAX_HEAPIFY](../tree/heap.md) is expected to perform and thus costs &Omicron;(n &sdot; log(n)) in total.
27+
28+
Since the [Heap Sort](#heap-sort) is also a [comparison-based sorting](overview.md) that has proven with a lower bound &Omega;(n &sdot; log(n)), the more tighter bound for Heap Sort will be &Theta;(n &sdot; log(n)).

sorting/quick-sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,7 @@ wherein Pr(&zscr;<sub>i</sub> compares &zscr;<sub>j</sub>) = Pr(&zscr;<sub>i</su
148148
complexity holds, proof ends.
149149

150150
[quick-sort]: (#quick-sort)
151+
152+
## Additional References
153+
154+
1. Why is quicksort better than other sorting algorithms in practice? https://cs.stackexchange.com/questions/3/why-is-quicksort-better-than-other-sorting-algorithms-in-practice

0 commit comments

Comments
 (0)