排序类型 | 时间复杂度 |
---|---|
选择排序(Selection Sort) | O ( n 2 ) O(n^{2} ) O(n2) |
合并/归并排序(Merge Sort) | O ( n log n ) O(n\log n ) O(nlogn) |
快速排序(Quick Sort) | 平均情况 O ( n log n ) O(n\log n ) O(nlogn)最糟情况 O ( n 2 ) O(n^{2} ) O(n2) |
计数排序(Counting Sort) | O ( n + k ) O(n+k ) O(n+k) |
来自《算法图解》一书
def findSmallest(arr):
smallest = arr[0] # 存储最小值
smallest_index = 0 # 存储最小值索引
for i in range(1,len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index
def selectionSort(arr):
newArr = []
for i in range(len(arr)):
smallest_ind = findSmallest(arr)
newArr.append(arr.pop(smallest_ind))
return newArr
print(selectionSort([5,3,6,2,10,58,23,31,9,14,4,46,25,35,1,56,29,20,18,43,40,36,49]))
参考: Python实现合并排序(归并排序)(一文看懂)
def merge(arr_a, arr_b):
arr_c = []
i = j = 0
while i < len(arr_a) and j < len(arr_b):
if arr_a[i] < arr_b[j]:
arr_c.append(arr_a[i])
i += 1
else:
arr_c.append(arr_b[j])
j += 1
if i == len(arr_a):
return arr_c + arr_b[j:]
else:
return arr_c + arr_a[i:]
def mergeSort(arr):
if len(arr) < 2:
return arr
middle = len(arr) // 2
left = mergeSort(arr[:middle])
right = mergeSort(arr[middle:])
return merge(left, right)
print(mergeSort([23,12,3,7,5,32,37,29,15,24,19]))
来自《算法图解》一书
def quickSort(arr):
if len(arr) < 2:
return arr # 基线条件:为空或只包含一个元素的数组是“有序”的
else:
pivot = arr[0] # 递归条件,基准值
less = [i for i in arr[1:] if i <= pivot] # 由所有小于基准值的元素组成的子数组
greater = [i for i in arr[1:] if i > pivot] # 由所有大于基准值的元素组成的子数组
return quickSort(less) + [pivot] + quickSort(greater)
print(quickSort([10,28,2,36,45,23,14,39,32,24]))
参考: python实现【计数排序】(Count Sort)
def countingSort(arr):
max_value = max(arr)
res = []
count_nums = [0 for i in range(max_value + 1)]
for num in arr:
count_nums[num] += 1
for i in range(len(count_nums)):
if count_nums[i] != 0:
# res.extend(count_nums[i] * [i]) # 元素i有 count_nums[i]个,添加入最终的排序数组
res += count_nums[i] * [i]
return res
print(countingSort([12,30,2,29,23,18,5,7,25,15,8]))