用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)容請搜索腳本之家以前的文章或繼續(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)文章
windows+vscode穿越跳板機(jī)調(diào)試遠(yuǎn)程代碼的圖文教程
本文通過圖文并茂的形式給大家介紹了windows+vscode穿越跳板機(jī)調(diào)試遠(yuǎn)程代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
python使用opencv實(shí)現(xiàn)馬賽克效果示例
這篇文章主要介紹了python使用opencv實(shí)現(xiàn)馬賽克效果,結(jié)合實(shí)例形式分析了Python使用cv2模塊操作圖片實(shí)現(xiàn)馬賽克效果的相關(guān)技巧,需要的朋友可以參考下2019-09-09
Python庫學(xué)習(xí)Tkinter制作GUI個(gè)性簽名設(shè)計(jì)軟件
Tkinter 是 Python 中的標(biāo)準(zhǔn) GUI 庫,使用 Tkinter 可以快速地創(chuàng)建 GUI 應(yīng)用程序。今天我們打算再用一個(gè)小案例,帶大家加深對Tkinter的理解2021-09-09
python基于tkinter實(shí)現(xiàn)gif錄屏功能
一直在思索實(shí)現(xiàn)一個(gè)透明的窗體,然后可以基于這個(gè)窗體可以開發(fā)出各種好玩的應(yīng)用,這一期,我們將實(shí)現(xiàn)有趣的GIF錄屏功能2021-05-05
用Python復(fù)現(xiàn)二戰(zhàn)德軍enigma密碼機(jī)
大家好,本篇文章主要講的是用Python復(fù)現(xiàn)二戰(zhàn)德軍enigma密碼機(jī),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01

