Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7612162
  • 博文数量: 1769
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16352
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1769)

文章存档

2024年(15)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: Python/Ruby

2012-01-03 22:12:45

前段时间为准备百度面试恶补的东西,虽然最后还是被刷了,还是把那几天的“战利品”放点上来,算法一直是自己比较薄弱的地方,以后还要更加努力啊。

下面用Python实现了几个常用的排序,如快速排序,选择排序,以及二路并归排序等等。

 

  1. #encoding=utf-8
  2. import random
  3. from copy import copy

  4. def directInsertSort(seq):
  5.     """ 直接插入排序 """
  6.     size = len(seq)
  7.     for i in range(1,size):
  8.         tmp, j = seq[i], i
  9.         while j > 0 and tmp < seq[j-1]:
  10.             seq[j], j = seq[j-1], j-1
  11.         seq[j] = tmp
  12.     return seq

  13. def directSelectSort(seq):
  14.     """ 直接选择排序 """
  15.     size = len(seq)
  16.     for i in range(0,size - 1):
  17.         k = i;j = i+1
  18.         while j < size:
  19.             if seq[j] < seq[k]:
  20.                 k = j
  21.             j += 1
  22.         seq[i],seq[k] = seq[k],seq[i]
  23.     return seq

  24. def bubbleSort(seq):
  25.     """冒泡排序"""
  26.     size = len(seq)
  27.     for i in range(1,size):
  28.         for j in range(0,size-i):
  29.             if seq[j+1] < seq[j]:
  30.                 seq[j+1],seq[j] = seq[j],seq[j+1]
  31.     return seq

  32. def _divide(seq, low, high):
  33.     """快速排序划分函数"""
  34.     tmp = seq[low]
  35.     while low != high:
  36.         while low < high and seq[high] >= tmp: high -= 1
  37.         if low < high:
  38.             seq[low] = seq[high]
  39.             low += 1
  40.         while low < high and seq[low] <= tmp: low += 1
  41.         if low < high:
  42.             seq[high] = seq[low]
  43.             high -= 1
  44.     seq[low] = tmp
  45.     return low

  46. def _quickSort(seq, low, high):
  47.     """快速排序辅助函数"""
  48.     if low >= high: return
  49.     mid = _divide(seq, low, high)
  50.     _quickSort(seq, low, mid - 1)
  51.     _quickSort(seq, mid + 1, high)

  52. def quickSort(seq):
  53.     """快速排序包裹函数"""
  54.     size = len(seq)
  55.     _quickSort(seq, 0, size - 1)
  56.     return seq

  57. def merge(seq, left, mid, right):
  58.     tmp = []
  59.     i, j = left, mid
  60.     while i < mid and j <= right:
  61.         if seq[i] < seq[j]:
  62.             tmp.append(seq[i])
  63.             i += 1
  64.         else:
  65.             tmp.append(seq[j])
  66.             j += 1
  67.     if i < mid: tmp.extend(seq[i:])
  68.     if j <= right: tmp.extend(seq[j:])

  69.     seq[left:right+1] = tmp[0:right-left+1]

  70. def _mergeSort(seq, left, right):
  71.     if left == right:
  72.         return
  73.     else:
  74.         mid = (left + right) / 2
  75.         _mergeSort(seq, left, mid)
  76.         _mergeSort(seq, mid + 1, right)
  77.         merge(seq, left, mid+1, right)

  78. #二路并归排序
  79. def mergeSort(seq):
  80.     size = len(seq)
  81.     _mergeSort(seq, 0, size - 1)
  82.     return seq

  83. if __name__ == '__main__':
  84.     s = [random.randint(0,100) for i in range(0,20)]
  85.     print s
  86.     print "\n"
  87.     print directSelectSort(copy(s))
  88.     print directInsertSort(copy(s))
  89.     print bubbleSort(copy(s))
  90.     print quickSort(copy(s))
  91.     print mergeSort(copy(s))

 

运行结果如下:

1 2 3 4 5 6 7 8 9 E:\python_project\practice>sorting.py [10, 47, 56, 76, 64, 84, 26, 8, 47, 51, 88, 81, 32, 95, 91, 29, 28, 69, 61, 45]     [8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95] [8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95] [8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95] [8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95] [8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]

转:

 

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

上一篇:MooConf B/C

下一篇:spark scala

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