1. 程式人生 > >Python實現經典的排序演算法

Python實現經典的排序演算法

1.氣泡排序

def bubbleSort(alist):
	for p in range(len(alist)-1, 0, -1):
		for i in range(p):
			if alist[i] > alist[i+1]:
				temp = alist[i+1]
				alist[i+1] = alist[i]
				alist[i] = temp

def shortBubbleSort(alist):
	length = len(alist) - 1
	flag = True
	while length > 0 and flag:
		flag = False
		for i in range(length):
			if alist[i] > alist[i+1]:
				flag = True
				temp = alist[i+1]
				alist[i+1] = alist[i]
				alist[i] = temp
		length -= 1


if __name__ == '__main__':
	lst = [54,26,93,17,77,31,44,55,20]
	bubbleSort(lst)
	print(lst)

	lst2 = [54,26,93,17,77,31,44,55,20]
	shortBubbleSort(lst2)
	print(lst2)

2.選擇排序

def selectSort(alist):
	for full in range(len(alist)-1, 0, -1):
		mininumPos = 0
		for i in range(1, full+1):
			if alist[i] > alist[mininumPos]:
				mininumPos = i

		temp = alist[mininumPos]
		alist[mininumPos] = alist[full]
		alist[full] = temp

if __name__ == '__main__':
	lst = [54,26,93,17,77,31,44,55,20]
	bubbleSort(lst)
	print(lst)

	lst2 = [54,26,93,17,77,31,44,55,20]
	shortBubbleSort(lst2)
	print(lst2)

	lst2 = [54,26,93,17,77,31,44,55,20]
	selectSort(lst2)
	print(lst2)

3.插入排序

def insertSort(alist):
	for index in range(1, len(alist)):
		currentvalue = alist[index]
		pos = index

		while pos > 0 and alist[pos-1] > currentvalue:
			alist[pos] = alist[pos - 1]
			pos -= 1

		alist[pos] = currentvalue

if __name__ == '__main__':
	lst = [54,26,93,17,77,31,44,55,20]
	bubbleSort(lst)
	print(lst)

	lst2 = [54,26,93,17,77,31,44,55,20]
	shortBubbleSort(lst2)
	print(lst2)

	lst3 = [54,26,93,17,77,31,44,55,20]
	selectSort(lst3)
	print(lst3)

	lst4 = [54,26,93,17,77,31,44,55,20]
	selectSort(lst4)
	print(lst4)

4.希爾排序

def shellSort(alist):
	sublist = len(alist) // 2
	while sublist > 0:
		for startpos in range(sublist):
			gapInsertSort(alist, startpos, sublist)
		print("after increments of size\n", sublist, 'The list is\n', alist)

		sublist = sublist // 2

def gapInsertSort(alist, startpos, sublist):
	for i in range(startpos+sublist, len(alist), sublist):
		currentvalue = alist[i]
		pos = i

		while pos >= sublist and alist[pos-sublist] > currentvalue:
			alist[pos] = alist[pos-sublist]
			pos -= sublist

		alist[pos] = currentvalue

if __name__ == '__main__':
        lst5 = [54,26,93,17,77,31,44,55,20]
	shellSort(lst5)
	print(lst5)

5.歸併排序

def mergeSort(alist):
	print('Splitting...', alist)
	if len(alist) > 1:
		mid = len(alist) // 2
		lefthalf = alist[:mid]
		righthalf = alist[mid:]
		print('===================')
		print(lefthalf)
		print(righthalf)
		print('===================')
		mergeSort(lefthalf)
		mergeSort(righthalf)

		i, j, k = 0, 0, 0
		while i < len(lefthalf) and j < len(righthalf):
			if lefthalf[i] < righthalf[j]:
				alist[k] = lefthalf[i]
				i += 1
			else:
				alist[k] = righthalf[j]
				j += 1
			k += 1

		while i < len(lefthalf):
			alist[k] = lefthalf[i]
			i += 1
			k += 1
		while j < len(righthalf):
			alist[k] = righthalf[j]
			j += 1
			k += 1

	print('Merging...', alist)

6.快排

def quickSort(alist):
	quickSortHelper(alist, 0, len(alist)-1)

def quickSortHelper(alist, first, last):
	if first < last:

		splitpoint = partition(alist, first, last)
		quickSortHelper(alist, first, splitpoint-1)
		quickSortHelper(alist, splitpoint+1, last)

def partition(alist, first, last):
	pivotvalue = alist[first]

	leftmark = first + 1
	rightmark = last

	flag = False

	while not flag:
		while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
			leftmark += 1
		while leftmark <= rightmark and alist[rightmark] >= pivotvalue:
			rightmark -= 1

		if rightmark < leftmark:
			flag = True
		else:
			temp = alist[leftmark]
			alist[leftmark] = alist[rightmark]
			alist[rightmark] = temp

	temp = alist[first]
	alist[first] = alist[rightmark]
	alist[rightmark] = temp

	return rightmark


if __name__ == '__main__':
	
	print('---------------------------')

	lst7 = [54,26,93,17,77,31,44,55,20]
	quickSort(lst7)
	print('quiictSort', lst7)
	print( lst7==lst5 )