闲来无事,借着写排序算法实现一遍复习一下python的语法~
class Sort(object):
"""
quickSort: 快速排序
最优/平均时间复杂度O(nlogn) 最差时间复杂度O(n^2) 最优空间复杂度O(logn) 最差空间复杂度O(n)
不稳定排序
heapSort: 堆排序
最优/最差/平均时间复杂度O(nlogn) 空间复杂度O(1)
不稳定排序
insertSort: 插入排序
最优时间复杂度 O(n) 平均/最差时间复杂度O(n^2) 空间复杂度O(1)
稳定排序
selectSort: 选择排序
时间复杂度 O(n^2) 空间复杂度O(1)
不稳定排序
bubbleSort: 冒泡排序
最优时间复杂度 O(n) 最差/平均时间复杂度O(n^2) 空间复杂度O(1)
稳定排序
mergeSort: 归并排序
时间复杂度O(nlogn) 空间复杂度O(n)
稳定排序
"""
def __init__(self, array):
self.array = array
def __repr__(self):
return str(self.array)
def __swap(self, index_a, index_b):
self.array[index_a], self.array[index_b] = self.array[index_b], self.array[index_a]
def __Partition(self, low, high):
"""
Split Array with pivot,
:param low: int
the start index of array
:param high: int
the end index of array
:return: int
index of pivot
"""
pivot = self.array[low]
while(low < high):
while(low < high and self.array[high] > pivot):
high -= 1
self.array[low] = self.array[high]
while(low < high and self.array[low] < pivot):
low += 1
self.array[high] = self.array[low]
self.array[low] = pivot
return low
def quickSort(self, low, high):
if low < high:
mid = self.__Partition(low, high)
self.quickSort(low, mid - 1)
self.quickSort(mid + 1, high)
def __adjustDown(self, low, high):
i = low
j = 2 * i
while(j <= high):
if j + 1 <= high and self.array[j + 1] > self.array[j]:
j += 1
if self.array[j] > self.array[i]:
self.__swap(i, j)
i = j
j = 2 * i
else:
break
def __createHeap(self, low, high):
for fa_index in range(int(high / 2), low - 1, -1):
self.__adjustDown(low, fa_index)
def __pop_heap(self, high):
self.__swap(0, high)
self.__adjustDown(0, high - 1)
def heapSort(self, low, high):
self.__createHeap(low, high)
for now in range(high, low, -1):
self.__pop_heap(now)
def __merge(self, low_L, low_R, high_L, high_R):
"""
merge two array with ascending order
:param low_L: int
low index of array_1
:param low_R: int
high index of array_1
:param high_L: int
low index of array_2
:param high_R:
high index of array_2
:return:
None
"""
order_array = []
i, j = low_L, high_L
while(i <= low_R and j <= high_R):
if(self.array[i] < self.array[j]):
order_array.append(self.array[i])
i += 1
else:
order_array.append(self.array[j])
j += 1
while(i <= low_R):
order_array.append(self.array[i])
i += 1
while(j <= high_R):
order_array.append(self.array[j])
j += 1
for member in order_array:
self.array[low_L] = member
low_L += 1
def mergeSort(self, low, high):
if(low < high):
mid = int((low + high) / 2)
self.mergeSort(low, mid)
self.mergeSort(mid + 1, high)
self.__merge(low, mid, mid + 1, high)
def bubbleSort(self, low, high):
array_length = len(self.array)
for empty in range(high, low, -1): # empty 为插入的位置
flag = False
for j in range(low, empty):
if(self.array[j] > self.array[j + 1]):
self.__swap(j, j + 1)
flag = True
if flag == False: break
def insertSort(self, low, high):
for index in range(low + 1, high + 1):
# 插入过程
temp = self.array[index]
while(index >= 1 and temp < self.array[index - 1]):
self.array[index] = self.array[index - 1]
index -= 1
self.array[index] = temp
def selectSort(self, low, high):
for empty in range(low, high):
# empty : 当前应当被排序好的位置
min_index = empty
for index in range(empty + 1, high + 1):
# for寻找区间 (empty, high] 之间最小的数, 放入empty中
if self.array[index] < self.array[min_index]:
min_index = index
self.__swap(empty, min_index)
# 测试代码
if __name__ == '__main__':
array = [int(i) for i in "9365412780"]
sort = Sort(array)
sort.selectSort(0, len(sort.array) - 1)
# sort.insertSort(0, len(sort.array) - 1)
# sort.quickSort(0, len(sort.array) - 1)
# sort.heapSort(0, len(sort.array) - 1)
# sort.bubbleSort(0, len(sort.array) - 1)
print(sort.array)