1、创建链表:

from random import randint

class DLinkedNode(object):

    def __init__(self, data=None, pre=None, post=None):
self.data = data
self.pre = pre
self.post = post class DLinkedList(object): def __init__(self):
self.head = DLinkedNode()
self.tail = DLinkedNode()
self.head.post = self.tail
self.tail.pre = self.head def build(self, n):
pre = self.head
for _ in range(n):
data = randint(1, 100)
node = DLinkedNode(data, post=self.tail)
self.tail.pre = node
pre.post = node
node.pre = pre
pre = node return self.head, self.tail

2、快速排序:

class Solution(object):

    def quick_sort(self, low, high):
if not lowor not low.post:
return if low != high:
p, q = low, high
key = p.data
while p != q:
while p != q and q.data >= key:
q = q.pre
p.data = q.data
while p != q and p.data <= key:
p = p.post
q.data = p.data
p.data = key if low != p:
self.quick_sort(low, p.pre)
if p != high:
self.quick_sort(p.post, high)

3、测试:

h, t = DLinkedList().build(10)
curr = h
while curr.post:
print curr.post.data,
curr = curr.post
print()
while curr.pre:
print curr.pre.data,
curr = curr.pre
print() Solution().quick_sort(h.post, h.post, t.pre)
curr = h while curr.post:
print curr.post.data,
curr = curr.post

list快速排序:

import random

class Solution(object):

    def quick_sort(self, a, left, right):

        if left >= right: return

        pivot = self.partition(a, left, right)

        self.quick_sort(a, left, pivot-1)
self.quick_sort(a, pivot+1, right) def partition(self, a, left, right):
index = left + 1
key = a[left]
for i in range(left+1, right+1):
if a[i] <= key:
a[i], a[index] = a[index], a[i]
index += 1
a[left], a[index-1] = a[index-1], key return index-1 if __name__ == '__main__':
a = [random.randint(0, 100) for _ in range(10)]
print(a)
Solution().quick_sort(a, 0, len(a)-1)
print(a)

最新文章

  1. 如何修复Windows 10 Enterprise 在9月更新后图片全部由绘图板打开的情况
  2. java获取当前时间前一周、前一月、前一年的时间
  3. asp.net mvc4 学习笔记一(基本原理)
  4. MvvmLight ToolKit 教程
  5. jQuery知识点总结(第四天)
  6. java基础----&gt;java中正则表达式二
  7. HDU 5795 博弈
  8. hibernate里createSQLQuery
  9. 使用Playground编写第一个Swift程序
  10. hdu 4658 Integer Partition
  11. python正则表达式实例
  12. 编程实战——电影管理器之界面UI及动画切换
  13. nginx重启几种方法
  14. php进阶篇
  15. 使用 python 处理 nc 数据
  16. Redis+Restful 构造序列号和压力测试【后续】
  17. win10x64 批处理自动安装打印机
  18. 配置了yum本地源
  19. module.js:549 throw err;
  20. c# ListBox控件

热门文章

  1. oracle 对应的JDBC驱动 版本
  2. ubuntu登录黑屏“failed to start session”, gdm+kdm+lightdm
  3. iOS:Masonry介绍与使用
  4. Windows数据备份软件Deltacopy-数据备份与还原
  5. VR虚拟现实的工作原理,你知道多少?【转】
  6. Win7如何自定义桌面右键菜单
  7. CentOS系统使用yum安装配置MariaDB数据库
  8. MPJoystick
  9. Android 自己定义控件开发入门(二)
  10. Windows环境下完全手工配置Apache、MySQL和PHP