Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
34 lines (26 loc) · 982 Bytes

File metadata and controls

34 lines (26 loc) · 982 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
##########################################################
#Heap Sort Algorithm in Python - Code by Trusha Talati
##########################################################
def heapify(arr, n, i):
largest = i
left_child = 2*i + 1
right_child = 2*i + 2
if(left_child<n and arr[left_child] > arr[largest]):
largest = left_child
if(right_child<n and arr[right_child] > arr[largest]):
largest = right_child
if largest != i:
arr[largest], arr[i] = arr[i] , arr[largest]
heapify(arr,n,largest)
def heapsort(arr,n) :
for i in range(n//2 - 1,-1,-1): #gives max heap
heapify(arr,n,i)
for i in range(n-1,0,-1):
arr[0] , arr[i] = arr[i] , arr[0] # max element at last ith position
heapify(arr,i,0) # heapify ith elements to have next max at 0th position.
def main():
arr = [4, 10, 3, 5, 1]
heapsort(arr,len(arr))
print("Sorted array -->",arr)
if __name__ == '__main__':
main()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.