排序算法(快速排序)

快速排序算法效率高,运行稳定的算法。jdk 内置就是采用的快速排序算法。

和归并排序相似快排也是采用分治法思想,将待排数列分成两部分,取一个参照元素,从两端到中间依次比较所有元素,将较小和较大元素分开。

然后重复这个过程,直至分到一个列表只有一个元素。

 1 """
 2 @Author TZG
 3 @Email 1651504722@qq.com
 4 """
 5 import random
 6 import time
 7 import sys
 8 
 9 
10 # 生成一个长度1w的随机数列
11 a = []
12 for i in range(10000):
13     a.append(random.randint(0, 10000))
14 
15 print(len(a))
16 temp = a[:]
17 temp[5:9995] = ['...']
18 print(temp)
19 
20 
21 # 快速排序
22 def quickSortTurn(L, low, high):
23     i = low
24     j = high
25     if i >= j:
26         return
27     key = L[i]
28     while i < j:
29         while i < j and L[j] >= key:
30             j = j - 1
31         L[i] = L[j]
32         while i < j and L[i] <= key:
33             i = i + 1
34         L[j] = L[i]
35     L[i] = key
36     quickSortTurn(L, low, i - 1)
37     quickSortTurn(L, j + 1, high)
38     return L
39 
40 
41 def quickSort(L):
42     return quickSortTurn(L, 0, len(L)-1)
43 
44 
45 # 快速排序开始
46 begin = time.time()
47 b = quickSort(a)
48 end = time.time()
49 print(len(b))
50 temp = b[:]
51 temp[5:9995] = ['...']
52 print(temp)
53 t3 = (end - begin)
54 print("耗时:%ss" % t3)

输出:

1 10000
2 [4987, 3878, 5304, 3628, 3937, '...', 7613, 4245, 5005, 7630, 1]
3 10000
4 [1, 3, 3, 8, 8, '...', 9990, 9991, 9992, 9997, 9998]
5 耗时:0.025002479553222656s

 

转载于:https://www.cnblogs.com/t0000/p/9378364.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值