forked from mayankchaudhary26/HacktoberFest-Practice-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.py
More file actions
40 lines (37 loc) Β· 701 Bytes
/
quickSort.py
File metadata and controls
40 lines (37 loc) Β· 701 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
33
34
35
36
37
38
39
40
def partition(a,l,r):
p=a[l]
i=l
j=r+1
while True:
while True: #i or while i < j-1: for worst case input error
i=i+1
if a[i] >= p:
break
while True: #j
j=j-1
if a[j] <= p:
break
a[i] , a[j] = a[j] , a[i] #swap i,j
print(a)
if i >= j:
break
a[i] , a[j] = a[j] , a[i] #reswap i,j
print(a)
a[l] , a[j] = a[j] , a[l] #swap pivot,j
print(a)
return j
def quickSort(a,l,r):
if l < r:
s= partition(a,l,r)
quickSort(a,l,s-1)
quickSort(a,s+1,r)
def main():
a=[]
n=eval(input("Enter the number of elements in array : "))
print("Enter the numbers : ")
for i in range (n):
a.append(eval(input()))
print(a)
quickSort(a,0,n-1)
print(a)
main()