File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ def heapify (unsorted ,index ,heap_size ):
2+ largest = index
3+ left_index = 2 * index + 1
4+ right_index = 2 * index + 2
5+ if left_index < heap_size and unsorted [left_index ] > unsorted [largest ]:
6+ largest = left_index
7+
8+ if right_index < heap_size and unsorted [right_index ] > unsorted [largest ]:
9+ largest = right_index
10+
11+ if largest != index :
12+ unsorted [largest ],unsorted [index ] = unsorted [index ],unsorted [largest ]
13+ heapify (unsorted ,largest ,heap_size )
14+
15+ def heap_sort (unsorted ):
16+ n = len (unsorted )
17+ for i in range (n / 2 - 1 , - 1 , - 1 ) :
18+ heapify (unsorted ,i ,n )
19+ for i in range (n - 1 , - 1 , - 1 ):
20+ unsorted [0 ], unsorted [i ] = unsorted [i ], unsorted [0 ]
21+ heapify (unsorted ,0 ,i )
22+ return unsorted
23+
24+
25+ if __name__ == '__main__' :
26+ import sys
27+ if sys .version_info .major < 3 :
28+ input_function = raw_input
29+ else :
30+ input_function = input
31+
32+ user_input = input_function ('Enter numbers separated by coma:\n ' )
33+ unsorted = [int (item ) for item in user_input .split (',' )]
34+ print heap_sort (unsorted )
35+
You can’t perform that action at this time.
0 commit comments