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实现迭代时使用索引的方法。分享给大家供大家参考,具体如下: 索引迭代 Python中,迭代永远是取出元素本身,而非元素的索引。 对于有序集合,元素确实是有索引的...

django之对FileField字段的upload_to的设定方法

用django开发,经常要处理用户上传的文件, 比如user模型里面如果又个人头像的字段 ImageField等等,而django在FielField字段(包括ImageField)的支...

caffe binaryproto 与 npy相互转换的实例讲解

在caffe中,如果使用的是c++接口,均值文件默认为.binaryproto格式,而如果使用的是python接口,均值文件默认的是numpy的.npy格式,在工作中有时需要将两者进行互...

python对字典进行排序实例

本文实例讲述了python对字典进行排序的方法,是非常实用的技巧。分享给大家供大家参考。 具体实现方法如下: import itertools thekeys = ['b','a'...

tensorflow更改变量的值实例

如下所示: from __future__ import print_function,division import tensorflow as tf #create a Var...