Python实现的插入排序,冒泡排序,快速排序,选择排序算法示例

yipeiwu_com5年前Python基础

本文实例讲述了Python实现的插入排序,冒泡排序,快速排序,选择排序算法。分享给大家供大家参考,具体如下:

#!/usr/bin/python
# coding:utf-8
#直接插入排序
def insert_sort(list):
  for i in range(len(list)):
    Key = list [i]      #待插入元素
      j = i - 1
      while(Key < list[j] and j >= 0):
        list[j+1] = list[j]  #后移元素
        list[j] = Key
        j=j-1
  return list
#冒泡排序  
def bubble_sort(list):
  for i in range(1, len(list)):
    for j in range(len(list)-i):
      if list[j] > list [j+1]:
        list[j+1],list[j] =list[j],list[j+1]
  return list
#快速排序
def position_key(list, low, high):
  i = low
  j = high
  key = list[low]
  while(i < j):
    while(i < j and list[j] >= key):  #从右向左,寻找第一个小于基准元素的数据索引。 注意:i<y 的判断,内循环更新j
      j -= 1
    if i < j :
      list[i] = list[j]
    while(i<j and list[i] <= key):   #从左向右,寻找地一个大于基准元素的数据索引。i<y同上
      i += 1
    if i < j:
      list[j] = list[i]
  list[j] = key     # 基准元素位置 
  return j
def quick_sort(list, low, high):
  if low < high:
    position = position_key(list, low, high) #将数据分成前后两个子序列(前边序列值均小于后边序列)
    quick_sort(list, low, position-1)    #将前面的序列快速排序
    quick_sort(list, position+1, high)    #将后面的序列快速排序
  return list
#选择排序
def select_sort(list):
  for i in range(len(list)):
    for j in range(i, len(list)):
      if list[i] > list[j]:
        list[i], list[j] = list[j], list[i]
  return list
list = [23, 232, 11, 89,121, 64, 34, 12, 23423, 2312, 167, 768, 932, 346, 32789, 335, 2, 1145, 34, 56, 99, 111]
print '原始序列  :', list
print '直接插入排序:', insert_sort(list)
print '冒泡排序  :', bubble_sort(list)
print '快速排序  :', quick_sort(list, 0, len(list)-1)
print '选择排序  :', select_sort(list)

结果如下:

原始序列    : [23, 232, 11, 89, 121, 64, 34, 12, 23423, 2312, 167, 768, 932, 346, 32789, 335, 2, 1145, 34, 56, 99, 111]
直接插入排序: [2, 11, 12, 23, 34, 34, 56, 64, 89, 99, 111, 121, 167, 232, 335, 346, 768, 932, 1145, 2312, 23423, 32789]
冒泡排序    : [2, 11, 12, 23, 34, 34, 56, 64, 89, 99, 111, 121, 167, 232, 335, 346, 768, 932, 1145, 2312, 23423, 32789]
快速排序    : [2, 11, 12, 23, 34, 34, 56, 64, 89, 99, 111, 121, 167, 232, 335, 346, 768, 932, 1145, 2312, 23423, 32789]
选择排序    : [2, 11, 12, 23, 34, 34, 56, 64, 89, 99, 111, 121, 167, 232, 335, 346, 768, 932, 1145, 2312, 23423, 32789]

PS:这里再为大家推荐一款关于排序的演示工具供大家参考:

在线动画演示插入/选择/冒泡/归并/希尔/快速排序算法过程工具:
http://tools.jb51.net/aideddesign/paixu_ys

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python栈算法的实现与简单应用示例

Python栈算法的实现与简单应用示例

本文实例讲述了Python栈算法的实现与简单应用。分享给大家供大家参考,具体如下: 原理: 栈作为一种数据结构,是一种只能在一端进行插入和删除操作。它按照先进后出的原则存储数据,先进入的...

对python 中re.sub,replace(),strip()的区别详解

对python 中re.sub,replace(),strip()的区别详解

1.strip(): str.strip([chars]);去除字符串前面和后面的所有设置的字符串,默认为空格 chars -- 移除字符串头尾指定的字符序列。 st = " he...

Python+微信接口实现运维报警

说到运维报警,我觉得都可以写个长篇历史来详细解释了报警的前世来生,比如最早报警都是用邮件,但邮件实时性不高,比如下班回家总不能人一直盯着邮箱吧,所以邮件这种报警方式不适合用来报紧急的故障...

解决yum对python依赖版本问题

错误 # yum list File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ Synt...

Python MD5文件生成码

import md5 import sys def sumfile(fobj): m = md5.new() while True: d = fobj.read(8096) if not...