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

Commit 814d9bb

Browse files
add code
1 parent d10ca70 commit 814d9bb

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

‎chaoxi/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
+ [twoai_chat](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/twoai_chat) :两个机器人在一起会碰撞出怎样的的火花?
99
+ [Matplotlib_3D](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/Matplotlib_3D) :Python 30 行代码画各种 3D 图形
1010
+ [five_code](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/five_code) :Python 5 行代码的神奇操作!
11-
11+
+[python_sort](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/python_sort) :Python 排序了解一下?
1212

1313
---
1414

‎chaoxi/python_sort/python_sort.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 冒泡排序
2+
def bubble_s(data):
3+
length = len(data)
4+
# 第二层循环:循环一次,表示相邻两个元素进行了一次比较
5+
for i in range(length):
6+
for j in range(1, length - i):
7+
if data[j - 1] > data[j]:
8+
# 相邻元素进行替换
9+
data[j], data[j - 1] = data[j - 1], data[j]
10+
print(data)
11+
12+
# 选择排序
13+
def select_s(data):
14+
# 第一层循环:取出数组中的所有元素
15+
for i in range(len(data)):
16+
temp = i # 取出第一个元素用来比较
17+
# 第二层循环:从第i后面的一个值开始循环,与data[i]进行比较
18+
for j in range(i+1,len(data)):
19+
if data[j] < data[temp]:
20+
data[temp], data[j] = data[j], data[temp]
21+
print(data)
22+
23+
24+
25+
# 插入排序
26+
# 将第一个元素作为有序区的元素,从无序区取出一个元素与有序区元素进行逐个比较,并加入到有序区,依次循环
27+
28+
def insert_s(data):
29+
# 第一层循环: 从第二个元素开始循环取出元素,取出的元素再与有序区元素进行比较
30+
for i in range(1,len(data)):
31+
temp = data[i]
32+
j = i-1
33+
while j>=0 and temp < data[j]:
34+
data[j+1] = data[j]
35+
j = j-1 # 在与前面一个元素进行比较,所以j 需要减1
36+
# 当j = -1 就跳出循环,将temp值赋给第一个值,即data[0]
37+
data[j+1] = temp
38+
print(data)
39+
40+
41+
42+
# 快速排序
43+
def partition(data, left, right):
44+
temp = data[left]
45+
while left < right:
46+
# 如果最右边的值大于中间值,则最右边值往后退一个位置,反之,就将值赋值给最左边位置
47+
while left < right and data[right] >= temp:
48+
right = right - 1
49+
data[left] = data[right]
50+
# 如果最左边的值小于中间值,则最左边值往前进一个位置,反之,就将值赋值给最右边位置
51+
while left < right and data[left] <= temp:
52+
left = left + 1
53+
data[right] = data[left]
54+
# 循环结束,即可定位到中间位置,将初始值,赋值到这个位置
55+
data[left] = temp
56+
return left
57+
58+
59+
def quick_sort(data, left, right):
60+
if left < right:
61+
mid = partition(data, left, right)
62+
quick_sort(data, left, mid)
63+
quick_sort(data, mid + 1, right)
64+
65+
66+
67+
68+
# 计数排序
69+
def count_sort(data):
70+
count = [0 for _ in range(len(data)+1)]
71+
for i in data:
72+
count[i] += 1
73+
data.clear()
74+
for index, nums in enumerate(count):
75+
for j in range(nums):
76+
data.append(index)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /