-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.py
More file actions
30 lines (27 loc) · 755 Bytes
/
Copy pathQuickSort.py
File metadata and controls
30 lines (27 loc) · 755 Bytes
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
import numpy as np
import math
#返回排好序的一个元素的索引
# arr = [start],(start,partIdx],[partIdx+1,end-1]
def _partition(arr,start,end):
flagCmp = arr[start]
partIdx = start
index = start
while(index <= end):
if arr[index] < flagCmp:
temp = arr[partIdx+1]
arr[partIdx+1] = arr[index]
arr[index] = temp
partIdx += 1
index += 1
temp = arr[start]
arr[start] = arr[partIdx]
arr[partIdx] = temp
return partIdx
def _quickSort(arr,start,end):
if start >= end:
return
pIdx = _partition(arr,start,end)
_quickSort(arr,start,pIdx-1)
_quickSort(arr,pIdx+1,end)
def quickSort(arr):
_quickSort(arr,0,len(arr)-1)