#冒泡排序
def maopao(test_list):
for i in range(0,len(test_list)):
for j in range(0,len(test_list)-i-1):
if test_list[j+1] > test_list[j]:
test_list[j],test_list[j+1] = test_list[j+1],test_list[j]
return test_list
#二分法找查找
def Binary_Search(test_list,num):
test_list = sorted(test_list)
print(test_list)
low = 0
high = len(test_list)-1
while low <= high:
mid = int((low+high) / 2)
if num > test_list[mid]:
low = mid + 1
elif num < test_list[mid]:
high = mid - 1
elif num == test_list[mid]:
return mid
#快速排序
def quick_sort(test_list):
if len(test_list) <=1:
return test_list
base_data = test_list[0]
l_list,r_list,m_list = [],[],[base_data]
for i in range(1,len(test_list)):
if test_list[i]>base_data:
r_list.append(test_list[i])
elif test_list[i]<base_data:
l_list.append(test_list[i])
else:
m_list.append(test_list[i])
return quick_sort(l_list) + m_list + quick_sort(r_list)
python 实现简单的算法示例
于 2021-04-01 21:04:16 首次发布