Quick Sort

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CIBM%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

使用python实现了快速排序,两种策略:
1
、直接选择第一个为基准值。

  1. #!/usr/bin/python
  2. #coding: UTF-8

  3. """
  4. Quick Sort.

  5. Author : Hegc Huang
  6. Time   : 2008-12-21
  7. """
  8. import math

  9. data = [49173683715291807102345672, -25]


  10. count = 0
  11. c = 0
  12. def qsort (d, left, l):
  13.     global count, c
  14.     start = left
  15.     base = d[left]
  16.     right = left+1
  17.     while right < l:
  18.         move = d[right]
  19.         if (base>move and left < right):
  20.             d[right] = base
  21.             d[left]  = move
  22.             tmp = left
  23.             left = right
  24.             right = tmp
  25.             c += 1
  26.         elif (base<move and left>right) :
  27.             d[right] = base
  28.             d[left]  = move
  29.             left = right
  30.             c += 1
  31.         right += 1
  32.     count += 1
  33.     print "%4d " % count,  d 
  34.     if left > start+1:
  35.         qsort(d, start, left)
  36.     if left < l-1:
  37.         qsort(d, left+1, l)

  38. qsort(data, 0, len(data))
  39. print u"排序结果:", data
  40. print u"预计比较次数", len(data)*math.log10(len(data))
  41. print u"实际比较次数:", c
rel="File-List" href="file:///C:%5CDOCUME%7E1%5CIBM%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C02%5Cclip_filelist.xml">

2、在每次的数组序列中随机挑选一个作为基准值:

  1. #!/usr/bin/python
  2. #coding: UTF-8

  3. """
  4. Quick Sort with Random Selection.

  5. Author : Hegc Huang
  6. Time   : 2008-12-21
  7. """
  8. import math
  9. import random
  10. import time

  11. data = [49173683715291807102345672, -25]


  12. count = 0
  13. c = 0
  14. def qsort (d, left, le):
  15.     global count, c
  16.     start = left
  17.     random.seed(time.time())
  18.     left = random.randrange(left, le)
  19.     base = d[left]
  20.     right = start
  21.     while right < le:
  22.         move = d[right]
  23.         if (base>move and left < right):
  24.             d[right] = base
  25.             d[left]  = move
  26.             tmp = left
  27.             left = right
  28.             right = tmp
  29.             c += 1
  30.         elif (base<move and left>right):
  31.             d[right] = base
  32.             d[left]  = move
  33.             left = right
  34.             c += 1
  35.         right += 1
  36.     count += 1
  37.     print "%4d " % count,  d 
  38.     if left > start+1:
  39.         qsort(d, start, left)
  40.     if left < le-1:
  41.         qsort(d, left+1, le)

  42. qsort(data, 0, len(data))
  43. print u"排序结果:", data
  44. print u"预计比较次数", len(data)*math.log10(len(data))
  45. print u"实际比较次数:", c


rel="File-List" href="file:///C:%5CDOCUME%7E1%5CIBM%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C03%5Cclip_filelist.xml"> 这两种方法对于同一个序列[4, 9, 17, 3, 6, 8, 37, 15, 29, 18, 0, 7, 102, 34, 56, 72, -2, 5]18个数字)的测试情况为:
第一种方法:进入qsort()方法13次,比较交换了36次。
第二种方法:结果如下,第一列是序号,第儿列是进入qsort的次数,最后是对应的比较交换的次数。

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CIBM%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C07%5Cclip_filelist.xml">

1

15

24

2

16

38

3

14

40

4

15

36

5

16

44

6

15

30

7

12

34

8

16

36

9

17

42

10

16

46

11

15

34

12

14

24

13

14

28

14

15

40

15

16

30

16

12

40

17

13

22

18

13

32

19

15

24

20

13

30


最后的平均水平是进入qsort函数14.6次,比较交换了33.7次,与第一种方法的结果差别不大,而且稳定性差,最好的时候比较交换22次,最坏的时候46次。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Quick sort is a popular sorting algorithm that works by partitioning an array into two sub-arrays, and then recursively sorting each sub-array. It is a divide-and-conquer algorithm that has an average time complexity of O(n log n), making it one of the fastest sorting algorithms. The basic idea behind quick sort is to select a pivot element, partition the array around the pivot element, and then recursively apply the same process to each of the sub-arrays. The partitioning process involves selecting a pivot element, rearranging the array so that all elements less than the pivot are on one side and all elements greater than the pivot are on the other side, and then returning the index of the pivot element. This pivot index is then used to divide the array into two sub-arrays, which are recursively sorted. Here's an example implementation of quick sort in Python: ``` def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[0] left = [] right = [] for i in range(1, len(arr)): if arr[i] < pivot: left.append(arr[i]) else: right.append(arr[i]) return quick_sort(left) + [pivot] + quick_sort(right) ``` This implementation selects the first element of the array as the pivot, and then uses list comprehensions to create the left and right sub-arrays. The left sub-array contains all elements less than the pivot, while the right sub-array contains all elements greater than or equal to the pivot. The function then recursively sorts the left and right sub-arrays and combines them with the pivot element to produce the final sorted array.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值