GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — algorithm-pattern-python/basic_algorithm/heapsort.py at master · dashidhy/algorithm-pattern-python

Expand file tree

/

heapsort.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
39 lines (29 loc) · 842 Bytes
/

heapsort.py

Copy path

File metadata and controls

39 lines (29 loc) · 842 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
def heap_adjust(A, start=0, end=None):
if end is None:
end = len(A)
while start is not None and start < end // 2:
l, r = start * 2 + 1, start * 2 + 2
swap = None
if A[l] > A[start]:
swap = l
if r < end and A[r] > A[start] and (swap is None or A[r] > A[l]):
swap = r
if swap is not None:
A[start], A[swap] = A[swap], A[start]
start = swap
return
def heapsort(A):
# construct max heap
n = len(A)
for i in range(n // 2 - 1, -1, -1):
heap_adjust(A, i)
# sort
for i in range(n - 1, 0, -1):
A[0], A[i] = A[i], A[0]
heap_adjust(A, end=i)
return A
# test
if __name__ == '__main__':
a = [7, 6, 8, 5, 2, 1, 3, 4, 0, 9, 10]
print(a)
print(heapsort(a))
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.