python实现快速排序的几种方法.docx_第1页
python实现快速排序的几种方法.docx_第2页
python实现快速排序的几种方法.docx_第3页
python实现快速排序的几种方法.docx_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

python实现快速排序的几种方法快速排序算法说明:设要排序的数组是A0AN-1,首先任意选取一个数据(通常选用中间的数)作为关键数据,然后将所有比它小的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序。值得注意的是,快速排序不是一种稳定的排序算法,也就是说,多个相同的值的相对位置也许会在算法结束时产生变动。一趟快速排序的算法是:1)设置两个变量i、j,排序开始的时候:i=0,j=N-1;2)以第一个数组元素作为关键数据,赋值给key,即key=A0;3)从j开始向前搜索,即由后开始向前搜索(j - ),找到第一个小于key的值Aj;4)从i开始向后搜索,即由前开始向后搜索(i + ),找到第一个大于key的Ai,Ai与Aj交换;5)重复第3、4、5步,直到 I=J; (3,4步中,没找到符合条件的值,即3中Aj不小于key,4中Aj不大于key的时候改变j、i的值,使得j=j-1,i=i+1,直至找到为止。找到符合条件的值,进行交换的时候i, j指针位置不变。另外,i=j这一过程一定正好是i+或j-完成的时候,此时令循环结束)。(注:以上说明摘自百度百科)方法一:经典快速排序算法下面的代码中是经典快速排序算法,并且范围正序和倒序两种排列方式:#正序排列函数def qiucksortasc(arr,begin,end): #设定取出开始的第一个值用于判断 key=arrbegin #从左边开始找大于key值的元素下标 pl=begin #从右边开始找小于key值的元素下标 pr=end while 1: #找到从右边开始第一个小于key的元素,如果大于key,pr-1继续寻找 while arrprkey: pr=pr-1 #找到从左边开始第一个大于key的元素,如果小于key,pl+1继续寻找 while arrplkey: pl=pl+1 #交换arrpr和arrpl的值 arrpl,arrpr=arrpr,arrpl print pl is %d and pr is %d %(pl,pr), and ,arr #当pr和pl相等的时候跳出循环 if pl=pr: print 完成一趟快排 break; #递归开始 #对key左边的元素开始排序 if(pl+1begin): qiucksortasc(arr,begin,pr-1)#倒序排列函数def qiucksortdesc(arr,begin,end): #设定取出开始的第一个值用于判断 key=arrbegin #从左边开始找大于key值的元素下标 pl=begin #从右边开始找小于key值的元素下标 pr=end while 1: #找到从右边开始第一个大于key的元素,如果小于key,pr-1继续寻找 while arrprkey: pl=pl+1 #交换arrpr和arrpl的值 arrpl,arrpr=arrpr,arrpl print pl is %d and pr is %d %(pl,pr), and ,arr #当pr和pl相等的时候跳出循环 if pl=pr: print 完成一趟快排 break; #递归开始 #对key左边的元素开始排序 if(pl-1begin): qiucksortdesc(arr,begin,pl-1) #对key右边的元素开始排序 if(pr+1end): qiucksortdesc(arr,pr+1,end)#初始化需要排序的数组a=4,2,1,5,3,7,6,8,9print 排序前数组:,a#调用排序函数print *print 正序排序过程qiucksortasc(a,0,len(a)-1)print 正序排序后数组:,aprint *a=4,2,1,5,3,7,6,8,9print *print 倒序排序过程qiucksortdesc(a,0,len(a)-1)print 倒序排序后数组:,aprint *代码运行后我们可以在控制台的输出中看到结果以及快速排序的过程:排序前数组: 4, 2, 1, 5, 3, 7, 6, 8, 9*正序排序过程pl is 0 and pr is 4 and 3, 2, 1, 5, 4, 7, 6, 8, 9pl is 3 and pr is 4 and 3, 2, 1, 4, 5, 7, 6, 8, 9pl is 3 and pr is 3 and 3, 2, 1, 4, 5, 7, 6, 8, 9完成一趟快排pl is 4 and pr is 4 and 3, 2, 1, 4, 5, 7, 6, 8, 9完成一趟快排pl is 5 and pr is 6 and 3, 2, 1, 4, 5, 6, 7, 8, 9pl is 6 and pr is 6 and 3, 2, 1, 4, 5, 6, 7, 8, 9完成一趟快排pl is 7 and pr is 7 and 3, 2, 1, 4, 5, 6, 7, 8, 9完成一趟快排pl is 0 and pr is 2 and 1, 2, 3, 4, 5, 6, 7, 8, 9pl is 2 and pr is 2 and 1, 2, 3, 4, 5, 6, 7, 8, 9完成一趟快排pl is 0 and pr is 0 and 1, 2, 3, 4, 5, 6, 7, 8, 9完成一趟快排正序排序后数组: 1, 2, 3, 4, 5, 6, 7, 8, 9*倒序排序过程pl is 0 and pr is 8 and 9, 2, 1, 5, 3, 7, 6, 8, 4pl is 1 and pr is 8 and 9, 4, 1, 5, 3, 7, 6, 8, 2pl is 1 and pr is 7 and 9, 8, 1, 5, 3, 7, 6, 4, 2pl is 2 and pr is 7 and 9, 8, 4, 5, 3, 7, 6, 1, 2pl is 2 and pr is 6 and 9, 8, 6, 5, 3, 7, 4, 1, 2pl is 4 and pr is 6 and 9, 8, 6, 5, 4, 7, 3, 1, 2pl is 4 and pr is 5 and 9, 8, 6, 5, 7, 4, 3, 1, 2pl is 5 and pr is 5 and 9, 8, 6, 5, 7, 4, 3, 1, 2完成一趟快排pl is 0 and pr is 0 and 9, 8, 6, 5, 7, 4, 3, 1, 2完成一趟快排pl is 1 and pr is 1 and 9, 8, 6, 5, 7, 4, 3, 1, 2完成一趟快排pl is 2 and pr is 4 and 9, 8, 7, 5, 6, 4, 3, 1, 2pl is 3 and pr is 4 and 9, 8, 7, 6, 5, 4, 3, 1, 2pl is 3 and pr is 3 and 9, 8, 7, 6, 5, 4, 3, 1, 2完成一趟快排pl is 6 and pr is 6 and 9, 8, 7, 6, 5, 4, 3, 1, 2完成一趟快排pl is 7 and pr is 8 and 9, 8, 7, 6, 5, 4, 3, 2, 1pl is 8 and pr is 8 and 9, 8, 7, 6, 5, 4, 3, 2, 1完成一趟快排倒序排序后数组: 9, 8, 7, 6, 5, 4, 3, 2, 1*方法二:基于算法本身的特征其实快速排序的思想就是选定一个key值然后把比他小的放到一边,把比他大的放到另一边,然后不断的分下去,直到最小的元素和最大的元素为止。所以基于此我们可以用下面的方法:def qiucksortshort(arr): return if

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论