Skip to content

Commit 44f17a1

Browse files
authored
Add files via upload
1 parent 7f4aa4c commit 44f17a1

10 files changed

+219
-0
lines changed

十大排序算法/01.冒泡排序.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 外循环控制循环次数,内循环控制左右元素两两比较,若第一个大于第二个则交换位置,最终最大的元素会放在最右边
2+
# 除去最后一个元素,对剩余元素重复以上循环
3+
def bubble_sort(nums):
4+
for i in range(1, len(nums)-1):
5+
for j in range(len(nums)-i):
6+
if nums[j] > nums[j+1]:
7+
nums[j], nums[j+1] = nums[j+1], nums[j]
8+
return nums
9+
10+
11+
if __name__ == '__main__':
12+
nums = [2, 1, 6, 7, 9, 5, 0, 4, 3, 8]
13+
print(bubble_sort(nums))

十大排序算法/02.选择排序.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 外循环控制循环次数,内循环查找出最小元素,并与第一个元素交换位置
2+
# 除去第一个元素,对剩余元素重复以上循环
3+
def select_sort(nums):
4+
for i in range(len(nums)-1):
5+
min_index = i
6+
for j in range(i+1, len(nums)):
7+
if nums[j] < nums[min_index]:
8+
min_index = j
9+
nums[min_index], nums[i] = nums[i], nums[min_index]
10+
return nums
11+
12+
13+
if __name__ == '__main__':
14+
nums = [2, 1, 6, 7, 9, 5, 0, 4, 3, 8]
15+
print(select_sort(nums))

十大排序算法/03.插入排序.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 从左往右遍历每个元素,将遍历到的元素插入到该元素左边序列中,且位置在从右往左第一个小于等于该元素的元素右边
2+
def insert_sort(nums):
3+
if len(nums) < 2:
4+
return nums
5+
for i in range(1, len(nums)):
6+
j = i
7+
while j > 0 and nums[j] < nums[j-1]:
8+
nums[j], nums[j-1] = nums[j-1], nums[j]
9+
j -= 1
10+
return nums
11+
12+
13+
if __name__ == '__main__':
14+
nums = [2, 1, 6, 7, 9, 5, 0, 4, 3, 8]
15+
print(insert_sort(nums))

十大排序算法/04.希尔排序.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 把元素按下标的一定增量分组,对每组使用直接插入排序算法排序
2+
def shell_sort(nums):
3+
n = len(nums)
4+
gap = n // 2
5+
while gap > 0:
6+
for i in range(gap, n):
7+
while i >= gap and nums[i] < nums[i-gap]:
8+
nums[i], nums[i-gap] = nums[i-gap], nums[i]
9+
i -= gap
10+
gap //= 2
11+
return nums
12+
13+
14+
if __name__ == '__main__':
15+
nums = [2, 1, 6, 7, 9, 5, 0, 4, 3, 8]
16+
print(shell_sort(nums))

十大排序算法/05.归并排序.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 将数组分解最小,然后依次合并两个有序数组
2+
# 合并思路:比较两个数组的最左边的数,取较小者,取了后相应的指针往右移一位。继续比较,直至一个数组为空,最后复制另一个数组的剩余部分
3+
def merge_sort(nums):
4+
if len(nums) < 2:
5+
return nums
6+
left = merge_sort(nums[:len(nums)//2])
7+
right = merge_sort(nums[len(nums)//2:])
8+
return merge(left, right)
9+
10+
11+
def merge(left, right):
12+
l = r = 0
13+
res = []
14+
while l < len(left) and r < len(right):
15+
if left[l] < right[r]:
16+
res.append(left[l])
17+
l += 1
18+
else:
19+
res.append(right[r])
20+
r += 1
21+
res += left[l:]
22+
res += right[r:]
23+
return res
24+
25+
26+
if __name__ == '__main__':
27+
nums = [2, 1, 6, 7, 9, 5, 0, 4, 3, 8]
28+
print(merge_sort(nums))

十大排序算法/06.快速排序.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 先在数组中挑出一个“基准”元素,比它小的放左边,比它大的放右边
2+
3+
# 逐一赋值形式
4+
def quick_sort(nums):
5+
l, r = 0, len(nums)-1
6+
# 数组为空或只有一个元素
7+
if l >= r:
8+
return nums
9+
key = nums[l]
10+
while l < r:
11+
while l < r and nums[r] >= key:
12+
r -= 1
13+
nums[l] = nums[r]
14+
while l < r and nums[l] <= key:
15+
l += 1
16+
nums[r] = nums[l]
17+
nums[l] = key
18+
l_nums = quick_sort(nums[:l])
19+
r_nums = quick_sort(nums[l+1:])
20+
return l_nums + [key] + r_nums
21+
22+
23+
# 两者交换形式
24+
def quick_sort2(nums):
25+
l, r = 0, len(nums)-1
26+
if l >= r:
27+
return nums
28+
p = l
29+
while l < r:
30+
while l < r and nums[r] >= nums[p]:
31+
r -= 1
32+
while l < r and nums[l] <= nums[p]:
33+
l += 1
34+
nums[r], nums[l] = nums[l], nums[r]
35+
nums[l], nums[p] = nums[p], nums[l]
36+
l_nums = quick_sort(nums[:l])
37+
r_nums = quick_sort(nums[l+1:])
38+
return l_nums + [nums[l]] + r_nums
39+
40+
41+
if __name__ == '__main__':
42+
nums = [2, 1, 6, 7, 9, 5, 0, 4, 3, 8]
43+
print(quick_sort(nums))

十大排序算法/07.堆排序.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def heap_sort(nums):
2+
n = len(nums)
3+
# 最后一个有子节点的结点的索引
4+
first = n//2-1
5+
# 构造最大堆。逐个有子节点的结点进行调整
6+
for start in range(first, -1, -1):
7+
max_heapify(nums, start, n-1)
8+
# 将最大堆转化成有序数组
9+
for end in range(n-1, 0, -1):
10+
# 最后一个元素与第一个交换位置
11+
nums[end], nums[0] = nums[0], nums[end]
12+
# 交换后排除最后一个元素,重新调整成最大堆
13+
max_heapify(nums, 0, end-1)
14+
return nums
15+
16+
17+
# 最大堆调整。子节点较大者与父节点交换
18+
def max_heapify(nums, start, end):
19+
root = start
20+
# 父节点交换下来后判新子节点是否还可继续交换
21+
while True:
22+
child = 2*root+1
23+
if child > end:
24+
break
25+
# 获取较大子节点
26+
if child+1 <= end and nums[child] < nums[child+1]:
27+
child += 1
28+
# 较大子节点成为父节点
29+
if nums[child] > nums[root]:
30+
nums[child], nums[root] = nums[root], nums[child]
31+
root = child
32+
else:
33+
break
34+
35+
36+
if __name__ == '__main__':
37+
nums = [2, 1, 6, 7, 9, 5, 0, 4, 3, 8]
38+
print(heap_sort(nums))

十大排序算法/08.计数排序.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def count_sort(nums):
2+
max_num, min_num = max(nums), min(nums)
3+
count = [0] * (max_num - min_num + 1)
4+
res = [0] * len(nums)
5+
# 记录每个元素出现次数
6+
for num in nums:
7+
count[num-min_num] += 1
8+
# 加上前一元素值,此时每个元素的含义是包括当前元素在内有多少个元素在桶里
9+
for i in range(1, len(count)):
10+
count[i] += count[i-1]
11+
# 获取每个元素在最终数组中对应位置索引并存入
12+
for num in nums:
13+
res[count[num-min_num]-1] = num
14+
count[num-min_num] -= 1
15+
return res
16+
17+
18+
if __name__ == '__main__':
19+
nums = [2, 1, 8, 9, 9, 5, 9, 4, 4, 8]
20+
print(count_sort(nums))

十大排序算法/09.桶排序.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 获取数组最大值和最小值并构建在该范围的数组长度,数组记录元素出现的个数,最后遍历数组获取对应个数的元素
2+
def bucket_sort(nums):
3+
max_num, min_num = max(nums), min(nums)
4+
count = [0 for _ in range(min_num, max_num+1)]
5+
res = []
6+
for num in nums:
7+
count[num-min_num] += 1
8+
for index in range(len(count)):
9+
if count[index] != 0:
10+
res += [index+min_num] * count[index]
11+
return res
12+
13+
14+
if __name__ == '__main__':
15+
nums = [2, 1, 8, 9, 9, 5, 9, 4, 4, 8]
16+
print(bucket_sort(nums))

十大排序算法/10.基数排序.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def radix_sort(nums):
2+
# 最大数字长度作为排序次数
3+
for k in range(len(str(max(nums)))):
4+
# 数字0-9建立10个桶
5+
buckets = [[] for _ in range(10)]
6+
# 对每个元素从低位到高位获取对应数字,放入对应的桶
7+
for num in nums:
8+
buckets[num//(10**k) % 10].append(num)
9+
nums = [num for bucket in buckets for num in bucket]
10+
return nums
11+
12+
13+
if __name__ == '__main__':
14+
nums = [21, 111, 84, 91, 454, 52, 312, 421, 4, 8]
15+
print(radix_sort(nums))

0 commit comments

Comments
 (0)