数据结构与算法

2021-05-04 04:29

阅读:472

标签:pos   app   last   i+1   xtend   双链表   turn   快速排序   rem   

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(self.length()-1):
            self.append(item)
        else:  
            pre=self.__head
            count=0
            while countalist[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]=1:    
        for j in range(gap,n):
            i=j
            while i>0:
                if alist[i]=last:
        return
    mid_value=alist[first]
    low=first
    high=last
    while low=mid_value:
            high-=1
        alist[low]=alist[high]
        while low

  

数据结构与算法

标签:pos   app   last   i+1   xtend   双链表   turn   快速排序   rem   

原文地址:https://www.cnblogs.com/Turing-dz/p/13196122.html


评论


亲,登录后才可以留言!