Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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 }}
tomoncle
Python-notes
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Python-notes
/
algorithms
/
algorithm_sorting.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
68 lines (55 loc) · 1.71 KB
master
Breadcrumbs
Python-notes
/
algorithms
/
algorithm_sorting.py
Copy path
Top
File metadata and controls
Code
Blame
68 lines (55 loc) · 1.71 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 17-4-19 上午11:14
# @Author : Tom.Lee
# @Description :
# @File : algorithm_sorting.py
# @Product : PyCharm
def
bubble_sort
():
"""
冒泡排序:
n个元素,循环n-1轮,
每一轮,比较n-i次,选出最大值
"""
L
=
[
9
,
8
,
7
,
6
,
5
,
4
,
3
,
2
,
1
]
n
=
len
(
L
)
for
i
in
range
(
1
,
n
):
# 比较n-1轮
# print n - i
for
j
in
range
(
n
-
i
):
# 每i轮比较n-i次,选出最大值
if
L
[
j
]
>
L
[
j
+
1
]:
L
[
j
],
L
[
j
+
1
]
=
L
[
j
+
1
],
L
[
j
]
print
L
def
insertion_sort
():
"""
插入排序算法:
原序列:[2,3,1,34,5,6,11,7,8]
下标从0开始:
第一次:取下标1 和下标[:1]比较
第二次:取下标2 和下标[:2]比较
。。。
第n-1次:取下标n-1(注意此时的元素已经是最后一个元素了)和[:n-1]比较
共比较n-1次
"""
L
=
[
9
,
8
,
7
,
5
,
6
,
4
,
3
,
2
,
1
]
n
=
len
(
L
)
for
i
in
range
(
n
-
1
):
for
j
in
range
(
i
+
1
):
# 因为下标从0开始,所以第i次,对应的数据位置要 i+1表示当前下标位置
# print i+1,'-',j
if
L
[
i
+
1
]
<
L
[
j
]:
L
[
i
+
1
],
L
[
j
]
=
L
[
j
],
L
[
i
+
1
]
print
L
def
selection_sort
():
"""
选择排序算法:
每次找出最小元素,放置到序列头部,循环序列
第一次:找出最小放到下标0
第二次:在剩余找出最小放到下标1
。。。
第n-1次
"""
L
=
[
5
,
4
,
3
,
2
,
1
,
0
,
-
77
]
n
=
len
(
L
)
for
i
in
range
(
n
-
1
):
for
j
in
range
(
i
+
1
,
n
):
# print i,'-',j
if
L
[
i
]
>
L
[
j
]:
L
[
i
],
L
[
j
]
=
L
[
j
],
L
[
i
]
print
L
You can’t perform that action at this time.