Chinaunix首页 | 论坛 | 博客
  • 博客访问: 58250
  • 博文数量: 13
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 199
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-07 22:11
文章分类

全部博文(13)

文章存档

2014年(13)

我的朋友

分类: Python/Ruby

2014-07-22 23:46:50

wiki几类排序算法原理后,写起来就不费劲了,有瑕疵,还请见谅。

点击(此处)折叠或打开

  1. #冒泡排序
  2. def bulle_sort(so,so_1):
  3.     for i in range(so_1):
  4.         int (i)
  5.         if so[i:i+1]>so[i+1:i+2]:
  6.             temp=so[i+1:i+2]
  7.             so[i+1:i+2]=so[i:i+1]
  8.             so[i:i+1]=temp
  9.             so_1=len(so)-1
  10.             return (bulle_sort(so,so_1)) #递归函数
  11. #直接插入排序
  12. def Insert_sort(so):
  13.     ss=[]
  14.     for i in range(len(so)-1):
  15.         ss=so[i:i+1]
  16.         if so[i:i+1]<so[i+1:i+2]:
  17.             ss=ss.extend(so[i+1:i+2])
  18.         else:
  19.             soi=so[i+1:i+2]
  20.             ss=soi.extend(ss[i:i+1])
  21.     so=ss
  22.     return (so)
  23. #直接选择排序
  24. def Select_sort(so,n):
  25.     mi=[]
  26.     small=0
  27.     for i in range(0,n):
  28.         small=i
  29.         j=i+1
  30.         for j in range(j,n):
  31.             if so[j]<so[small]:
  32.                 small=j
  33.         if small!=i:
  34.             mi=so[i]
  35.             so[i]=so[small]
  36.             so[small]=mi
  37.     return (so)
  38. #堆排序
  39. def Heap_sort(so):
  40.     def Create_heap(start,end):
  41.         root=start
  42.         
  43.         while True:
  44.                 child=(2*root+1)
  45.                 if child > end:
  46.                     break
  47.                 if (child+1)<=end and so[child]<so[child+1]:
  48.                     child+=1
  49.                 if so[root]<so[child]:
  50.                     so[root],so[child]=so[child],so[root]
  51.                     root=child
  52.                 else:
  53.                     break
  54.     for i in range(int ((len(so)-2)/2),-1,-1):
  55.         Create_heap(i,len(so)-1)
  56.     for n in range(len(so)-1,0,-1):
  57.         so[0],so[n]=so[n],so[0]
  58.         Create_heap(0,n-1)
  59.     return (so)
  60. #快速排序
  61. def Quick_sort(so,low,high):
  62.     tmp=so[low]
  63.     i=low
  64.     j=high
  65.     while i<j:
  66.         while i<j and so[j]>=tmp:
  67.             j=j-1
  68.         if i<j:
  69.             so[i]=so[j]
  70.             i=i+1
  71.         while i<j and so[i]<tmp:
  72.             i=i+1
  73.         if i<j:
  74.             so[j]=so[i]
  75.             j=j-1
  76.     so[i]=tmp
  77.     if low<i:
  78.         Quick_sort(so,low,i-1)
  79.     if i<high:
  80.         Quick_sort(so,j+1,high)


  81. if __name__=='__main__':
  82.     so=[3,4,33,7,9,3,10,2,88]
  83.     so_1=len(so)

  84.     bulle_sort(so,so_1)
  85.     print ("Bulle sort:")
  86.     print (so)

  87.     Insert_sort(so)
  88.     print ("Insert sort:")
  89.     print (so)

  90.     Select_sort(so,so_1)
  91.     print ("Select sort:")
  92.     print (so)
  93.     
  94.     Heap_sort(so)
  95.     print ("Heap sort:")
  96.     print (so)

  97.     Quick_sort(so,0,(len(so)-1))
  98.     print ("Quick sort:")
  99.     print (so)

阅读(1482) | 评论(0) | 转发(0) |
0

上一篇:浅探python

下一篇:没有了

给主人留下些什么吧!~~