Skip to content

Commit 870a753

Browse files
authored
Merge pull request deutranium#219 from K-u-n-a-l-c/patch-6
Create README.md HeapSort
2 parents 3d19707 + 99bf67f commit 870a753

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

sortingAlgo/heapSort/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Heap Sort
2+
Heap sort is one of the sorting algorithms used to arrange a list of elements in order. Heapsort algorithm uses one of the tree concepts called Heap Tree.
3+
In this sorting algorithm, we use Max Heap to arrange list of elements in Descending order and Min Heap to arrange list elements in Ascending order.
4+
There are 2 variations of heap possible
5+
### MIN HEAP
6+
* Here the value of parent is always less than the value of its children
7+
Hence root will be the minimum in the entire heap
8+
### MAX HEAP
9+
* Here the value of parent is always more than the value of its children
10+
Hence root will be the maximum in the entire heap
11+
12+
## Algorithm
13+
* Step 1 - Construct a Binary Tree with given list of Elements.
14+
* Step 2 - Transform the Binary Tree into Max Heap.
15+
* Step 3 - Delete the root element from Max Heap using Heapify method.
16+
* Step 4 - Put the deleted element into the Sorted list.
17+
* Step 5 - Repeat the same until Max Heap becomes empty.
18+
* Step 6 - Display the sorted list.
19+
20+
## Pseudo-code
21+
```
22+
heapify(arr , i)
23+
leftChild = arr [2*0 + 1];
24+
rightChild = arr [2*0 + 2];
25+
maxIndex = max( arr[i], leftChild, rightChild)
26+
if(i != maxIndex)
27+
swap(arr[i], arr[maxIndex])
28+
```
29+
```
30+
buildMaxHeap(arr)
31+
for(int i = n / 2 - 1; i >= 0; i--)
32+
heapify(arr, i);
33+
```
34+
35+
## Complexity
36+
37+
- Worst case time complexity: `O(n log n)`
38+
- Average case time complexity: `O(n log n)`
39+
- Best case time complexity: `O(n log n)`
40+
- Space complexity: `O(1)`
41+
42+
## Applications of Heap Sort
43+
* K sorted array
44+
* K largest or smallest elements in an array
45+
46+
### Instruction for Running code:
47+
- C
48+
```
49+
gcc heapSort.c
50+
./a.out
51+
```
52+
- Cpp
53+
54+
````
55+
g++ heapSort.cpp
56+
./a.out
57+
````
58+
- Java
59+
60+
```
61+
javac heapSort.java
62+
java heapSort.class
63+
```
64+
- Python
65+
```
66+
python3 heapSort.py
67+
```

0 commit comments

Comments
 (0)