forked from gzc/CLRS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhoare.py
More file actions
32 lines (27 loc) · 656 Bytes
/
hoare.py
File metadata and controls
32 lines (27 loc) · 656 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
31
32
#!/usr/bin/env python
# coding=utf-8
def quicksort(items, p, r):
if p < r:
q = partition(items, p, r)
quicksort(items, p, q)
quicksort(items, q+1, r)
def partition(items, p, r):
x = items[p]
i = p - 1
j = r + 1
while True:
while True:
j = j - 1
if items[j] <= x:
break
while True:
i = i + 1
if items[i] >= x:
break
if i < j:
items[i],items[j] = items[j],items[i]
else:
return j
items = [13,19,9,5,12,8,7,4,11,2,6,21]
quicksort(items, 0, len(items)-1)
print items