用python實(shí)現(xiàn)各種數(shù)據(jù)結(jié)構(gòu)
快速排序
def quick_sort(_list):
if len(_list) < 2:
return _list
pivot_index = 0
pivot = _list(pivot_index)
left_list = [i for i in _list[:pivot_index] if i < pivot]
right_list = [i for i in _list[pivot_index:] if i > pivot]
return quick_sort(left) + [pivot] + quick_sort(right)
選擇排序
def select_sort(seq):
n = len(seq)
for i in range(n-1)
min_idx = i
for j in range(i+1,n):
if seq[j] < seq[min_inx]:
min_idx = j
if min_idx != i:
seq[i], seq[min_idx] = seq[min_idx],seq[i]
插入排序
def insertion_sort(_list):
n = len(_list)
for i in range(1,n):
value = _list[i]
pos = i
while pos > 0 and value < _list[pos - 1]
_list[pos] = _list[pos - 1]
pos -= 1
_list[pos] = value
print(sql)
歸并排序
???
def merge_sorted_list(_list1,_list2): #合并有序列表
len_a, len_b = len(_list1),len(_list2)
a = b = 0
sort = []
while len_a > a and len_b > b:
if _list1[a] > _list2[b]:
sort.append(_list2[b])
b += 1
else:
sort.append(_list1[a])
a += 1
if len_a > a:
sort.append(_list1[a:])
if len_b > b:
sort.append(_list2[b:])
return sort
def merge_sort(_list):
if len(list1)<2:
return list1
else:
mid = int(len(list1)/2)
left = mergesort(list1[:mid])
right = mergesort(list1[mid:])
return merge_sorted_list(left,right)
堆排序heapq模塊
from heapq import nsmallest
def heap_sort(_list):
return nsmallest(len(_list),_list)
棧
from collections import deque
class Stack:
def __init__(self):
self.s = deque()
def peek(self):
p = self.pop()
self.push(p)
return p
def push(self, el):
self.s.append(el)
def pop(self):
return self.pop()
隊(duì)列
from collections import deque
class Queue:
def __init__(self):
self.s = deque()
def push(self, el):
self.s.append(el)
def pop(self):
return self.popleft()
二分查找
def binary_search(_list,num):
mid = len(_list)//2
if len(_list) < 1:
return Flase
if num > _list[mid]:
BinarySearch(_list[mid:],num)
elif num < _list[mid]:
BinarySearch(_list[:mid],num)
else:
return _list.index(num)
到此這篇關(guān)于用python實(shí)現(xiàn)各種數(shù)據(jù)結(jié)構(gòu)的文章就介紹到這了,更多相關(guān)python實(shí)現(xiàn)各種數(shù)據(jù)結(jié)構(gòu)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
- 基于python實(shí)現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型
- Python 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)-堆棧和隊(duì)列的操作方法
- Python 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)-循環(huán)隊(duì)列的操作方法
- python算法與數(shù)據(jù)結(jié)構(gòu)之單鏈表的實(shí)現(xiàn)代碼
- Python 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中的的棧隊(duì)列
相關(guān)文章
keras Lambda自定義層實(shí)現(xiàn)數(shù)據(jù)的切片方式,Lambda傳參數(shù)
這篇文章主要介紹了keras Lambda自定義層實(shí)現(xiàn)數(shù)據(jù)的切片方式,Lambda傳參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
python使用點(diǎn)操作符訪(fǎng)問(wèn)字典(dict)數(shù)據(jù)的方法
這篇文章主要介紹了python使用點(diǎn)操作符訪(fǎng)問(wèn)字典(dict)數(shù)據(jù)的方法,涉及Python操作字典的技巧,需要的朋友可以參考下2015-03-03
在win和Linux系統(tǒng)中python命令行運(yùn)行的不同
本文給大家分享的是作者在在win和Linux系統(tǒng)中python命令行運(yùn)行的不同的解決方法,有相同需求的小伙伴可以參考下2016-07-07
Python使用scrapy爬取陽(yáng)光熱線(xiàn)問(wèn)政平臺(tái)過(guò)程解析
這篇文章主要介紹了Python使用scrapy爬取陽(yáng)光熱線(xiàn)問(wèn)政平臺(tái)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
django API 中接口的互相調(diào)用實(shí)例
這篇文章主要介紹了django API 中接口的互相調(diào)用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
聊聊Python中的浮點(diǎn)數(shù)運(yùn)算不準(zhǔn)確問(wèn)題
這篇文章主要介紹了聊聊Python中的浮點(diǎn)數(shù)運(yùn)算不準(zhǔn)確問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
淺談cv2.imread()和keras.preprocessing中的image.load_img()區(qū)別
這篇文章主要介紹了淺談cv2.imread()和keras.preprocessing中的image.load_img()區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
Python中協(xié)程coroutine適用場(chǎng)景分析
多線(xiàn)程中可能出現(xiàn)多個(gè)線(xiàn)程爭(zhēng)搶變量,所以變量需要加鎖;協(xié)程中任一時(shí)刻都只有一個(gè)線(xiàn)程,所以變量不需要加鎖,這篇文章主要介紹了Python中協(xié)程(coroutine)詳解,需要的朋友可以參考下2024-04-04
python批量制作雷達(dá)圖的實(shí)現(xiàn)方法
本文通過(guò)實(shí)例代碼介紹了如何用python批量制作雷達(dá)圖的實(shí)現(xiàn)方法,下面一起來(lái)看看如何實(shí)現(xiàn)的。2016-07-07

