forked from vksahu399/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick Sort.py
More file actions
30 lines (26 loc) · 708 Bytes
/
Quick Sort.py
File metadata and controls
30 lines (26 loc) · 708 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
# Implementation of Quick Sort
# Function to swap values
def swap(Arr,i,j):
temp = Arr[i]
Arr[i] = Arr[j]
Arr[j] = temp
# Function to find the partition position
def partition(Arr,l,r):
pivot = Arr[r]
i = l - 1
for j in range(l,r):
if Arr[j] < pivot:
i = i + 1
swap(Arr,i,j)
swap(Arr,i+1,r)
return i+1
# Function to perform quicksort
def QuickSort(Arr,l,r):
if l < r:
pi = partition(Arr,l,r)
QuickSort(Arr,l,pi-1)
QuickSort(Arr,pi+1,r)
N = int(input("Enter number of elements: "))
Arr = list(map(int,input("Enter elements: ").strip().split()))[:N]
QuickSort(Arr,0,N-1)
print("Sorted Array: ",*Arr)