forked from jeklen/quicksort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.py
50 lines (30 loc) · 819 Bytes
/
quicksort.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
def quicksort(A, p, r):
if (p < r):
q = partition(A, p, r)
quicksort(A, p, q-1)
quicksort(A, q+1, r)
# when partitioning, data is three parts: smaller, bigger, unknown
def partition(A, p, r):
x = A[r]
i = p - 1
# notice that j is from p to r - 1
for j in range(p, r):
if (A[j] <= x):
i = i + 1
A[i], A[j] = A[j], A[i]
A[r], A[i+1] = A[i+1], A[r]
return i + 1
def partition2(A, p, r):
x = A[r]
i = p
# notice that j is from p to r - 1
for j in range(p, r):
if (A[j] <= x):
A[i], A[j] = A[j], A[i]
i = i + 1
A[r], A[i] = A[i], A[r]
return i
def partition2(A, si, di):
A = [1, 9, 2, 3, -1, 10]
quicksort(A, 0, 5)
print(A)