Python实现的几种排序算法
2013-11-18 19:09
411 查看
Python实现的几种排序算法
FuniK 发布于 2013年10月11日 15时,6评/489阅
分享到
新浪微博腾讯微博
收藏+8
踩顶0
包括了:冒泡排序,插入排序,选择排序,快速排序和希尔排序
Python版本:2.7.3
标签: <无>
代码片段(1)[全屏查看所有代码]
1. [文件]sort.py ~ 2KB
下载(30)跳至
[1][全屏预览]
view sourceprint?001 | import sys, getopt, random |
002 |
003 | def bubble_sort(seq): |
004 | for i in range ( len (seq)): |
005 | for j in range ( 1 , len (seq)): |
006 | if seq[j - 1 ]>seq[j]: |
007 | seq[j - 1 ], seq[j] = seq[j], seq[j - 1 ] |
008 | return seq |
009 |
010 | def insertion_sort(seq): |
011 | for i in range ( 1 , len (seq)): |
012 | tmp = seq[i] |
013 | pos = i; |
014 | for j in range (i - 1 , - 1 , - 1 ): |
015 | if seq[j]>tmp: |
016 | seq[j + 1 ] = seq[j] |
017 | pos = j |
018 | seq[pos] = tmp |
019 | return seq |
020 |
021 | def selection_sort(seq): |
022 | for i in range ( len (seq)): |
023 | min_index = i; |
024 | for j in range (i, len (seq)): |
025 | if seq[j]<seq[min_index]: |
026 | min_index = j |
027 | seq[i],seq[min_index] = seq[min_index],seq[i] |
028 | return seq |
029 |
030 | def partition(seq,p,l,r): |
031 | pivot = seq[p] |
032 | seq[p],seq[r] = seq[r],seq[p] |
033 | p_pos = l |
034 | for i in range (l,r): |
035 | if seq[i]< = pivot: |
036 | seq[i],seq[p_pos] = seq[p_pos],seq[i] |
037 | p_pos = p_pos + 1 |
038 | seq[p_pos],seq[r] = seq[r],seq[p_pos] |
039 | return p_pos |
040 |
041 |
042 | def quick_sort(seq,left,right): |
043 | if left < right: |
044 | pivot = random.randint(left,right) |
045 | mid = partition(seq,pivot,left,right) |
046 | quick_sort(seq,left,mid - 1 ) |
047 | quick_sort(seq,mid + 1 ,right) |
048 | return seq |
049 |
050 | def shell_sort(seq): |
051 | incr = len (seq) / 2 |
052 | while (incr> = 1 ): |
053 | for i in range (incr, len (seq)): |
054 | tmp = seq[i] |
055 | pos = i; |
056 | for j in range (i - incr, - 1 , - incr): |
057 | if seq[j]>tmp: |
058 | seq[j + incr] = seq[j] |
059 | pos = j |
060 | seq[pos] = tmp |
061 | incr = incr / 2 |
062 | return seq |
063 |
064 | def usage(): |
065 | 'Usage: python sort.py sorttype[-q|-i|-b|-s|--shell]sequence' |
066 | 'Example: python sort.py -q 11,32,3,24,5' |
067 |
068 | def main(): |
069 | try : |
070 | if ( len (sys.argv) = = 1 ) or ( len (sys.argv)! = 3 ): |
071 | raise Exception() |
072 | else : |
073 | opts,args = getopt.getopt(sys.argv[ 1 :], 'qibs' ,[ 'shell' ]) |
074 |
075 | if len (args)> 0 : |
076 | seq = [] |
077 | tmp = args[ 0 ].split( ',' ) |
078 | for i in tmp: |
079 | seq.append( int (i)) |
080 | else : |
081 | raise Exception() |
082 |
083 | for opt in opts: |
084 | if opt[ 0 ] = = '-q' : |
085 | quick_sort(seq, 0 , len (seq) - 1 ) |
086 | elif opt[ 0 ] = = '-i' : |
087 | insertion_sort(seq) |
088 | elif opt[ 0 ] = = '-b' : |
089 | bubble_sort(seq) |
090 | elif opt[ 0 ] = = '-s' : |
091 | selection_sort(seq) |
092 | elif opt[ 0 ] = = '--shell' : |
093 | shell_sort(seq) |
094 |
095 | except Exception,e: |
096 | usage() |
097 | e |
098 | sys.exit() |
099 |
100 | if __name__ = = "__main__" : |
101 | main() |
相关文章推荐
- 几种排序算法的python实现
- 几种排序算法分析及python实现
- 几种常用的排序算法(二)--python实现
- python实现的几种排序算法
- Python实现几种排序算法
- python实现几种排序算法
- 几种常用的排序算法(一)--python实现
- 几种常用排序算法的python实现
- Python2实现的几种排序算法
- Java实现几种常见排序算法代码
- python实现排序算法(上)
- 几种常见的排序算法实现
- 几种排序算法的实现与比较
- 如何用Python实现八大排序算法
- 基本排序算法[python实现]
- 排序算法—希尔排序算法分析与实现(Python)
- 突破python缺陷,实现几种自定义线程池 以及进程、线程、协程的介绍
- Python几种并发实现方案的性能比较
- python排序算法的实现-冒泡
- 基于c++实现的几种排序算法