12
12
13
13
from __future__ import print_function
14
14
15
-
15
+ import pdb
16
16
def heapify (unsorted , index , heap_size ):
17
- largest = index
18
- left_index = 2 * index + 1
19
- right_index = 2 * index + 2
20
- if left_index < heap_size and unsorted [left_index ] > unsorted [largest ]:
21
- largest = left_index
22
-
23
- if right_index < heap_size and unsorted [right_index ] > unsorted [largest ]:
24
- largest = right_index
25
-
26
- if largest != index :
27
- unsorted [largest ], unsorted [index ] = unsorted [index ], unsorted [largest ]
28
- heapify (unsorted , largest , heap_size )
29
-
30
-
17
+ print ("in heapify: " , "index=" ,index ," list=" ,unsorted )
18
+
19
+ largest = index
20
+ left_index = 2 * index + 1
21
+ right_index = 2 * index + 2
22
+ if left_index < heap_size and unsorted [left_index ] > unsorted [largest ]:
23
+ largest = left_index
24
+
25
+ if right_index < heap_size and unsorted [right_index ] > unsorted [largest ]:
26
+ largest = right_index
27
+
28
+ if largest != index :
29
+ unsorted [largest ], unsorted [index ] = unsorted [index ], unsorted [largest ]
30
+ #print("in heapify: ",unsorted)
31
+ heapify (unsorted , largest , heap_size )
32
+
33
+
31
34
def heap_sort (unsorted ):
32
35
'''
33
36
Pure implementation of the heap sort algorithm in Python
@@ -45,11 +48,13 @@ def heap_sort(unsorted):
45
48
>>> heap_sort([-2, -5, -45])
46
49
[-45, -5, -2]
47
50
'''
51
+ print ("in heap_sort: " , " list=" ,unsorted )
48
52
n = len (unsorted )
49
53
for i in range (n // 2 - 1 , - 1 , - 1 ):
50
54
heapify (unsorted , i , n )
51
55
for i in range (n - 1 , 0 , - 1 ):
52
56
unsorted [0 ], unsorted [i ] = unsorted [i ], unsorted [0 ]
57
+ #print("in heap_sort: ",unsorted)
53
58
heapify (unsorted , 0 , i )
54
59
return unsorted
55
60
@@ -61,7 +66,8 @@ def heap_sort(unsorted):
61
66
else :
62
67
input_function = input
63
68
tstart = time .clock ()
64
- unsorted = list (range (150000 ,1 ,- 1 ))
69
+ pdb .set_trace ()
70
+ unsorted = list (range (10 ,1 ,- 1 ))
65
71
#print(sort(unsorted))
66
72
print (heap_sort (unsorted ))
67
73
tend = time .clock ()
0 commit comments