十大经典排序算法------python实现
排序算法模块
def list_clone(old_list):
new_list = list(old_list)
return new_list
def arrSwap(arr, x1, x2):
temp = arr[x1]
arr[x1] = arr[x2]
arr[x2] = temp
def maxValueOfList(arr):
res = arr[0]
for i in arr:
if i > res:
res = i
return res
def minValueOfList(arr):
res = arr[0]
for i in arr:
if i < res:
res = i
return res
# --------------------------------bubble sort-------------------------------
def BubbleSort(arr):
if len(arr) < 2:
return arr
for i in range(0, len(arr)):
for j in range(len(arr) - 1 - i):
if arr[j + 1] < arr[j]:
temp = arr[j + 1]
arr[j + 1] = arr[j]
arr[j] = temp
# ------------------------------selection sort--------------------------------
def SelectSort(arr):
if len(arr) < 2:
return arr
for i in range(0, len(arr)):
minIndex = i
for j in range(i, len(arr)):
if arr[j] < arr[minIndex]:
minIndex = j
arrSwap(arr, i, minIndex)
# ----------------------------insertion sort--------------------------------
def InsertSort(arr):
for i in range(0, len(arr)):
current = arr[i]
index = i - 1
while index >= 0 and arr[index] > current:
arr[index + 1] = arr[index]
index -= 1
arr[index + 1] = current
# --------------------------------shell sort--------------------------------
def ShellSort(arr):
interval = int(len(arr) / 2)
while interval > 0:
for i in range(interval, len(arr), 1):
current = arr[i]
index = i - interval
while index >= 0 and arr[index] > current:
arr[index + interval] = arr[index]
index -= interval
arr[index + interval] = current
interval = int(interval / 2)
# --------------------------------quick sort--------------------------------
def myPartition(arr, low, high):
keyIndex = -1
mid = int((low + high) / 2)
if arr[low] >= arr[mid] and arr[low] <= arr[high]:
keyIndex = low
elif arr[mid] >= arr[low] and arr[mid] <= arr[high]:
keyIndex = mid
else:
keyIndex = high
arrSwap(arr, keyIndex, low)
key = arr[low]
left = low + 1
right = high
while left <= right:
while left <= right and arr[left] <= key:
left += 1
while left <= right and arr[right] >= key:
right -= 1
if left < right:
arrSwap(arr, left, right)
arrSwap(arr, low, right)
return right
# index
def QuickSort(arr, low, high):
if low < high:
index = myPartition(arr, low, high)
QuickSort(arr, low, index - 1)
QuickSort(arr, index + 1, high)
# --------------------------------merge sort--------------------------------
def merger(arr, low, mid, high):
# helper = copy.deepcopy(arr)
helper = list_clone(arr)
left = low
right = mid + 1
index = low
while left <= mid and right <= high:
if helper[left] <= helper[right]:
arr[index] = helper[left]
left += 1
else:
arr[index] = helper[right]
right += 1
index += 1
while left <= mid:
arr[index] = helper[left]
index += 1
left += 1
# index
def MergeSort(arr, low, high):
if low < high:
mid = int((low + high) / 2)
MergeSort(arr, low, mid)
MergeSort(arr, mid + 1, high)
merger(arr, low, mid, high)
# --------------------------------Heap sort--------------------------------
def BuildMaxHeap(arr):
for i in range(int(len(arr) / 2) - 1, -1, -1):
AdjustHeap(arr, i, len(arr))
def AdjustHeap(arr, index, size):
left = index * 2 + 1
right = index * 2 + 2
if left >= size:
return
maxIndex = left
if right >= size:
pass
else:
if arr[right] > arr[left]:
maxIndex = right
if arr[index] >= arr[maxIndex]:
return
arrSwap(arr, maxIndex, index)
AdjustHeap(arr, maxIndex, size)
def HeapSort(arr):
BuildMaxHeap(arr)
for x in range(len(arr), 0, -1):
arrSwap(arr, 0, x - 1)
AdjustHeap(arr, 0, x - 1)
# ---------------------------counting sort--------------------------------
def CountSort(arr):
maxValue = maxValueOfList(arr)
minValue = minValueOfList(arr)
helpArr = []
distance = 0 - minValue
for x in range(minValue, maxValue + 1):
helpArr.append(0)
for x in arr:
helpArr[x + distance] += 1
i = 0
index = 0
while index < len(arr):
if helpArr[i] != 0:
arr[index] = i - distance
helpArr[i] -= 1
index += 1
else:
i += 1
# ---------------------------bucket sort--------------------------------
def BucketSort(arr):
# Linked List Array index =
# Value * NUMBER_OF_ELEMENTS / (MAXIMUN_ARRAY_VALUE + 1)
bucketlist = []
max_value = maxValueOfList(arr)
for x in range(len(arr)):
bucketlist.append([])
for value in arr:
bucket_index = int(value * len(arr) / (max_value + 1))
if len(bucketlist[bucket_index]) == 0:
bucketlist[bucket_index].append(value)
else:
for i in range(len(bucketlist[bucket_index])):
if i == len(bucketlist[bucket_index]) - 1 \
and value > bucketlist[bucket_index][i]:
bucketlist[bucket_index].append(value)
if value <= bucketlist[bucket_index][i]:
bucketlist[bucket_index].insert(i, value)
break
else:
pass
# Collect
i = 0
index = 0
for bucket in bucketlist:
# print(bucket)
for x in bucket:
# print(x)
arr[index] = x
index += 1
# ---------------------------radix sort--------------------------------
def RadixSort(arr):
maxDigit = 0
bucketlist = []
for i in range(10):
bucketlist.append([])
v = 0
max_value = maxValueOfList(arr)
v = max_value
while v != 0:
v /= 10
maxDigit += 1
digit = 0
while digit < maxDigit:
for bucket in bucketlist:
bucket.clear()
for x in arr:
index = int(x / (10 ** digit)) % 10
bucketlist[index].append(x)
digit += 1
# collect
i = 0
for bucket in bucketlist:
for value in bucket:
arr[i] = value
i += 1
def main():
pass
if __name__ == '__main__':
main()
测试代码
RandomList是随机产生一个长度为5000,数值为1-10000的列表,为了让所有算法的测试用例一致,所以在每个排序算法前面都对产生随机列表进行拷贝
import MySortModule
import functools
import random
import time
random_list = [random.randint(0, 10000) for x in range(5000)]
def timer(func):
functools.wraps(func)
def wrapTheFunction(sort_func, args_list):
start_time = time.time()
func(sort_func, args_list)
# print(args_list)
end_time = time.time()
return sort_func.__name__ + " running time is " + \
str(round(end_time - start_time, 5)) + "s"
return wrapTheFunction
# quick & merge sort Function decorator
def timer2(func):
functools.wraps(func)
def wrapTheFunction(sort_func, args_list, s, e):
start_time = time.time()
func(sort_func, args_list, s, e)
end_time = time.time()
return sort_func.__name__ + " running time is " + \
str(round(end_time - start_time, 5)) + "s"
return wrapTheFunction
@timer
def test(sort_func, args_list):
# test = timer(test)
sort_func(args_list)
@timer2
def test2(sort_func, args_list, start, end):
sort_func(args_list, start, end)
def main():
print(test(MySortModule.BubbleSort, random_list.copy()))
print(test(MySortModule.SelectSort, random_list.copy()))
print(test(MySortModule.InsertSort, random_list.copy()))
print(test(MySortModule.ShellSort, random_list.copy()))
print(test2(MySortModule.QuickSort, random_list.copy(), 0, len(random_list) - 1))
print(test2(MySortModule.MergeSort, random_list.copy(), 0, len(random_list) - 1))
print(test(MySortModule.HeapSort, random_list.copy()))
print(test(MySortModule.CountSort, random_list.copy()))
print(test(MySortModule.BucketSort, random_list.copy()))
print(test(MySortModule.RadixSort, random_list.copy()))
if __name__ == '__main__':
main()