python 算法
冒泡
# def buttle_sort(li):
# for i in range(len(li)-1):
# for j in range(len(li)-i-1):
# if li[j] > li[j+1]:
# li[j],li[j+1] = li[j+1],li[j]
# li = [3,6,4,8,5,3]
# buttle_sort(li)
# print(li)
# 二分查找
# def func(alist, item):
# low = 0
# high = len(alist)-1
# n = 0
# while low <= high:
# mid = int((low + high)/2)
# n += 1
# if alist[mid]==item:
# return mid
# if alist[mid]<item:
# low = mid + 1
# else:
# high = (mid-1)
# return None
#
# m=[1,2,3,4,8,9,11,12,14,18,19,20,28]
# print(func(m,14))
#选择排序
# def func(alist):
# # x就是1到传过来的这个参数的总长度
# for x in range(0,len(alist)):
#
# # min_num 就等于每次循环的小标对应数字
# min_num = alist[x]
#
# # 循环小标x+1 到所有下标长度
# for i in range(x+1,len(alist)):
# # 如果下标i的参数大于min_num
# if alist[i] < min_num:
# # 互换位置(参数互换)
# alist[i],min_num = min_num,alist[i]
# alist[x] = min_num
# return alist
#
# print(func([1,55,22,66,44,22,88]))
# 斐波那契额
nterms = int(input("你需要几项?"))
n1 = 0
n2 = 1
count = 2
if nterms <= 0:
print("请输入一个正整数。")
elif nterms == 1:
print("斐波那契数列:")
print(n1)
else:
print("斐波那契数列:")
print(n1, ",", n2, end=" , ")
while count < nterms:
nth = n1 + n2
print(nth, end=" , ")
n1 = n2
n2 = nth
count += 1