python实现的希尔排序算法实例

yipeiwu_com5年前Python基础

本文实例讲述了python实现希尔排序算法的方法。分享给大家供大家参考。具体如下:

def shellSort(items):
  inc = len(items) / 2
  while inc:
    for i in xrange(len(items)):
      j = i
      temp = items[i]
      while j >= inc and items[j-inc] > temp:
        items[j] = items[j - inc]
        j -= inc
      items[j] = temp
    inc = inc/2 if inc/2 else (0 if inc==1 else 1)
a = [35, -8, 11, 1, 68, 0, 3];
shellSort(a)
print a # [-8, 0, 1, 3, 11, 35, 68]

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

相关文章

python实现根据月份和日期得到星座的方法

本文实例讲述了python实现根据月份和日期得到星座的方法。分享给大家供大家参考。具体实现方法如下: #计算星座 def Zodiac(month, day): n = (u'摩...

Numpy之random函数使用学习

random模块用于生成随机数,下面看看模块中一些常用函数的用法: numpy.random.rand(d0, d1, ..., dn):生成一个[0,1)之间的随机浮点数或N维浮点...

Python编程实现正则删除命令功能

本文实例讲述了Python编程实现正则删除命令功能。分享给大家供大家参考,具体如下: 脚本用途: 在DOS下使用del功能箭头,不支持正则表达式的功能。 脚本实现: import s...

Python(TensorFlow框架)实现手写数字识别系统的方法

Python(TensorFlow框架)实现手写数字识别系统的方法

手写数字识别算法的设计与实现 本文使用python基于TensorFlow设计手写数字识别算法,并编程实现GUI界面,构建手写数字识别系统。这是本人的本科毕业论文课题,当然,这个也是机器...

Python 将pdf转成图片的方法

Python 将pdf转成图片的方法

本篇文章记录如何使用python将pdf文件切分成一张一张图片,包括环境配置、版本兼容问题。 环境配置(mac) 安装ImageMagick brew install imagemagi...