排序
冒泡排序
def Bubblesort(List):
for i in range(0,len(List)):
for j in range(0,len(List)-i-1):
if List[j]>List[j+1]:
temp=List[j+1]
List[j+1]=List[j]
List[j]=temp
return List
if __name__ == "__main__":
List = Bubblesort(list(map(int,input().split())))
print(List)
插入排序
def insertsort(List):
for i in range(0,len(List)-1):
for j in range(i,0,-1):
if List[j]<List[j-1]:
temp=List[j-1]
List[j-1]=List[j]
List[j]=temp
return List
if __name__ == "__main__":
List = insertsort(list(map(int,input().split())))
print(List)
选择排序
def selectsort(List):
for i in range(0,len(List)-1):
min_index=i
for j in range(i,len(List)):
if List[j]<List[min_index]:
min_index=j
temp=List[i]
List[i]=List[min_index]
List[min_index]=temp
return List
if __name__ == "__main__":
List = selectsort(list(map(int,input().split())))
print(List)
快速排序
def sort(list,l,r):
key=list[l]
while l<r:
while l<r and list[r]>=key:
r-=1
if l<r:
list[l],list[r]=list[r],list[l]
else:
break
while l<r and list[l]<key:
l+=1
if l<r:
list[r],list[l]=list[l],list[r]
else:
break
list[l]=key
return l
def quicksort(list,left,right):
if left<right:
key=sort(list,left,right)
quicksort(list,left,key-1)
quicksort(list,key+1,right)
return list
if __name__ == "__main__":
# list=list(map(int, input().split()))
list=[6,1,2,7,9,3,4,5,10,8]
List = quicksort(list,0,len(list)-1)
print(List)
归并排序
def mergesort(list):
if len(list)<=1:
return list
num=len(list)//2
left=mergesort(list[:num])
right=mergesort(list[num:])
return merge(left,right)
def merge(left,right):
l,r=0,0
result=[]
while l<len(left) and r<len(right):
if left[l]<right[r]:
result.append(left[l])
l+=1
else:
result.append(right[r])
r+=1
result = result + left[l:]
result = result + right[r:]
return result
if __name__ == "__main__":
list=[6,1,2,7,9,3,4,5,10,8]
# List = mergesort(list(map(int,input().split())))
List = mergesort(list)
print(List)
希尔排序
def shellsort(list):
step=len(list)//2
while step>0:
for i in range(step,len(list)-1):
while i>=step and list[i]<list[i-step]:
temp=list[i-step]
list[i-step]=list[i]
list[i]=temp
i=i-step
step=step//2
return list
if __name__ == "__main__":
List = shellsort(list(map(int,input().split())))
print(List)
查找
顺序查找
def shunxu_search(list,key):
length=len(list)
for i in range(length):
if list[i]==key:
return i
return False
if __name__ == "__main__":
data=list(map(int,input().split()))
key=int(input())
mid =shunxu_search(data,key)
print(mid)
二分查找
def erfen_search(list,key):
low=0
high=len(list)-1
while low<high:
mid=(low+high)//2
if list[mid]<key:
low=mid+1
elif list[mid]>key:
high=mid-1
else:
return mid
return False
if __name__ == "__main__":
data=list(map(int,input().split()))
key=int(input())
mid =erfen_search(data,key)
print(mid)
插值查找
def chazhi_search(list,key):
low=0
high=len(list)-1
while low<high:
mid=low+int((key-list[low])/(list[high]-list[low])*(high-low))
if list[mid]>key:
high=mid-1
elif list[mid]<key:
low=mid+1
else:
return mid
return False
if __name__ == "__main__":
data=list(map(int,input().split()))
key=int(input())
mid =chazhi_search(data,key)
print(mid)
斐波那契查找
def fib_search(list,key):
# write code here
Fib = [0,1]
length=len(list)
if length <= 1:
return None
else:
for i in range(2,length+1):
if i<length:
fib=Fib[i-2]+Fib[i-1]
Fib.append(fib)
low=0
high=length-1
count = 0
while length > Fib[count]:
count+=1
while low <= high:
mid = low + Fib[count - 1]
if list[mid] > key:
high = mid - 1
count = count - 1
elif list[mid] < key:
low = mid + 1
count = count - 2
else:
return mid
return False
if __name__ == "__main__":
data=list(map(int,input().split()))
key=int(input())
mid =fib_search(data,key)
print(mid)
哈希查找
def hash_search(list, key):
size=len(list)
elem = [None for i in range(size)]
i=0
while i<size:
address=list[i] % size
while elem[address]:
address = (address+1)%size
elem[address] = list[i]
i+=1
star =index= key % size
while elem[index] != key:
index = (index + 1) % size
if elem[index]==None or star==index:
return False
return index
if __name__ == "__main__":
data=list(map(int,input().split()))
key=int(input())
print(hash_search(data,key))
排序算法复杂度
排序算法 | 最差时间复杂度 | 平均时间复杂度 | 空间复杂度 | 是否稳定 |
---|---|---|---|---|
冒泡排序 | O ( n 2 ) O(n^2) O(n2) | O ( n 2 ) O(n^2) O(n2) | O ( 1 ) O(1) O(1) | 稳定 |
插入排序 | O ( n 2 ) O(n^2) O(n2) | O ( n 2 ) O(n^2) O(n2) | O ( 1 ) O(1) O(1) | 稳定 |
选择排序 | O ( n 2 ) O(n^2) O(n2) | O ( n 2 ) O(n^2) O(n2) | O ( 1 ) O(1) O(1) | 不稳定 |
快速排序 | O ( n 2 ) O(n^2) O(n2) | O ( n l o g 2 n ) O(nlog_2 n) O(nlog2n) | O ( n l o g 2 n ) O(nlog_2 n) O(nlog2n) | 不稳定 |
归并排序 | O ( n l o g 2 n ) O(nlog_2 n) O(nlog2n) | O ( n l o g 2 n ) O(nlog_2 n) O(nlog2n) | O ( n ) O(n) O(n) | 稳定 |
希尔排序 | O ( n 2 ) O(n^2) O(n2) | O ( n ) O(n) O(n)~ O ( n 2 ) O(n^2) O(n2) | O ( 1 ) O(1) O(1) | 不稳定 |
查找算法复杂度
排序算法 | 时间复杂度 |
---|---|
顺序查找 | O ( n ) O(n) O(n) |
二分查找 | O ( n l o g 2 n ) O(nlog_2 n) O(nlog2n) |
插值查找 | O ( l o g 2 ( l o g 2 n ) ) O(log_2(log_2n)) O(log2(log2n)) |
斐波那契查找 | O ( n l o g 2 n ) O(nlog_2 n) O(nlog2n) |
哈希查找 | O ( 1 ) O(1) O(1) |
如有错误,欢迎批评指出,谢谢!