Skip to content

Commit 20de6d3

Browse files
committed
add pdb in code
1 parent aef3f37 commit 20de6d3

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

sorts/heap_sort.py

+22-16
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@
1212

1313
from __future__ import print_function
1414

15-
15+
import pdb
1616
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+
3134
def heap_sort(unsorted):
3235
'''
3336
Pure implementation of the heap sort algorithm in Python
@@ -45,11 +48,13 @@ def heap_sort(unsorted):
4548
>>> heap_sort([-2, -5, -45])
4649
[-45, -5, -2]
4750
'''
51+
print("in heap_sort: ", " list=",unsorted)
4852
n = len(unsorted)
4953
for i in range(n // 2 - 1, -1, -1):
5054
heapify(unsorted, i, n)
5155
for i in range(n - 1, 0, -1):
5256
unsorted[0], unsorted[i] = unsorted[i], unsorted[0]
57+
#print("in heap_sort: ",unsorted)
5358
heapify(unsorted, 0, i)
5459
return unsorted
5560

@@ -61,7 +66,8 @@ def heap_sort(unsorted):
6166
else:
6267
input_function = input
6368
tstart=time.clock()
64-
unsorted=list(range(150000,1,-1))
69+
pdb.set_trace()
70+
unsorted=list(range(10,1,-1))
6571
#print(sort(unsorted))
6672
print(heap_sort(unsorted))
6773
tend=time.clock()

0 commit comments

Comments
 (0)