Python 排序与查找算法收集

Python 排序与查找算法收集,第1张

概述Python 语言实现几种不同的排序算法,代码来自于老男孩Python全栈开发,学习教程! import random import time import copy import sys def c

Python 语言实现几种不同的排序算法,代码来自于老男孩Python全栈开发,学习教程!

import randomimport timeimport copyimport sysdef cal_time(func):    def wrapper(*args,**kwargs):        t1 = time.time()        result = func(*args,**kwargs)        t2 = time.time()        print("%s running time: %s secs." % (func.__name__,t2 - t1))        return result    return wrapper@cal_timedef bubble_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]@cal_timedef bubble_sort_1(li):    for i in range(len(li) - 1):        exchange = False        for j in range(len(li) - i - 1):            if li[j] > li[j+1]:                li[j],li[j]                exchange = True        if not exchange:            breakdef select_sort(li):    for i in range(len(li) - 1):        min_loc = i        for j in range(i+1,len(li)):            if li[j] < li[min_loc]:                min_loc = j        li[i],li[min_loc] = li[min_loc],li[i]def insert_sort(li):    for i in range(1,len(li)):        tmp = li[i]        j = i - 1        while j >= 0 and li[j] > tmp:            li[j+1]=li[j]            j = j - 1        li[j + 1] = tmpdef quick_sort_x(data,left,right):    if left < right:        mID = partition(data,right)        quick_sort_x(data,mID - 1)        quick_sort_x(data,mID + 1,right)def partition(data,right):    tmp = data[left]    while left < right:        while left < right and data[right] >= tmp:            right -= 1        data[left] = data[right]        while left < right and data[left] <= tmp:            left += 1        data[right] = data[left]    data[left] = tmp    return left@cal_timedef quick_sort(data):    return quick_sort_x(data,len(data) - 1)@cal_timedef sys_sort(data):    return data.sort()def sift(data,low,high):    i = low    j = 2 * i + 1    tmp = data[i]    while j <= high: #只要没到子树的最后        if j < high and data[j] < data[j + 1]:            j += 1        if tmp < data[j]:#如果领导不能干            data[i] = data[j] #小领导上位            i = j            j = 2 * i + 1        else:            break    data[i] = tmpdef heap_sort(data):    n = len(data)    for i in range(n // 2 - 1,-1,-1):        sift(data,i,n - 1)        for i in range(n - 1,-1):            data[0],data[i] = data[i],data[0]            sift(data,i - 1)sys.setrecursionlimit(100000)data = List(range(1000,1,-1))data.sort()#random.shuffle(data)data1 = copy.deepcopy(data)data2 = copy.deepcopy(data)data3 = copy.deepcopy(data)bubble_sort(data1)quick_sort(data2)sys_sort(data3)

Python语言实现的几种,不同的查找算法。

import timeimport randomdef cal_time(func):    def wrapper(*args,t2 - t1))        return result    return wrapper@cal_timedef bin_search(data_set,val):    low = 0    high = len(data_set) - 1    while low <= high:        mID = (low+high)//2        if data_set[mID]['ID'] == val:            return mID        elif data_set[mID]['ID'] < val:            low = mID + 1        else:            high = mID - 1    returndef binary_search(dataset,find_num):    if len(dataset) > 1:        mID = int(len(dataset) / 2)        if dataset[mID] == find_num:            #print("Find it")            return dataset[mID]        elif dataset[mID] > find_num:            return binary_search(dataset[0:mID],find_num)        else:            return binary_search(dataset[mID + 1:],find_num)    else:        if dataset[0] == find_num:            #print("Find it")            return dataset[0]        else:            pass            #print("Cannot find it.")@cal_timedef binary_search_alex(data_set,val):    return binary_search(data_set,val)def random_List(n):    result = []    IDs = List(range(1001,1001+n))    a1 = ['zhao','qian','sun','li']    a2 = ['li','hao','','']    a3 = ['qiang','guo']    for i in range(n):        age = random.randint(18,60)        ID = IDs[i]        name = random.choice(a1)+random.choice(a2)+random.choice(a3)data = List(range(100000000))print(bin_search(data,173320))print(binary_search_alex(data,173320))
总结

以上是内存溢出为你收集整理的Python 排序与查找算法收集全部内容,希望文章能够帮你解决Python 排序与查找算法收集所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1157890.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存