Python實現(xiàn)桶排序與快速排序算法結(jié)合應(yīng)用示例
本文實例講述了Python實現(xiàn)桶排序與快速排序算法結(jié)合應(yīng)用的方法。分享給大家供大家參考,具體如下:
#-*- coding: UTF-8 -*-
import numpy as np
from QuickSort import QuickSort
def BucketSort(a, n):
barrel = {}
for i in xrange(0,n):
barrel.setdefault(i, [])
min = np.min(a)
max = np.max(a)
for x in a:
for i in xrange(0,n-1):
if x >= min +i* (max - min)/n and x < min +(i +1) * (max - min)/n:
barrel[i].append(x)
elif i == n-2 and x >= min +(i +1) * (max - min)/n:
barrel[i+1].append(x)
k = 0
for i in xrange(0,n):
if len(barrel[i]) != 0:
arr = np.array(barrel[i])
QuickSort(arr, 0, len(barrel[i]) -1)
for x in arr:
a[k] = x
k += 1
if __name__ == '__main__':
a = np.random.randint(0, 100, size = 10)
print "Before sorting..."
print "---------------------------------------------------------------"
print a
print "---------------------------------------------------------------"
BucketSort(a, 10)
print "After sorting..."
print "---------------------------------------------------------------"
print a
print "---------------------------------------------------------------"
快速排序QuickSort:
#-*- coding: UTF-8 -*-
import numpy as np
def Partition(a, i, j):
x = a[i] #將數(shù)組的第一個元素作為初始基準(zhǔn)位置
p = i #同時記錄下該元素的位置
while i < j:
while i < j and a[j] >= x:
j -= 1
while i < j and a[i] <= x:
i += 1
if i != j:
a[i], a[j] = a[j], a[i] #交換a[i]與a[j]
a[p], a[i] = a[i], a[p] #將a[p]與a[i]進行交換
p = i #得到分隔位置
return p
def QuickSort(a, i, j):
if i < j:
p = Partition(a, i, j)
QuickSort(a, i, p-1)
QuickSort (a, p+1, j)
if __name__ == '__main__':
a = np.random.randint(0, 100, size = 100)
print "Before sorting..."
print "---------------------------------------------------------------"
print a
print "---------------------------------------------------------------"
QuickSort(a, 0, a.size - 1)
print "After sorting..."
print "---------------------------------------------------------------"
print a
print "---------------------------------------------------------------"
程序運行結(jié)果:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- 基于python進行桶排序與基數(shù)排序的總結(jié)
- python算法學(xué)習(xí)之桶排序算法實例(分塊排序)
- Python實現(xiàn)的桶排序算法示例
- 10個python3常用排序算法詳細(xì)說明與實例(快速排序,冒泡排序,桶排序,基數(shù)排序,堆排序,希爾排序,歸并排序,計數(shù)排序)
- python實現(xiàn)計數(shù)排序與桶排序?qū)嵗a
- Python數(shù)據(jù)結(jié)構(gòu)與算法之常見的分配排序法示例【桶排序與基數(shù)排序】
- Python實現(xiàn)希爾排序,歸并排序和桶排序的示例代碼
- Python桶排序原理與實現(xiàn)詳解
相關(guān)文章
Python使用ChainMap實現(xiàn)組合數(shù)據(jù)魔法實例探究
這篇文章主要為大家介紹了Python使用ChainMap實現(xiàn)組合數(shù)據(jù)魔法實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
python正則表達式re之compile函數(shù)解析
這篇文章主要介紹了python正則表達式re之compile函數(shù)解析,介紹了其定義,匹配模式等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。2017-10-10
Python借助with語句實現(xiàn)代碼段只執(zhí)行有限次
這篇文章主要介紹了Python借助with語句實現(xiàn)代碼段只執(zhí)行有限次,首先要定義一個能夠在with語句中使用的類實現(xiàn)enter和exit,下文詳細(xì)介紹需要的小伙伴可以參考一下2022-03-03
Python json 模塊核心用法之字典 / 列表與 JSON&nb
在Python開發(fā)中,我們經(jīng)常會遇到Python內(nèi)置數(shù)據(jù)類型(字典、列表)和JSON字符串的相互轉(zhuǎn)換需求,本文會通過實際代碼案例,詳細(xì)講解json模塊的兩個核心方法,感興趣的朋友跟隨小編一起看看吧2026-02-02

