数据结构与算法
import time start_time=time.time() 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,b,c:%d,%d,%d"%(a,b,c)) end_time=time.time() print("times:%d"%(end_time-start_time)) #timeit模块 from timeit import Timer#从timeit模块中导入Timer类 def t1(): li=[] for i in range(10000): li.append(i) def t2(): li=[] for i in range(10000): li+=[i] def t3(): [i for i in range(10000)] def t4(): list(range(10000)) def t5(): li=[] for i in range(10000): li.extend([i]) timer1=Timer('t1()','from __main__ import t1')#构造对象,第一个是调用函数的名字,第二个是调用函数的条件 print("append:",timer1.timeit(1000))#使用对象的timeit方法,参数是函数跑多少次 timer2=Timer('t2()','from __main__ import t2') print("+:",timer2.timeit(1000)) timer3=Timer('t3()','from __main__ import t3') print("【】:",timer3.timeit(1000)) timer4=Timer('t4()','from __main__ import t4') print("list:",timer4.timeit(1000)) timer5=Timer('t5()','from __main__ import t5') print("extend:",timer5.timeit(1000)) #single_link_like class Node (object): def __init__(self,elem): self.elem=elem self.next=None class SingleLinkList(object): def __init__(self,node=None): self.__head=node def is_empty(self): return self.__head==None def length(self): cur=self.__head count=0 while cur!=None: count+=1 cur=cur.next return count def travel(self): cur=self.__head while cur!=None: print(cur.elem,end=" ") cur=cur.next def add(self,item):#头插法 node=Node(item) node.next=self.__head self.__head=node def append(self,item):#尾插法 node=Node(item) if self.is_empty(): self.__head=node else: cur=self.__head while cur.next!=None: cur=cur.next cur.next=node def insert(self,pos,item): if pos<=0: self.add(item) elif pos>(self.length()-1): self.append(item) else: pre=self.__head count=0 while count<(pos-1): count+=1 pre=pre.next node=Node(item) node.next=pre.next pre.next=node def remove(self,item): cur=self.__head pre=None while cur!=None: if cur.elem==item: if cur==self.__head: self.__head=cur.next else: pre.next=cur.next else: pre=cur cur=cur.next break def search(self,item): cur=self.__head while cur!=None: if cur.elem==item: return True else: cur=cur.next return False if __name__=="__main__": ll=SingleLinkList() print(ll.is_empty()) print(ll.length()) ll.append(1) ll.append(2) ll.append(3) ll.append(4) ll.append(5) ll.add(8) print(ll.is_empty()) print(ll.length()) ll.insert(4,40) ll.travel() #双链表 class Node(object): def __init__ (self,item): self.elem=item self.next=None self.prev=None class DoubleLinkList(object): def __init__(self,node=None): self.__head=None def is_empty(self): return self.__head is None #栈stack class Stack(object): def __init__(self): self.__list=[] def push(self,item): self.__list.append(item) def pop(self): return self.__list.pop() def peek(self): if self.__list: return self.__list[-1] else: return None def is_empty(self): return self.__list==[] def size(self): return len(self.__list) if __name__=="__main__": s=Stack() s.push(1) s.push(2) s.push(3) s.push(4) print(s.pop()) print(s.pop()) print(s.pop()) print(s.pop()) #队列 class Queue(object): def __init__(self): self.__list=[] def enqueue(self,item):#加元素 self.__list.append(item) def dequeue(self):#减元素 return self.__list.pop(0) def is_empty(self): return self.__lsit==[] def size(self): return len(self.__list) if __name__=="__main__": s=Queue() s.enqueue(1) s.enqueue(2) s.enqueue(3) s.enqueue(4) print(s.dequeue()) print(s.dequeue()) print(s.dequeue()) print(s.dequeue()) #双端队列 class Deque(object): def __init__(self): self.__list=[] def add_front(self,item): self.__list.insert(0,item) def add_rear(self,item): self.__list.append(item) def pop_front(self): return self.__list.pop(0) def pop_rear(self): return self.__list.pop() def is_empty(self): return self.__list==[] def size(self); return len(self.__list) if __name__=="__main__": s=Deque() s.enqueue(1) s.enqueue(2) s.enqueue(3) s.enqueue(4) print(s.dequeue()) print(s.dequeue()) print(s.dequeue()) print(s.dequeue()) #Bubble sort def bubble_sort(alist): for j in range(0,len(alist)-1): count=0 for i in range(0,len(alist)-1-j): if alist[i]>alist[i+1]: alist[i],alist[i+1]=alist[i+1],alist[i] count+=1 if count==0: return #select sort def select_sort(alist): n=len(alist) for j in range(0,n-1): min_index=j for i in range(j+1,n): if alist[min_index]>alist[i]: min_index=i alist[min_index],alist[j]=alist[j],alist[min_index] def insert_sort(alist): n=len(alist) for j in range(1,n): i=j while i>0: if alist[i]<alist[i-1]: alist[i],alist[i-1]=alist[i-1],alist[i] i-=1 else: break #希尔排序 def shell_sort(alist): n=len(alist) gap=n//2 while gap>=1: for j in range(gap,n): i=j while i>0: if alist[i]<alist[i-gap]: alist[i],alist[i-gap]=alist[i-gap],alist[i] i-=gap else: break gap//=2 #快速排序 def quick_sort(alist,first,last): if first>=last: return mid_value=alist[first] low=first high=last while low<high: while low<high and alist[high]>=mid_value: high-=1 alist[low]=alist[high] while low<high and alist[low]<mid_value: low+=1 alist[high]=alist[low] alist[low]=mid_value quick_sort(alist,first,low-1) quick_sort(alist,low+1,last) #递归排序 def merge_sort(alist): n=len(alist) if n <=1: return alist mid =n//2 left_li=merge_sort(alist[:mid]) right_li=merge_sort(alist[mid:]) #将两个有序子序列合并为一个新的整体 left_pointer,right_pointer=0,0 result=[] while left_pointer<len(left_li) and right_pointer<len(right_li): if left_li[left_pointer]<right_li[right_pointer]: result.append(left_li[left_pointer]) left_pointer+=1 else: result.append(right_li[right_pointer]) right_pointer+=1 result+=left_li[left_pointer:] result+=right_li[right_pointer:] return result if __name__=="__main__": li=[54,26,93,17,77,31,44,55,20] print(li) #bubble_sort(li) #select_sort(li) #insert_sort(li) #shell_sort(li) #quick_sort(li,0,len(li)-1) li_2=merge_sort(alist) print(li_2) #二分查找; def binary_search(alist):