一、排序算法
排序算法是一种能将一串数据依照特定顺序进行排列的一种算法
默认使用升序
1.1 排序算法的稳定性
1.2 常见的排序算法
1.2.1 冒泡排序
按照遇到两个相同的数据不进行处理为前提,冒泡排序是稳定的
eg:
第一种:
import random
def bubble_sort(alist):
"""冒泡排序"""
n = len(alist)
for j in range(n-1):
for i in range(n-1-j):
if alist[i] > alist[i+1]:
alist[i], alist[i+1] = alist[i+1], alist[i]
if __name__ == '__main__':
my_list = []
for k in range(10):
res = random.randint(10, 60)
my_list.append(res)
print("排序前:", my_list)
bubble_sort(my_list)
print("升序后:", my_list)
第二种:改进
import random
def bubble_sort(alist):
"""冒泡排序"""
n = len(alist)
for j in range(n-1):
count = 0
for i in range(n-1-j):
if alist[i] > alist[i+1]:
alist[i], alist[i+1] = alist[i+1], alist[i]
count += 1
if 0 == count:
break
if __name__ == '__main__':
my_list = []
for k in range(10):
res = random.randint(10, 60)
my_list.append(res)
print("排序前:", my_list)
bubble_sort(my_list)
print("升序后:", my_list)
1.2.2 选择排序
选择排序处理有序和无序的一串数据是一样的,所以最优和最差时间复杂度一样
每次选择最小值时,选择排序是稳定的
eg:
import random
def select_sort(alist):
"""选择排序"""
min_index = alist[0]
n = len(alist)
for j in range(n-1):
min_index = j
for i in range(j+1, n):
if alist[i] < alist[min_index]:
min_index = i
if j != min_index:
alist[j], alist[min_index] = alist[min_index], alist[j]
if __name__ == '__main__':
my_list = []
for k in range(10):
res = random.randint(10, 60)
my_list.append(res)
print("排序前:", my_list)
select_sort(my_list)
print("升序后:", my_list)
1.2.3 插入排序
最坏的情况是处理降序的一串数据
最好的情况是处理升序的一串数据
eg:
import random
def insert_sort(alist):
"""插入排序"""
n = len(alist)
for j in range(1, n):
for i in range(j, 0, -1):
if alist[i] < alist[i-1]:
alist[i], alist[i-1] = alist[i-1], alist[i]
else:
break
if __name__ == '__main__':
my_list = []
for k in range(10):
res = random.randint(10, 60)
my_list.append(res)
print("排序前:", my_list)
insert_sort(my_list)
print("升序后:", my_list)
1.2.4 希尔排序
对插入排序的改进
间隔gap为4、2、1
gap=1就是插入排序
eg:
import random
def shell_sort(alist):
"""希尔排序"""
n = len(alist)
gap = n // 2
while gap >= 1:
for j in range(gap, n):
i = j
while (i-gap) >= 0:
if alist[i] < alist[i-gap]:
alist[i], alist[i-gap] = alist[i-gap], alist[i]
i -= gap
else:
break
gap //= 2
if __name__ == '__main__':
my_list = []
for k in range(10):
res = random.randint(10, 60)
my_list.append(res)
print("排序前:", my_list)
shell_sort(my_list)
print("升序后:", my_list)
1.2.5 快速排序重要
eg:
import random
def quick_sort(alist, start, end):
"""快速排序"""
if start >= end:
return
mid = alist[start]
left = start
right = end
while left < right:
while left < right and alist[right] >= mid:
right -= 1
alist[left] = alist[right]
while left < right and alist[left] < mid:
left += 1
alist[right] = alist[left]
alist[left] = mid
quick_sort(alist, start, left-1)
quick_sort(alist, left+1, end)
if __name__ == '__main__':
my_list = []
for k in range(10):
res = random.randint(10, 60)
my_list.append(res)
print("排序前:", my_list)
quick_sort(my_list, 0, len(my_list)-1)
print("升序后:", my_list)
1.2.6 归并排序
eg:
import random
def merge_sort(alist):
"""归并排序"""
n = len(alist)
if 1 == n:
return alist
mid = n // 2
# 对左半部分进行归并排序
left_sorted_list = merge_sort(alist[:mid])
# 对右半部分进行归并排序
right_sorted_list = merge_sort(alist[mid:])
# 合并两个有序集合
left, right = 0, 0
merge_sorted_list = []
left_n = len(left_sorted_list)
right_n = len(right_sorted_list)
while left < left_n and right < right_n:
if left_sorted_list[left] <= right_sorted_list[right]:
merge_sorted_list.append(left_sorted_list[left])
left += 1
else:
merge_sorted_list.append(right_sorted_list[right])
right += 1
merge_sorted_list += left_sorted_list[left:]
merge_sorted_list += right_sorted_list[right:]
return merge_sorted_list
if __name__ == '__main__':
my_list = []
for k in range(10):
res = random.randint(10, 60)
my_list.append(res)
print("排序前:", my_list)
res = merge_sort(my_list)
print("升序后:", my_list)
print("升序后:", res)
原序列并未改变,而是返回了一个新的序列
参阅:
- 十大经典排序算法(动图演示)
- 排序算法(Gif动图演示)
更新中......