冒泡 选择 二分法 算法 排序算法

#冒泡排序;
# def bubble_sort(alist):
# n=len(alist) #计算序列长度
# for i in range(n-1): #遍历无序序列;
# for j in range(n-1-i): #构建循环比较相邻的元素条件;
# if alist[j]>alist[j+1]: #如果前一个比后面相邻元素大(升序);
# alist[j],alist[j+1]=alist[j+1],alist[j] #交换他们两个;
# if __name__ == '__main__':
# alist=[99,54,78,21,33,11,60]
# bubble_sort(alist)
# print(alist)


#选择排序;
# def select_sort(alist):
# n=len(alist)
# for i in range(n-1): #需要进行n-1次操作
# min_index=i #记录最小位置
# for j in range(i+1,n): #从i+1位置到末尾选择出最小数据
# if alist[min_index]>alist[j]:
# min_index=j
# if min_index!=i: #如果选择出的位置不再正确位置,进行交换
# alist[i],alist[min_index]=alist[min_index],alist[i]
# if __name__ == '__main__':
# alist=[44,33,100,77,88,1,32,89,32]
# select_sort(alist)
# print(alist)

# def select_sort(alist):
# n=len(alist)
# for i in range(n-1): #需要进行n-1次操作
# min_index=i #记录最小位置
# for j in range(i+1,n): #遍历无序列表
# if alist[min_index]>alist[j]: #判断当前数值是否小于最小值,如果小于,记录索引
# min_index=j #记录索引
# if min_index!=i: #判断min_index索引是否相同,不相同,做数值交换
# alist[i],alist[min_index]=alist[min_index],alist[i] #不相同做数值交换
# if __name__ == '__main__':
# alist=[22,34,11,56,43,99,0]
# select_sort(alist)
# print(alist)



#二分法(非递归)
# def binary_search(alist,item):
# n=len(alist) #计算序列长度
# first=0 #构造初始索引
# last=n-1 #构造结束索引
# while first<=last: #构造循环查找条件
# mid=(first+last)//2 #构造折半查找的中间值
# if alist[mid]==item: #如果元素等于中间值的情况
# return True
# elif alist[mid]>item: #如果元素小于中间值的情况
# last=mid+1
# else: #如果元素大于中间值的情况
# first=mid-1
# else: #如果整个序列中查找一遍都没找到,返回False
# return False
# if __name__ == '__main__':
# alist=[11,33,21,43,66,54,777]
# print(binary_search(alist,66))



#二分法(递归)
# def binary_search(alist,item):
# if alist==[]: #判断是否为空列表
# return False # 如果为空则返回未找到
# else: #否则,构造中间值
# mid=len(alist)//2
# if alist[mid]==item: #如果元素等于中间值的情况
# return True
# elif alist[mid]>item: #如果元素小于中间值的情况
# return binary_search(alist[:mid],item)
# else: #如果元素大于中间值的情况
# return binary_search(alist[mid+1:],item)
#
# if __name__ == '__main__':
# alist = [11, 33, 43, 66, 777]
# print(binary_search(alist,11))


"""
有无序序列alist=[22,1,3,4,55,66,7,8,9,10]
使用二分法查找元素22
"""
# class Search(object):
# def bubble_sort(self):
# n=len(alist)
# for i in range(n-1):
# for j in range(n-1-i):
# if alist[j]>alist[j+1]:
# alist[j],alist[j+1]=alist[j+1],alist[j]
# print(alist)
# def binary_search(self,item):
# n = len(alist)
# first = 0
# last = n - 1
# while first <= last:
# mid = (first + last) // 2
# if alist[mid] == item:
# return True
# elif alist[mid] > item:
# last = mid - 1
# else:
# first = mid + 1
# else:
# return False
#
# if __name__ == '__main__':
# alist = [22, 1, 3, 4, 55, 66, 7, 8, 9, 10]
# search=Search()
# search.bubble_sort()
# print(search.binary_search(22))


# class Search(object):
# def __init__(self):
# self.alist = [5,44,99,8,101,1,22,33]
# def bubble_sort(self):
# print(self.alist)
# n = len(self.alist)
# for i in range(n-1):
# for j in range(n-1-i):
# if self.alist[j] > self.alist[j+1]:
# self.alist[j],self.alist[j+1] = self.alist[j+1],self.alist[j]
# print(self.alist)
# def binary_search(self,item):
# a = 0
# b = len(self.alist) - 1
# while a <= b:
# mid = (a + b) // 2
# if self.alist[mid] == item:
# return True
# elif self.alist[mid] > item:
# b = mid - 1
# else:
# a = mid + 1
# return False
#
# if __name__ == '__main__':
# aaa = Search()
# aaa.bubble_sort()
# print(aaa.binary_search(44))






# import time
# start_time=time.time
# for a in range(1001):
# for b in range(1001):
# for c in range(1001):
# if a+b+c==1000 and a**2+b**2==c**2:
# print("a=%d,b=%d,c=%d"%(a,b,c))
# end_time=time.time()
# print("最终的执行时间为:%f"%(end_time-start_time))


"""
a,b,c三个数
1.a+b+c=1000 (0--1000)
2.a**2+b**2=c**2
求a,b,c可能的组合
"""
# import time
# start_time=time.time()
# for a in range(0,1001):
# for b in range(0,1001):
# for c in range(0,1001):
# if a+b+c == 1000 and a**2+b**2 == c**2:
# print(" a = %d , b = %d , c = %d "%(a,b,c))
# end_time=time.time()
# print("demo执行共耗时:%f"%(end_time-start_time))


# import time
# start_time=time.time()
# for a in range(0,1001):
# for b in range(0,1001):
# c=1000-a-b
# if a+b+c == 1000 and a**2+b**2 == c**2:
# print(" a = %d , b = %d , c = %d "%(a,b,c))
# end_time=time.time()
# print("demo执行共耗时:%f"%(end_time-start_time))


# def func():
# for a in range(0,1001):
# for b in range(0,1001):
# c=1000-a-b
# if a**2+b**2 == c**2:
# print(" a = %d , b = %d , c = %d "%(a,b,c))
# import timeit #python内置性能测试模块;
#
# time01=timeit.Timer(stmt="func()",setup="from __main__ import func")
# #Timer(目标函数从哪去找这个函数)
# print(time01.timeit(1))


# import timeit #查看demo运行时间
# def t1(): #列表拼接;
# l=[]
# for i in range(1000):
# l=l+[i]
#
# def t2(): #append;
# l=[]
# for i in range(1000):
# l.append(i)
#
# def t3(): #列表推导式;
# l=[i for i in range(1000)]
#
# def t4(): #强制转换;
# l=list(range(1000))
#
# def t5(): #insert;
# l=[]
# for i in range(10000):
# l.insert(0,i)
#
#
# from timeit import Timer
# timer1=Timer("t1","from __main__ import t1")
# print("列表拼接:",timer1.timeit(number=10000),"seconds")
#
# timer2=Timer("t2()","from __main__ import t2")
# print("append",timer2.timeit(number=10000),"seconds")
#
# timer3=Timer("t3()","from __main__ import t3")
# print("列表推导式",timer3.timeit(number=10000),"seconds")
#
# timer4=Timer("t4()","from __main__ import t4")
# print("list强转",timer4.timeit(number=10000),"seconds")
#
# timer5 = timeit.Timer(stmt='t5()', setup='from __main__ import t5')
# print('insert:',timer5.timeit(1000))




# import timeit # 查看demo运行时间
# def t1(): # 列表拼接
# ll = []
# for i in range(10000):
# ll += [i]
#
# def t2(): # append
# ll = []
# for i in range(10000):
# ll.append(i)
#
# def t3(): # 列表推导式
# ll = [i for i in range(10000)]
#
# def t4(): # 强制转换
# ll = list(range(10000))
#
# def t5(): # insert
# ll = []
# for i in range(10000):
# ll.insert(0,i)
#
# timer1 = timeit.Timer(stmt='t1()', setup='from __main__ import t1')
# timer2 = timeit.Timer(stmt='t2()', setup='from __main__ import t2')
# timer3 = timeit.Timer(stmt='t3()', setup='from __main__ import t3')
# timer4 = timeit.Timer(stmt='t4()', setup='from __main__ import t4')
# timer5 = timeit.Timer(stmt='t5()', setup='from __main__ import t5')
#
# print('列表拼接:',timer1.timeit(1000))
# print('append:',timer2.timeit(1000))
# print('列表推导式:',timer3.timeit(1000))
# print('list强转:',timer4.timeit(1000))
# print('insert:',timer5.timeit(1000))
























上一篇:Blockly-图形化编程工具


下一篇:Git-cheat-sheet