Python实现冒泡排序、选择排序、插入排序、快速排序、归并排序、二分法查找算法(基于《算法导论》伪代码)

为加深对各种基础排序算法的理解,我基于Thomas H. Cormen等《算法导论》中的伪代码,用Python实现了冒泡排序、选择排序、插入排序、快速排序、归并排序、二分法查找算法。


具体算法如下:


冒泡排序:

Python实现冒泡排序、选择排序、插入排序、快速排序、归并排序、二分法查找算法(基于《算法导论》伪代码)_第1张图片

def bubbleSort(alist):
	for passnum in range(0, len(alist) - 1, 1):
		for i in range(0, len(alist) - passnum - 1, 1):
			if (alist[i] > alist[i+1]):
				tmp = alist[i+1]
				alist[i+1] = alist[i]
				alist[i] = tmp
				
alist = [54,26,93,17,77,31,44,55,20]
bubbleSort(alist)
print(alist)




选择排序

Python实现冒泡排序、选择排序、插入排序、快速排序、归并排序、二分法查找算法(基于《算法导论》伪代码)_第2张图片

def selectionSort(alist):
	for i in range(0, len(alist), 1):
		min = alist[i]
		index = i
		for j in range(i, len(alist), 1):
			if ( alist[j] < min):
				min = alist[j]
				index = j
		tmp = alist[i]
		alist[i] = min
		alist[index] = tmp

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


插入排序

Python实现冒泡排序、选择排序、插入排序、快速排序、归并排序、二分法查找算法(基于《算法导论》伪代码)_第3张图片

def insertionSort(alist):
	for i in range(1, len(alist), 1):
		j = i - 1
		key = alist[i]
		while (j >= 0 and alist[j] > key):
			alist[j+1] = alist[j]
			j = j - 1
		alist[j + 1] = key 
				
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)


快速排序



	
from math import floor 
	
def partitions(alist, p, r):
	
	print ('start: ' + str(p))
	print ('end: ' + str(r))
	
	i = p - 1
	pivot = alist[r]
	for j in range(p, r, 1):
		if (alist[j] < pivot):
			i = i + 1
			tmp = alist[j]
			alist[j] = alist[i]
			alist[i] = tmp
	tmp = alist[r]
	alist[r] = alist[i + 1]
	alist[i + 1] = tmp
	print ('q: ' + str(i + 1))
	return i + 1
	
	
def quickSort(alist, p, r):
	if (p < r):
		q = partitions(alist, p, r)
		quickSort(alist, p, q - 1)
		quickSort(alist, q + 1, r)
		
alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist, 0, 8)
print(alist)

# notice the range(0, 10, 1) is {0,1,2,3,4,5,6,7,8,9} not include 10 


归并排序

Python实现冒泡排序、选择排序、插入排序、快速排序、归并排序、二分法查找算法(基于《算法导论》伪代码)_第4张图片

Python实现冒泡排序、选择排序、插入排序、快速排序、归并排序、二分法查找算法(基于《算法导论》伪代码)_第5张图片

	
from math import floor 
	
def merge(alist, p, q, r):
	
	print ('start: ' + str(p))
	print ('q: ' + str(q))
	print ('end: ' + str(r))
	
	n1 = q - p + 1
	n2 = r - q
	left = [0]*(n1 + 1)
	right = [0]*(n2 + 1)
	for i in range(0, n1, 1):
		left[i] = alist[p + i]
	for j in range(0, n2, 1):
		right[j] = alist[q + j + 1]
	left[n1] = 100000
	right[n2] = 100000
	i = 0
	j = 0
	for k in range(p, r + 1, 1):
		if (left[i] <= right[j]):
			alist[k] = left[i]
			i = i + 1
		else:
			alist[k] = right[j]
			j = j + 1
	
def mergeSort(alist, p, r):
	if (p < r):
		q = floor((p + r)/2)
		mergeSort(alist, p, q)
		mergeSort(alist, q + 1, r)
		merge(alist, p, q, r)
		
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist, 0, 8)
print(alist)

# notice the range(0, 10, 1) is {0,1,2,3,4,5,6,7,8,9} not include 10 



二分查找

from math import floor

def binarySearch(alist, n, low, high):
	if (low < high):
		mid = floor((high + low)/2)
		if (alist[mid] > n):
			binarySearch(alist, n, low, mid)
		else:
			binarySearch(alist, n, mid + 1, high)
		if (alist[mid] == n):
			print (mid)
			return mid
				
alist = [1,2,3,4,5,6,7,8]
binarySearch(alist, 6, 0, 7)




你可能感兴趣的:(Algorithm)