1. 具体原理我这里就不解释了,可以查看数据结构课本或者百度百科
    这里只给出相应的代码(很简洁)
  2.  
  3. 1 __author__ = "WSX"
  4. class sort:
  5. def __init__(self,):
  6. pass
  7.  
  8. def merger(self, left , right): # 合并两个有序列表
  9. i = 0; j = 0 ; result = []
  10. while left and right:
  11. if left[i] <= right[j]:
  12. result.append(left.pop(0))
  13. else:
  14. result.append(right.pop(0))
  15. result += left[:]
  16. result += right[:]
  17. return result
  18.  
  19. def mergeSort(self, L): #归并排序
  20. if len(L) == 1:
  21. return L
  22. mid = len(L) // 2
  23. left = self.mergeSort(L[:mid])
  24. right = self.mergeSort(L[mid:])
  25. return self.merger(left, right)
  26.  
  27. def insert1(self, L): #直接插入排序 俩个for实现
  28. for i in range(1,len(L)):
  29. temp = L[i]
  30. for j in range(i-1,-1,-1):
  31. if L[i] < L[j]:
  32. L[j+1] = L[j]
  33. else:
  34. j += 1
  35. break
  36. L[j] = temp
  37. return L
  38.  
  39. def insert2(self, L): #直接插入排序 for 结合 while
  40. for i in range(1, len(L)):
  41. temp = L[i]
  42. j = i-1
  43. while j >=0 and temp < L[j]:
  44. L[j+1] = L[j]
  45. j -= 1
  46. L[j+1] = temp
  47. return L
  48.  
  49. def bubblk(self, L): #冒泡排序
  50. for i in range(len(L)-1):
  51. for j in range(len(L)-i-1):
  52. if L[j+1] < L[j]:
  53. L[j+1], L[j] = L[j], L[j+1]
  54. return L
  55.  
  56. def quickSort1(self,L): #快速排序
  57. if len(L) <= 1:
  58. return L
  59. else:
  60. return self.quickSort1([i for i in L[1:] if i < L[0]]) + [L[0]] + self.quickSort1([j for j in L[1:] if j > L[0]])
  61.  
  62. def shellSort(self, L):
  63. pass
  64.  
  65. def selectSort(self, L): #选择排序
  66. for i in range(len(L) -1 ):
  67. index = i
  68. for j in range(i+1, len(L)):
  69. if L[j] < L[index]:
  70. index = j
  71. L[index], L[i] = L[i] ,L[index]
  72. return L
  73.  
  74. def sortHeap(self,L): #堆排序
  75. def heap(L, size, root):
  76. larger = root
  77. left = 2 * root + 1;
  78. right = 2 * root + 2
  79. if left < size and L[left] > L[larger]:
  80. larger = left
  81. if right < size and L[right] > L[larger]:
  82. larger = right
  83. if larger != root:
  84. L[larger], L[root] = L[root], L[larger]
  85. heap(L, size, larger)
  86.  
  87. def bulidHeap(L): # 初始化堆
  88. heapsize = len(L)
  89. for i in range(heapsize // 2, -1, -1):
  90. heap(L, heapsize, i)
  91.  
  92. bulidHeap(L)
  93. for i in range(len(L) - 1, -1, -1):
  94. L[0], L[i] = L[i], L[0]
  95. heap(L, i, 0)
  96. return L
  97.  
  98. a = sort()
  99. print(a.sortHeap([25,3,9,7,55,96]))

经典排序的python实现的更多相关文章

  1. 十大经典排序算法(Python,Java实现)

    参照:https://www.cnblogs.com/wuxinyan/p/8615127.html https://www.cnblogs.com/onepixel/articles/7674659 ...

  2. 经典排序方法 python

    数据的排序是在解决实际问题时经常用到的步骤,也是数据结构的考点之一,下面介绍10种经典的排序方法. 首先,排序方法可以大体分为插入排序.选择排序.交换排序.归并排序和桶排序四大类,其中,插入排序又分为 ...

  3. 经典排序算法总结与实现 ---python

    原文:http://wuchong.me/blog/2014/02/09/algorithm-sort-summary/ 经典排序算法在面试中占有很大的比重,也是基础,为了未雨绸缪,在寒假里整理并用P ...

  4. 经典排序算法及python实现

    今天我们来谈谈几种经典排序算法,然后用python来实现,最后通过数据来比较几个算法时间 选择排序 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理是每一次从待排序的数据 ...

  5. 十大经典排序算法(python实现)(原创)

    个人最喜欢的排序方法是非比较类的计数排序,简单粗暴.专治花里胡哨!!! 使用场景: 1,空间复杂度 越低越好.n值较大: 堆排序 O(nlog2n) O(1) 2,无空间复杂度要求.n值较大: 桶排序 ...

  6. 经典排序算法的总结及其Python实现

    经典排序算法总结: 结论: 排序算法无绝对优劣之分. 不稳定的排序算法有:选择排序.希尔排序.快速排序.堆排序(口诀:“快速.选择.希尔.堆”).其他排序算法均为稳定的排序算法. 第一趟排序后就能确定 ...

  7. python 经典排序算法

    python 经典排序算法 排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存.常见的内部排序算 ...

  8. 经典排序算法及总结(python实现)

    目录 1.排序的基本概念和分类 排序的稳定性: 内排序和外排序 影响内排序算法性能的三个因素: 根据排序过程中借助的主要操作,可把内排序分为: 按照算法复杂度可分为两类: 2.冒泡排序 BubbleS ...

  9. python实现十大经典排序算法

    Python实现十大经典排序算法 代码最后面会给出完整版,或者可以从我的Githubfork,想看动图的同学可以去这里看看: 小结: 运行方式,将最后面的代码copy出去,直接python sort. ...

随机推荐

  1. Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c

    队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCL ...

  2. iOS单选和全选

    在日常开发中单选.多选.全选经常遇到,所以写一个demo放上来供大家参考, 先看效果图: Demo地址:https://github.com/domanc/SingleAndAllSelect.git

  3. Opencv Convex Hull (凸包)

    #include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...

  4. 如何清除保存的FTP用户名和密码

      很多人习惯登陆FTP时选择保存密码,这样下次只需打开地址就可以进入FTP的页面了.这样确实方便,但如果遇到更换别的FTP用户名登陆,该怎么办?相信不少人还真答不出.重装浏览器,或者重装系统?呵呵, ...

  5. jQuery开发者眼中的AngularJS

    文章来源:http://blog.jobbole.com/76265/ AngualrJS是一个很贴心的web应用框架.它有很不错的官方文档和示例:经过在现实环境中的测试著名的TodoMVC proj ...

  6. udacity term_sim.x86_64 ubuntu16.04 Vmware

    打印信息 ./term2_sim.x86_64 Set current directory to /home/mwolfram/udacity/sdcnd/term2/term2_sim_linux ...

  7. [C++] Lvalue and Rvalue Reference

    Lvalue and Rvalue Reference int a = 10;// a is in stack int& ra = a; // 左值引用 int* && pa ...

  8. query使用

    1.row_array():返回查询结果中的第一条数据 include APP_PATH . "../mysql.class.php";$db = new mysql();$sql ...

  9. 熟悉相关电路,控制I/O口,且配置相关参数,LED,光敏,74LS164数码管

    1.掌握zigbee无线模块的基本工作电路. 2.上面芯片跟仿真器连接需要5根线,电源.地.复位.P2_1.P2_2. 输出的配置:a.首先要让相应IO口处于普通IO口模式,非片上外设的模式:b.让普 ...

  10. 重叠io操作

    第一章 一. 重叠模型的优点 1. 可以运行在支持Winsock2的所有Windows平台 ,而不像完成端口只是支持NT系统. 2. 比起阻塞.select.WSAAsyncSelect以及WSAEv ...