def sequentialSearch(alist,item):
pos = 0
found = False
while pos<len(alist) and not found:
if alist[pos] == item:
found =True
else:
pos += 1
return found
def orderedSequentialSearch(alist,item):
pos = 0
found = False
stop = False
while pos<len(alist) and not found and not stop:
if alist[pos] == item:
found = True
else:
if alist[pos] > item:
stop = True
else:
pos += 1
return found
def binarySearch(alist,item):
first = 0
last = len(alist)-1
found = False
while first<=last and not found:
midpoint = (first+last)/2
if alist[midpoint]==item:
found = True
else:
if item<alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return found
def binarySearch(alist,item):
if len(alist) == 0:
return False
else:
midpoint = len(alist)/2
if alist[midpoint] == item:
return True
else:
if item<alist[midpoint]:
return binarySearch(alist[:midSearch],item)
else:
return binarySearch(alist[midpoint+1:],item)
注:递归调用了列表切片,其复杂度为O(K),其中K为切片长度。这使算法时间复杂度稍有增加
二分查找在时间复杂度上优于顺序查找。若一次排序后可进行多次查找,那么排序的开销可以摊薄;若数据经常变动,查找次数相对较少,则可能还是直接使用无序表更优
冒泡排序算法的思路在于对无序表进行多趟比较交换。每趟包括了多次两两相邻比较,
并将逆序的数据项交换位置,最终能将本趟的最大项就位。经过n-1趟比较,实现整表排序。
每趟的过程类似“气泡”在水上不断上浮到水面的过程。
def bubbleSort(alist):
for passnum in range(len(alist)-1,0,-1):
for i in range(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp # <=> alist[i],alist[i+1] = alist[i+1],alist[i]
常见算法可视化
def shortBubbleSort(alist):
exchange = True
passnum = len(alist)-1
while passnum > 0 and exchange:
exchange = False
for i in range(passnum):
if alist[i]>alist[i+1]:
exchange = True
alist[i],alist[i+1] = alist[i+1],alist[i]
passnum = passnum-1
def selectionSort(alist):
for fillslot in range(len(alist)-1,0,-1):
positionOFMAX = 0
for location in range(1,fillslot+1):
if alist[location]>alist[positionOFMAX]:
positionOFMAX = location
temp = alist[fillslot]
alist[fillslot] = alist[positionOFMAX]
alist[positionOFMAX] = temp
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[positon]=alist[position-1]
position -= 1
alist[position] = currentvalue
一句话概括:对子列表进行排序
def shellSort(alist):
sublistcount = len(alist)//2 # 返回整数值
while sublistcount>0:
for startposition in range(sublistcount):
gapInsertionSort(alist,startPosition,sublistcount)
print("after increments of size",sublistcount,
"the list is ",alist)
sublistcount = sublistcount//2
def gapInsertionSort(alist,start,gap):
for i in range(start+gap,len(alist),gap):
currentvalue = alist[i]
position = i
while position>=gap and alist[position-gap]>currentvalue:
alist[positon] = alist[position-gap]
position -= gap
alist[position] = currentvalue
def mergeSort(alist):
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i = j = k = 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
def merge_sort(lst):
if len(lst)<=1:
return lst
middle = len(lst)//2
left = merge_sort(lst[:middle])
right = merge_sort(lst[middle:])
merged = []
while left and right:
if left[0] <= right[0]:
merged.append(left.pop(0))
else:
merged.append(right.pop(0))
merged.extend(right if right else left)
return merged
思路: 依据一个中值数据项将数据表分为两半,每部分分别进行快速排序(递归)
代码:
def quickSort(alit):
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
done = False
while not done:
while leftmark<=rightmark and \
alist[leftmark]<=pivotvalue:
leftmark +=1
while alist[rightmark]>=pivotvalue and \
rightmark>=leftmark:
rightmark -=1
if rightmark<leftmark:
done=True
else:
temp = alist[leftmark]
alist[leftmark]= alist[rightmark]
alist[rightmark] = temp
temp = alist[first]
alist[first] = alist[rightmark]
alist[rightmark] = temp
return rightmark
未完待续。。。