forked from thakurRashmi/Begineer_Friendly_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapSort.py
More file actions
43 lines (34 loc) · 1.01 KB
/
heapSort.py
File metadata and controls
43 lines (34 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#python heap Sort
#function of heapify
#n=size of heap
def heapify(arr, n, i):
largest = i #initialize the largest as the root
l = 2 * i + 1#check child of left = 2*i + 1
r = 2 * i + 2#check child of right = 2*i + 2
#if left child of root exists and > than root
if l < n and arr[largest] < arr[l]:
largest = l
#if right child of root exists and > than root
if r < n and arr[largest] < arr[r]:
largest = r
#changing the root to largest
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
#heapify the root.
heapify(arr, n, largest)
#function heapsort to sort
def heapSort(arr):
n = len(arr)
#build the maxheap.
for i in range(n//2 - 1, -1, -1):
heapify(arr, n, i)
#extracting the elements
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)
#---main program--
arr = [12, 11, 13, 5, 6, 7]
print("Array we entering: ",arr)
heapSort(arr)
print()
print("Sorted array is",arr)