Python实现的几个常用排序算法实例
前段时间为准备百度面试恶补的东西,虽然最后还是被刷了,还是把那几天的“战利品”放点上来,算法一直是自己比较薄弱的地方,以后还要更加努力啊。
下面用Python实现了几个常用的排序,如快速排序,选择排序,以及二路并归排序等等。
#encoding=utf-8
importrandom
fromcopyimportcopy
defdirectInsertSort(seq):
"""直接插入排序"""
size=len(seq)
foriinrange(1,size):
tmp,j=seq[i],i
whilej>0andtmp<seq[j-1]:
seq[j],j=seq[j-1],j-1
seq[j]=tmp
returnseq
defdirectSelectSort(seq):
"""直接选择排序"""
size=len(seq)
foriinrange(0,size-1):
k=i;j=i+1
whilej<size:
ifseq[j]<seq[k]:
k=j
j+=1
seq[i],seq[k]=seq[k],seq[i]
returnseq
defbubbleSort(seq):
"""冒泡排序"""
size=len(seq)
foriinrange(1,size):
forjinrange(0,size-i):
ifseq[j+1]<seq[j]:
seq[j+1],seq[j]=seq[j],seq[j+1]
returnseq
def_divide(seq,low,high):
"""快速排序划分函数"""
tmp=seq[low]
whilelow!=high:
whilelow<highandseq[high]>=tmp:high-=1
iflow<high:
seq[low]=seq[high]
low+=1
whilelow<highandseq[low]<=tmp:low+=1
iflow<high:
seq[high]=seq[low]
high-=1
seq[low]=tmp
returnlow
def_quickSort(seq,low,high):
"""快速排序辅助函数"""
iflow>=high:return
mid=_divide(seq,low,high)
_quickSort(seq,low,mid-1)
_quickSort(seq,mid+1,high)
defquickSort(seq):
"""快速排序包裹函数"""
size=len(seq)
_quickSort(seq,0,size-1)
returnseq
defmerge(seq,left,mid,right):
tmp=[]
i,j=left,mid
whilei<midandj<=right:
ifseq[i]<seq[j]:
tmp.append(seq[i])
i+=1
else:
tmp.append(seq[j])
j+=1
ifi<mid:tmp.extend(seq[i:])
ifj<=right:tmp.extend(seq[j:])
seq[left:right+1]=tmp[0:right-left+1]
def_mergeSort(seq,left,right):
ifleft==right:
return
else:
mid=(left+right)/2
_mergeSort(seq,left,mid)
_mergeSort(seq,mid+1,right)
merge(seq,left,mid+1,right)
#二路并归排序
defmergeSort(seq):
size=len(seq)
_mergeSort(seq,0,size-1)
returnseq
if__name__=="__main__":
s=[random.randint(0,100)foriinrange(0,20)]
prints
print"\n"
printdirectSelectSort(copy(s))
printdirectInsertSort(copy(s))
printbubbleSort(copy(s))
printquickSort(copy(s))
printmergeSort(copy(s))
运行结果如下:
E:\python_project\practice>sorting.py
[10,47,56,76,64,84,26,8,47,51,88,81,32,95,91,29,28,69,61,45]
[8,10,26,28,29,32,45,47,47,51,56,61,64,69,76,81,84,88,91,95]
[8,10,26,28,29,32,45,47,47,51,56,61,64,69,76,81,84,88,91,95]
[8,10,26,28,29,32,45,47,47,51,56,61,64,69,76,81,84,88,91,95]
[8,10,26,28,29,32,45,47,47,51,56,61,64,69,76,81,84,88,91,95]
[8,10,26,28,29,32,45,47,47,51,56,61,64,69,76,81,84,88,91,95]
相关文章
- Python项目48-插播短信接口(使劲撸)
- 如何为pycharm配置Python解释器_pycharm选择python解释器
- Python爬取网易云歌曲评论,做词云分析
- Python入门系列(十)一篇学会python文件处理
- python中dtype的使用规范_Python numpy.dtype() 使用实例
- 简述python变量的命名规则_Python 变量命名规则
- python爬虫实例大全
- java和python哪个值得学-学java好还是Python好?
- python监控网页内容变化_使用Python监控文件内容变化代码实例
- python socket recvfrom_Python:socket.recvfrom()不返回任何地址
- Python 技巧篇-pip卸载python库实例演示,查看pip命令大全方法[通俗易懂]
- 【说站】python实例方法中self的作用
- Python修改文件后缀_python重命名文件名
- python程序化交易实例-用 Python 实现你的量化交易策略「建议收藏」
- Python 生成随机数_python 随机字符串
- 【测试开发】python系列教程:Python 运算符
- 预备小菜:Python入门之基础语法
- 网络工程师学Python-14-迭代器
- python在线抓取百度词典的翻译结果翻译单词详解编程语言
- 利用python实现冒泡排序详解编程语言
- python计数排序和基数排序算法实例
- python数据结构之二叉树的建立实例
- python字典多条件排序方法实例
- Python列表计数及插入实例
- python中的hashlib和base64加密模块使用实例
- python快速查找算法应用实例