快排
递归
算法思想
在待排序数组中找到一个元素作为标准元素t,在数组中将所有小于t的元素放在t左侧,所有大于t的元素放在t的右侧(从小到大排序,否则反过来)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15def qs(l,r,nums):
if l<r:
i = l
j = r
key = nums[l]
while i<j:
while i<j and nums[j]>=key:
j-=1
nums[i]=nums[j]
while i<j and nums[i]<=key:
i+=1
nums[j]= nums[i]
nums[i] = key
qs(l,i-1,nums)
qs(i+1,r,nums)
归并排序
1 |
|