由于最近开始深入学习 python 数据结构,简单的用python来实现一波十大经典排序算法。分别是:
比较类排序算法。算法描述如下(假设是升序排序):
O( n 2 n^2 n2)
'''冒泡排序'''
def BubbleSort(array):
length = len(array)
for i in range(length):
for j in range(length-i-1):
if array[j] > array[j+1]: array[j+1], array[j] = array[j], array[j+1]
return array
比较类排序算法。算法描述如下(假设是升序排序):
O( n 2 n^2 n2)
'''选择排序'''
def SelectionSort(array):
length = len(array)
for i in range(length-1):
idx_min = i
for j in range(i+1, length):
if array[j] < array[idx_min]:
idx_min = j
array[i], array[idx_min] = array[idx_min], array[i]
return array
比较类排序算法。算法描述如下(假设是升序排序):
O( n 2 n^2 n2)
'''插入排序'''
def InsertionSort(array):
length = len(array)
for i in range(1, length):
pointer, cur = i - 1, array[i]
while pointer >= 0 and array[pointer] > cur:
array[pointer+1] = array[pointer]
pointer -= 1
array[pointer+1] = cur
return array
比较类排序算法。其基本思想是把数据按下标的一定增量分组,对每组使用直接插入排序算法排序,随着增量逐渐减少,每组包含的数越来越多,当增量减至1时,整个文件恰被分成一组,算法终止。算法描述如下(假设是升序排序):
O( n 1.3 n^{1.3} n1.3)
'''希尔排序'''
def ShellSort(array):
length = len(array)
gap = length // 2
while gap > 0:
for i in range(gap, length):
j, cur = i, array[i]
while (j - gap >= 0) and (cur < array[j - gap]):
array[j] = array[j - gap]
j = j - gap
array[j] = cur
gap = gap // 2
return array
比较类排序算法。该算法采用了分治法的思想,将已有序的子序列合并,得到完全有序的序列。算法描述如下(假设是升序排序):
O( n l o g 2 n nlog_2n nlog2n)
'''数组合并'''
def Merge(array_1, array_2):
result = []
while array_1 and array_2:
if array_1[0] < array_2[0]:
result.append(array_1.pop(0))
else:
result.append(array_2.pop(0))
if array_1:
result += array_1
if array_2:
result += array_2
return result
'''归并排序'''
def MergeSort(array):
if len(array) < 2: return array
pointer = len(array) // 2
left = array[:pointer]
right = array[pointer:]
return Merge(MergeSort(left), MergeSort(right))
比较类排序算法。基本思想是通过一次排序将待排序数据分隔成独立的两部分,其中一部分数据均比另一部分的数据小。然后分别对这两部分数据继续进行排序,直到整个序列有序。算法描述如下(假设是升序排序):
O( n l o g 2 n nlog_2n nlog2n)
'''快速排序'''
def QuickSort(array, left, right):
if left >= right:
return array
pivot, i, j = array[left], left, right
while i < j:
while i < j and array[j] >= pivot:
j -= 1
array[i] = array[j]
while i < j and array[i] <= pivot:
i += 1
array[j] = array[i]
array[j] = pivot
QuickSort(array, left, i-1)
QuickSort(array, left+1, right)
return array
比较类排序算法。算法描述如下(假设是升序排序):
O( n l o g 2 n nlog_2n nlog2n)
'''堆化'''
def heapify(array, length, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < length and array[largest] < array[left]:
largest = left
if right < length and array[largest] < array[right]:
largest = right
if largest != i:
array[i], array[largest] = array[largest], array[i]
heapify(array, length, largest)
'''堆排序'''
def HeapSort(array):
length = len(array)
for i in range(length, -1, -1):
heapify(array, length, i)
for i in range(length-1, 0, -1):
array[i], array[0] = array[0], array[i]
heapify(array, i, 0)
return array
非比较类排序算法。算法描述如下(假设是升序排序):
O( n + k n+k n+k)
'''计数排序(假设都是0/正整数)'''
def CountingSort(array):
length = len(array)
max_value = max(array)
count = [0 for _ in range(max_value+1)]
output = [0 for _ in range(length)]
for i in range(length):
count[array[i]] += 1
for i in range(1, len(count)):
count[i] += count[i-1]
for i in range(length):
output[count[array[i]]-1] = array[i]
count[array[i]] -= 1
return output
非比较类排序算法。算法描述如下(假设是升序排序):
O( n + k n+k n+k)
'''桶排序(假设都是整数)'''
def BucketSort(array):
max_value, min_value, length = max(array), min(array), len(array)
buckets = [0 for _ in range(min_value, max_value+1)]
for i in range(length):
buckets[array[i]-min_value] += 1
output = []
for i in range(len(buckets)):
if buckets[i] != 0:
output += [i+min_value] * buckets[i]
return output
非比较类排序算法。其实就是先按最低位排序,然后按照高位排序,直到最高位。算法描述如下(假设是升序排序):
O( n ∗ k n*k n∗k)
'''基数排序(假设都是整数)'''
def RadixSort(array):
max_value = max(array)
num_digits = len(str(max_value))
for i in range(num_digits):
buckets = [[] for k in range(10)]
for j in array:
buckets[int(j / (10 ** i)) % 10].append(j)
output = [m for bucket in buckets for m in bucket]
return output