Skip to content

Commit c42992a

Browse files
committed
heap sort
1 parent 143e355 commit c42992a

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Python implementation of Heap Sort Algorithm
2+
3+
def heapify(arr, n, i):
4+
# Find largest among root and children
5+
largest = i
6+
l = 2 * i + 1
7+
r = 2 * i + 2
8+
9+
if l < n and arr[i] < arr[l]:
10+
largest = l
11+
12+
if r < n and arr[largest] < arr[r]:
13+
largest = r
14+
15+
# If root is not largest, swap with largest and continue heapifying
16+
if largest != i:
17+
arr[i], arr[largest] = arr[largest], arr[i]
18+
heapify(arr, n, largest)
19+
20+
21+
def heapSort(arr):
22+
n = len(arr)
23+
24+
# Build max heap
25+
for i in range(n//2, -1, -1):
26+
heapify(arr, n, i)
27+
28+
for i in range(n-1, 0, -1):
29+
# Swap
30+
arr[i], arr[0] = arr[0], arr[i]
31+
32+
# Heapify root element
33+
heapify(arr, i, 0)
34+
35+
# Driver Code
36+
arr = [1, 12, 9, 5, 6, 10]
37+
heapSort(arr)
38+
n = len(arr)
39+
print("Sorted array is")
40+
for i in range(n):
41+
print("%d " % arr[i], end='')

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,26 @@
557557

558558
![](https://github.com/aish21/Algorithms-and-Data-Structures/blob/main/Resources/Animations/bucket.webp)
559559

560+
### [Heap Sort](/Practice%20Concepts/Sorting/heap-sort.py)
561+
* Heap sort works by visualizing the elements of the array as a special kind of complete binary tree called a heap.
562+
* If the index of any element in the array is i, the element in the index 2i+1 will become the left child and element in 2i+2 index will become the right child. Also, the parent of any element at index i is given by the lower bound of (i-1)/2
563+
* A binary tree is said to follow a heap data structure if - it is a complete binary tree and all nodes in the tree follow the property that they are greater than their children - 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
564+
565+
![](https://github.com/aish21/Algorithms-and-Data-Structures/blob/main/Resources/Animations/min-max-heap.png)
566+
567+
* Starting from a complete binary tree, we can modify it to become a Max-Heap by running a function called heapify on all the non-leaf elements of the heap.
568+
* To maintain the max-heap property in a tree where both sub-trees are max-heaps, we need to run heapify on the root element repeatedly until it is larger than its children or it becomes a leaf node.
569+
* Heap Sort Method:
570+
- Since the tree satisfies Max-Heap property, then the largest item is stored at the root node.
571+
- Swap: Remove the root element and put at the end of the array (nth position) Put the last item of the tree (heap) at the vacant place
572+
- Remove: Reduce the size of the heap by 1
573+
- Heapify: Heapify the root element again so that we have the highest element at root
574+
- Repeat this process
575+
* Time Complexity - O(nlog(n))
576+
* Space Complexity - O(1)
577+
578+
![](https://github.com/aish21/Algorithms-and-Data-Structures/blob/main/Resources/Animations/heapsort.gif)
579+
560580
## Search Algorithms
561581
<p align="center">
562582
<a href="#linear-search">Linear Search</a> •

Resources/Animations/heapsort.gif

645 KB
Loading

Resources/Animations/min-max-heap.png

33 KB
Loading

0 commit comments

Comments
 (0)