python实现的各种排序算法代码


Posted in Python onMarch 04, 2013
# -*- coding: utf-8 -*-
# 测试各种排序算法
# link:3water.com
# 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  
#基数排序,均为整数,不支持负数和重复
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
#测试例子
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'

非常基础的知识内容,选择、冒泡、插入、归并、基数,还有快排都能手写出来,但写了一遍发现堆排序忘了怎么做了。要复习啦。

Python 相关文章推荐
如何搜索查找并解决Django相关的问题
Jun 30 Python
Python内置数据类型详解
Aug 18 Python
利用Python和OpenCV库将URL转换为OpenCV格式的方法
Mar 27 Python
python实现微信发送邮件关闭电脑功能
Feb 22 Python
使用python生成目录树
Mar 29 Python
django模板结构优化的方法
Feb 28 Python
python调用webservice接口的实现
Jul 12 Python
python tkinter组件使用详解
Sep 16 Python
python/Matplotlib绘制复变函数图像教程
Nov 21 Python
Cython编译python为so 代码加密示例
Dec 23 Python
Python flask路由间传递变量实例详解
Jun 03 Python
Python OpenCV中的numpy与图像类型转换操作
Dec 11 Python
python 获取本机ip地址的两个方法
Feb 25 #Python
把大数据数字口语化(python与js)两种实现
Feb 21 #Python
python正则表达式修复网站文章字体不统一的解决方法
Feb 21 #Python
Python操作Mysql实例代码教程在线版(查询手册)
Feb 18 #Python
python的常见命令注入威胁
Feb 18 #Python
centos下更新Python版本的步骤
Feb 12 #Python
Python3.x和Python2.x的区别介绍
Feb 12 #Python
You might like
PHP 9 大缓存技术总结
2015/09/17 PHP
PHP中mysqli_get_server_version()的实例用法
2020/02/03 PHP
使用JSLint提高JS代码质量方法分享
2013/12/16 Javascript
js简单的表格添加行和删除行操作示例
2014/03/31 Javascript
jquery实现图片按比例缩放示例
2014/07/01 Javascript
分享一则JavaScript滚动条插件源码
2015/03/03 Javascript
jQuery+CSS3文字跑马灯特效的简单实现
2016/06/25 Javascript
微信小程序 input输入框控件详解及实例(多种示例)
2016/12/14 Javascript
canvas实现探照灯效果
2017/02/07 Javascript
为你的微信小程序体积瘦身详解
2017/05/20 Javascript
Angular4实现动态添加删除表单输入框功能
2017/08/11 Javascript
微信小程序组件之srcoll-view的详解
2017/10/19 Javascript
Vue组件系列开发之模态框
2019/04/18 Javascript
js单线程的本质 Event Loop解析
2019/10/29 Javascript
[02:49]2014DOTA2电竞也是体育项目! 势要把荣誉带回中国!
2014/07/20 DOTA
[01:59]游戏“zheng”当时试玩会
2019/08/21 DOTA
python的正则表达式re模块的常用方法
2013/03/09 Python
Python列表生成器的循环技巧分享
2015/03/06 Python
python执行等待程序直到第二天零点的方法
2015/04/23 Python
Python3使用PyQt5制作简单的画板/手写板实例
2017/10/19 Python
python+numpy+matplotalib实现梯度下降法
2018/08/31 Python
将Python文件打包成.EXE可执行文件的方法
2019/08/11 Python
Python中itertools的用法详解
2020/02/07 Python
python加密解密库cryptography使用openSSL生成的密匙加密解密
2020/02/11 Python
python实现银行实战系统
2020/02/26 Python
Python json读写方式和字典相互转化
2020/04/18 Python
详解Python 函数参数的拆解
2020/09/02 Python
CSS图片翻转动画技术详解(IE也实现了)
2014/04/03 HTML / CSS
解决html5中的video标签ios系统中无法播放使用的问题
2020/08/10 HTML / CSS
英国领先的在线高尔夫设备零售商:Golfgeardirect
2020/12/11 全球购物
高一学生评语大全
2014/04/25 职场文书
2014年党员个人工作总结
2014/12/02 职场文书
观看《信仰》心得体会
2016/01/15 职场文书
ThinkPHP5和ThinkPHP6的区别
2021/03/31 PHP
thinkphp 获取控制器及控制器方法
2021/04/16 PHP
深入理解MySQL中MVCC与BufferPool缓存机制
2022/05/25 MySQL