python插入排序和sort时间比较

来源:互联网 发布:网络博客被骗怎么追 编辑:程序博客网 时间:2024/04/30 01:03
# 插入排序函数def insertSort(a):      for i in range(len(a)-1):            for j in range(i+1,len(a)):              if a[i]>a[j]:                  temp = a[i]                  a[i] = a[j]                  a[j] = temp      return a 

下面是自己写的插入排序和自带的sort比较

tb = {} for i in range(0,1000):    tb[i] = {}    tb[i][1] = random.randint(1, 100)    tb[i][2] = random.randint(1, 100)    tb[i][3] = random.randint(1, 100)    tb[i][4] = random.randint(1, 100)    tb[i][5] = random.randint(1, 100)import datetimeprint datetime.datetime.now()cloth = []cloth2 = []def getmax(attr):    for c in tb:        val = tb[c][attr[0]] + tb[c][attr[1]] + tb[c][attr[2]] + tb[c][attr[3]]        lenth = len(cloth)        if not lenth:            cloth.append(val)        else:            for i in range(0, lenth):                if val >= cloth[i]:                    cloth.insert(i,val)                    break                if i == lenth-1:                    cloth.append(val)getmax([1,2,3,4])                    #print clothprint datetime.datetime.now()      def getmax2(attr):    for c in tb:        val = tb[c][attr[0]] + tb[c][attr[1]] + tb[c][attr[2]] + tb[c][attr[3]]        cloth2.append(val)getmax2([1,2,3,4])cloth2.sort()#print clothprint datetime.datetime.now()

下面是结果,python自带的sort函数效率很高

# 000 2015-05-14 17:20:40.328000# 111 2015-05-14 17:20:40.351000# 222 2015-05-14 17:20:40.352000
0 0
原创粉丝点击