Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
LockGit
Py
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Py
/
heapq_sort.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
45 lines (37 loc) · 1.92 KB
master
Breadcrumbs
Py
/
heapq_sort.py
Copy path
Top
File metadata and controls
Code
Blame
45 lines (37 loc) · 1.92 KB
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
40
41
42
43
44
45
# !/usr/bin/env python
# encoding: utf-8
# author: Lock
# Created by Vim
"""
算法过程:
(1)、建堆:从len/2到第一个节点0处一直调用调整堆的过程,其中len为数组长度,len/2表示节点深度。
(2)、调整堆:比较节点i和它的孩子节点left(i),right(i),选出三者最大者,如果最大值不是节点i而是它的一个孩子节点,那便交换节点i和该节点,然后再调用调整堆过程,这是一个递归的过程。调整堆的过程时间复杂度与堆的深度有关系,是lgn的操作。
(3)、堆排序:主要利用上面两个过程进行。首先是根据元素构建堆,然后将堆的根节点取出(一般是与最后一个节点进行交换),将前面len-1个节点继续进行堆调整的过程,然后再将根节点取出,这样一直到所有节点都取出。
"""
def build_heap(seq):
length = len(seq)
for item in range(0, int((length / 2)))[::-1]:
adjust_heap(seq, item, length)
def adjust_heap(seq, root, length):
left_child = 2 * root + 1
right_child = 2 * root + 2
root_max = root
if left_child < length and seq[left_child] > seq[root_max]:
root_max = left_child
if right_child < length and seq[right_child] > seq[root_max]:
root_max = right_child
if root_max != root: # 如果做了堆调整,则root_max变更后是左节点或者右节点的,进行对调值操作
seq[root_max], seq[root] = seq[root], seq[root_max]
adjust_heap(seq, root_max, length)
def heap_sort(seq):
length = len(seq)
build_heap(seq) # 建立初始堆
for i in range(0, length)[::-1]:
seq[0], seq[i] = seq[i], seq[0] # 将根节点取出与最后一位做对调
adjust_heap(seq, 0, i) # 对前面len-1个节点继续进行堆调整过程
return seq
if __name__ == "__main__":
arr = [2, 1, 3, 8, 12, 5, 5, 6, 4, 10, 0]
print(arr)
heap_sort(arr)
print(arr)
You can’t perform that action at this time.