-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.py
41 lines (35 loc) · 809 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
#!/bin/python3
'''
Author: Sai Uday Bhaskar Mudivarthy
Program: Quick Sort
Time Complexity: O(n log n)
Worst Case: O(n*n)
'''
import sys
from array import *
def Swap(A,i,j):
temp = A[i]
A[i]=A[j]
A[j]=temp
def QuickSort(A,start,end):
if start<end:
pIndex = PartitionIndex(A,start,end)
QuickSort(A,start,pIndex-1)
QuickSort(A,pIndex+1,end)
def PartitionIndex(A,start,end):
pivot = A[end]
pIndex = start
for i in range(start,end):
if(A[i] <= pivot):
Swap(A,i,pIndex)
pIndex = pIndex +1
Swap(A,pIndex,end)
return pIndex
n = input().strip().split(' ')
intarray = array('i',list(map(int,n)))
QuickSort(intarray,0,len(intarray)-1)
print()
print("Sorted List Follows")
for i in intarray:
print(i,end=" ")
print()