python實(shí)現(xiàn)的各種排序算法代碼
# -*- coding: utf-8 -*-
# 測(cè)試各種排序算法
# link:www.dbjr.com.cn
# date:2013/2/2
#選擇排序
def select_sort(sort_array):
for i, elem in enumerate(sort_array):
for j, elem in enumerate(sort_array[i:]):
if sort_array[i] > sort_array[j + i]:
#交換
sort_array[i], sort_array[j + i] = sort_array[j + i], sort_array[i]
#冒泡排序
def bubble_sort(sort_array):
for i, elem in enumerate(sort_array):
for j, elem in enumerate(sort_array[:len(sort_array) - i - 1]):
if sort_array[j] > sort_array[j + 1]:
sort_array[j], sort_array[j + 1] = sort_array[j + 1], sort_array[j]
#插入排序
def insert_sort(sort_array):
for i, elem in enumerate(sort_array):
for j, elem in enumerate(sort_array[:i]):
if sort_array[j] > sort_array[i]:
sort_array.insert(j, sort_array[i])
del sort_array[i + 1]
#歸并排序
def merge_sort_wrapper(sort_array):
merge_sort(sort_array, 0, len(sort_array) - 1)
def merge_sort(sort_array, left = 0, right = 0):
if left < right:
center = (left + right) / 2
merge_sort(sort_array, left, center)
merge_sort(sort_array, center + 1, right)
merge(sort_array, left, right, center)
def merge(sort_array, left, right, center):
result = []
arrayA = sort_array[left:center + 1]
arrayB = sort_array[center + 1:right + 1]
while((len(arrayA) > 0) and (len(arrayB) > 0)):
if(arrayA[0] > arrayB[0]):
result.append(arrayB.pop(0))
else:
result.append(arrayA.pop(0))
if(len(arrayA) > 0):
result.extend(arrayA)
if(len(arrayB) > 0):
result.extend(arrayB)
sort_array[left:right + 1] = result
#快排
def quick_sort(sort_array):
if(len(sort_array) < 2):
return
left = [x for x in sort_array[1:] if x < sort_array[0]]
right = [x for x in sort_array[1:] if x >= sort_array[0]]
quick_sort(left)
quick_sort(right)
sort_array[:] = left + [sort_array[0]] + right
#shell排序
def shell_sort(sort_array):
dist=len(sort_array)/2
while dist > 0:
for i in range(dist,len(sort_array)):
tmp=sort_array[i]
j = i
while j >= dist and tmp < sort_array[j - dist]:
sort_array[j] = sort_array[j - dist]
j -= dist
sort_array[j] = tmp
dist /= 2
#基數(shù)排序,均為整數(shù),不支持負(fù)數(shù)和重復(fù)
def radix_sort(sort_array):
max_elem = max(sort_array)
bucket_list = []
for i in range(max_elem):
bucket_list.insert(i, 0)
for x in sort_array:
bucket_list[x - 1] = -1
sort_array[:] = [x + 1 for x in range(len(bucket_list)) if bucket_list[x] == -1]
#堆排序
def heap_sort(sort_array):
#沒有寫出來,再想想
pass
#測(cè)試?yán)?BR>def algo_sort_test(sort_array, sort_method):
sort_method(sort_array)
if __name__ == '__main__':
sort_array = [1, 2, 3, 5, -4, 4, 10, 3, 19, 13, 16, 18, 5, 190, 456, 23]
algo_sort_test(sort_array, select_sort)
print sort_array
sort_array = [1, 2, 3, 5, -4, 4, 10, 3, 19, 13, 16, 18, 5, 190, 456, 23]
algo_sort_test(sort_array, bubble_sort)
print sort_array
sort_array = [1, 2, 3, 5, -4, 4, 10, 3, 19, 13, 16, 18, 5, 190, 456, 23]
algo_sort_test(sort_array, insert_sort)
print sort_array
sort_array = [1, 2, 3, 5, -4, 4, 10, 3, 19, 13, 16, 18, 5, 190, 456, 23]
algo_sort_test(sort_array, merge_sort_wrapper)
print sort_array
sort_array = [1, 2, 3, 5, -4, 4, 10, 300, 19, 13, 16, 18, 500, 190, 456, 23]
algo_sort_test(sort_array, quick_sort)
print sort_array
sort_array = [1, 2, 3, 5, -4, 4, 10, 3, 19, 13, 16, 18, 5, 190, 456, 23]
algo_sort_test(sort_array, shell_sort)
print sort_array
sort_array = [1, 2, 3, 5, 4, 10, 19, 13, 16, 18, 190, 456, 23]
algo_sort_test(sort_array, radix_sort)
print sort_array
print 'OK'
非?;A(chǔ)的知識(shí)內(nèi)容,選擇、冒泡、插入、歸并、基數(shù),還有快排都能手寫出來,但寫了一遍發(fā)現(xiàn)堆排序忘了怎么做了。要復(fù)習(xí)啦。
- python實(shí)現(xiàn)冒泡排序算法的兩種方法
- python冒泡排序算法的實(shí)現(xiàn)代碼
- python 算法 排序?qū)崿F(xiàn)快速排序
- python 實(shí)現(xiàn)插入排序算法
- python選擇排序算法的實(shí)現(xiàn)代碼
- Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之快速排序詳解
- python 實(shí)現(xiàn)歸并排序算法
- Python實(shí)現(xiàn)的直接插入排序算法示例
- 基于python的七種經(jīng)典排序算法(推薦)
- 如何利用Python動(dòng)態(tài)展示排序算法
相關(guān)文章
python實(shí)現(xiàn)整數(shù)序列求和
這篇文章主要介紹了python實(shí)現(xiàn)整數(shù)序列求和,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07解決Python運(yùn)行文件出現(xiàn)out of memory框的問題
今天小編就為大家分享一篇解決Python運(yùn)行文件出現(xiàn)out of memory框的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python中range、np.arange和np.linspace的區(qū)別
本文主要介紹了Python中range、np.arange和np.linspace的區(qū)別,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03python中DataFrame數(shù)據(jù)合并merge()和concat()方法詳解
Pandas提供了很多合并Series和Dataframe的強(qiáng)大的功能,通過這些功能可以方便的進(jìn)行數(shù)據(jù)分析,下面這篇文章主要給大家介紹了關(guān)于python中DataFrame數(shù)據(jù)合并merge()和concat()方法的相關(guān)資料,需要的朋友可以參考下2022-07-07Python3.5面向?qū)ο笈c繼承圖文實(shí)例詳解
這篇文章主要介紹了Python3.5面向?qū)ο笈c繼承,結(jié)合圖文與實(shí)例形式詳細(xì)分析了Python3.5面向?qū)ο笈c繼承的相關(guān)概念、原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2019-04-04python繪制子圖技巧之plt.subplot、plt.subplots及坐標(biāo)軸修改
一個(gè)圖片里邊繪制多個(gè)圖像是繪圖中的常見需求,下面這篇文章主要給大家介紹了關(guān)于python繪制子圖技巧之plt.subplot、plt.subplots及坐標(biāo)軸修改的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05注意import和from import 的區(qū)別及說明
這篇文章主要介紹了注意import和from import 的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Python之tkinter組合框Combobox用法及說明
這篇文章主要介紹了Python之tkinter組合框Combobox用法及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05