Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commita667d82

Browse files
fix bug
1 parenteceace8 commita667d82

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

‎heapSort.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@ def heapSort(alist):
55
output= []
66
foriinrange(length):
77
tempLen=len(alist)
8-
forjinrange((tempLen-1)//2,-1,-1):
9-
k=j
10-
v,heap=alist[k],False
11-
whilenotheapand2*k<=tempLen-1:
12-
index=2*k
13-
ifindex<tempLen-1:
14-
ifalist[index]<alist[index+1]:
15-
index+=1
16-
ifv>=alist[index]:
8+
forjinrange(tempLen//2-1,-1,-1):
9+
preIndex=j
10+
preVal,heap=alist[preIndex],False
11+
while2*preIndex<=tempLen-1andnotheap:
12+
curIndex=2*preIndex+1
13+
ifcurIndex<tempLen-1:
14+
ifalist[curIndex]<alist[curIndex+1]:
15+
curIndex+=1
16+
ifpreVal>=alist[curIndex]:
1717
heap=True
1818
else:
19-
alist[k]=alist[index]
20-
k=index
21-
alist[k]=v
22-
heapVal=alist.pop(0)
23-
output.insert(0,heapVal)
19+
alist[preIndex]=alist[curIndex]
20+
preIndex=curIndex
21+
alist[preIndex]=preVal
22+
output.insert(0,alist.pop(0))
2423
returnoutput
2524

2625
test= [2,6,5,9,10,3,7]
27-
print(heapSort(test))
26+
print(heapSort(test))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp