forked from Shambhavi8/DataScienceMasters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickcount2.py
64 lines (53 loc) · 1.12 KB
/
quickcount2.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
count = 0
#import sys
#sys.setrecursionlimit(10010000)
f = open("Quicksort.txt")
a = list()
for line in f:
a.append(int(line))
def bigger(a,b):
if a > b:
return a
else:
return b
def biggest(a,b,c):
return bigger(a,bigger(b,c))
def median(a,b,c):
largest = biggest(a,b,c)
if a == largest:
median = bigger(b, c)
elif b == largest:
median = bigger(a, c)
else:
median = bigger(a, b)
return median
def partition(a,l,r):
global count
count = count + len(a[l:r])
#print count
#print l
#a[l], a[r] = a[r], a[l] #--------------------> for part 2 of the question
e = median(a[l], a[(l+r)/2], a[r])
x = a.index(e) #----------------------> for part 3 of question
a[l], a[x] = a[x], a[l]
p = a[l]
i = l + 1
j = 0
j = l + 1
while j <=r:
if a[j] < p:
a[i], a[j] = a[j], a[i]
i = i + 1
j = j + 1
a[l], a[i-1] = a[i-1], a[l]
#print a
return (i-1)
def quicksort(a,l,r):
if l < r:
j = partition(a,l,r)
quicksort(a,l,j-1)
#if (j-1) >= 0:
quicksort(a,j+1,r)
return a
print quicksort(a,0,a.index(a[-1]))
print count