forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.py
38 lines (33 loc) · 1010 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
"""
Quick sort using last element as pivot index.
Initial array: [23, 45, 12, 78, 90, 1]
-------------- [1, 45, 12, 78, 90, 23]
-------------- [1, 12, 23, 78, 90, 45]
-------------- [1, 12, 23, 45, 90, 78]
-------------- [1, 12, 23, 45, 78, 90]
Sorted array : [1, 12, 23, 45, 78, 90]
"""
def find_pviot_index(A,start,end):
pivot=A[end]
p_index=start
for iter in range(start,end):
if A[iter] <= pivot:
A[p_index],A[iter]=A[iter],A[p_index]
p_index+=1
A[p_index],A[end]=pivot,A[p_index]
return p_index
def quick_sort(A,start,end):
if start < end:
pivot_index=find_pviot_index(A,start,end)
print("--------------",A)
quick_sort(A,start,pivot_index-1)
quick_sort(A,pivot_index+1,end)
#main
A=list()
n=int(input("Enter how many numbers you want ot enter: "))
for x in range(0,n):
num=int(input("enter num:"))
A.append(num)
print("Initial array:",A)
quick_sort(A,0,n-1)
print("Sorted array :",A)