-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathav33qsort.py
65 lines (54 loc) · 973 Bytes
/
av33qsort.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def swap(x, i,j):
a=x[i]
x[i]=x[j]
x[j]=a
#pivot around last element and return its position
def pivot(x,l,h):
print()
pv=x[h]
p=None
# for i in range(l,h-1):
# for j in range(i,h-1):
# if x[j]<pv:
# swap(i,j)
l1=[]
l2=[]
for i in range(l,h):
if x[i]<=pv:
l1.append(x[i])
else:
l2.append(x[i])
i=l
for v in l1:
x[i]=v
i+=1
x[i]=pv
p=i
i+=1
for v in l2:
x[i]=v
i+=1
return p
#pivot around last element and return its position
def pivot2(x,l,h):
print()
pv=x[h]
p=None
i=0
j=0
while j<=h-1:
if x[j]<=pv:
swap(x,i,j)
i=i+1
j+=1
swap(x,i,h)
return i
def qsortx(x,l,h):
if l>= h:
return
p=pivot2(x,l,h)
qsortx(x,l,p-1)
qsortx(x,p+1,h)
x=[3,2, 5, 4,7, -4,3]
qsortx(x,0,len(x)-1)
print(x)