排序算法依次为:冒泡排序、选择排序、插入排序、shell排序、快速排序
def Bubble_Sort(array):
lengths = len(array)
num = 0
for i in range(lengths-num-1):
j = 0;
while j < lengths-num-1:
if (array[j]>array[j+1]):
array[j],array[j+1] = array[j+1],array[j]
j = j+1
else:
j = j+1
num = num +1;
return (array)
def Selection_Sort(array):
lengths = len(array)
for i in range(lengths-1):
min_array = array.index(min(array[i:lengths]))
array[i],array[min_array] = array[min_array],array[i]
return (array)
def Insertion_Sort(array):
lengths = len(array)
for i in range(1,lengths):
temp = array[i]
j = i-1
while j>=0:
if array[j]>temp:
array[j+1] = array[j]
array[j] = temp
j -= 1
return array
def Shell_Sort(array,n):
lengths = len(array)
interval = int(lengths/n)
id = []
while interval>0:
for i in range(interval):
while (i<lengths):
id.append(i)
i = i+interval
# print(a) Insertion Sort
for j in range(1,len(id)):
temp = array[id[j]]
k = j-1
while k>=0:
if array[id[k]]>temp:
array[id[k+1]] = array[id[k]]
array[id[k]] = temp
k -= 1
id = []
# print(array)
interval = int(interval/n)
return array
def Quick_Sort(array, low, high):
i = low
j = high
if i >= j:
return array
key = array[i]
while i < j:
while i < j and array[j] >= key:
j = j-1
array[i] = array[j]
while i < j and array[i] <= key:
i = i+1
array[j] = array[i]
array[i] = key
Quick_Sort(array, low, i-1)
Quick_Sort(array, j+1, high)
return array
if __name__ == '__main__':
arrays = [-1,-3,-12,0,1,4,5,23,6,26,33,21,34,12,333,89,211,15,13,112,10]
# arrays = [3,2,1,4,7,6,8,9,5]
# print(Bubble_Sort(arrays))
# print(Selection_Sort(arrays))
# print(Insertion_Sort(arrays))
# print(Shell_Sort(arrays,2))
print(Quick_Sort(arrays,0,len(arrays)-1))