-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_sort.py
50 lines (40 loc) · 1004 Bytes
/
quick_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May 26 10:05:50 2020
Quick Sort
快速排序
@author: xueqiwang
"""
def partition(nums, left, right):
i = left
j = right
temp = nums[i]
while i < j:
# 从右往左找小于temp的数
while i < j and nums[j] >= temp:
j -= 1
if i < j:
# 交换
nums[i] = nums[j]
i += 1
# 从左往右找大于temp的数
while i < j and nums[i] <= temp:
i += 1
if i < j:
# 交换
nums[j] = nums[i]
j -= 1
nums[i] = temp
return nums, i
def QuickSort(nums, left, right):
if left < right:
nums, i = partition(nums, left, right)
#print(nums)
nums = QuickSort(nums, left, i-1)
#print(nums)
nums = QuickSort(nums, i+1, right)
#print(nums)
return nums
nums = [5,5,4,3,2,1,6]
print(QuickSort(nums, 0, 6))