forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap_sort.py
66 lines (53 loc) · 1.91 KB
/
Heap_sort.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Python program for implementation of heap Sort
# To heapify subtree rooted at index.
# length is size of heap
def heapify(arr, length, index):
# base case
# we will call this function until the largest number is the index...
largest_num_index = index
left_index = (index * 2) + 1
right_index = (index * 2) + 2
if(left_index < length and arr[index] < arr[left_index]):
largest_num_index = left_index
if(right_index < length and arr[largest_num_index] < arr[right_index]):
largest_num_index = right_index
# if index is not the largest, make it the largest!
# and run it again!
if(largest_num_index != index):
arr[index], arr[largest_num_index] = arr[largest_num_index], arr[index]
heapify(arr, length, largest_num_index)
def heap_sort(arr):
# need array length to create indices
length = len(arr)
for index in range(length, -1, -1):
# ask about functions modifying arrays without return value
# build max heap
heapify(arr, length, index)
# for each sorted heap, swap the root and the last number
for index in range(length - 1, 0, -1):
arr[index], arr[0] = arr[0], arr[index]
# then call heapify again with the new array
heapify(arr, index, 0)
# Taking Elements to be Sorted
data = [int(input())
for index in range(int(input("Enter elements to be sorted : ")))]
# Sending element to get sorted
heap_sort(data)
# Priting Elements after Getting Sorted
print(data)
'''
Sample Output:
Enter elements to be sorted : 5
30
50
10
20
40
[10, 20, 30, 40, 50]
Building the max heap requires O(n) calls to the buildHeap function which
takes O(log n) time to build the max heap
Therefore, total time complexity = O(nlog n)
In heap sort since we are only keeping data in the array and only
rearranging it so no data needs to be stored anywhere else (other than the swap step)
So, space complexity = O(1)
'''