-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.py
More file actions
76 lines (64 loc) · 1.71 KB
/
quicksort.py
File metadata and controls
76 lines (64 loc) · 1.71 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
'''def partition(arr,low,high):
pivot=arr[low]
i=low+1
j=high
while(i<j):
while(i<j and arr[i]<pivot):
i+=1
while(arr[j]>pivot):
j-=1
if(i<j):
temp=arr[i]
arr[i]=arr[j]
arr[j]=temp'''
'''def partition(arr,low,high):
i = ( low-1 )
print(i)
# index of smaller element
pivot = arr[high] # pivot
for j in range(low , high):
# If current element is smaller than the pivot
if arr[j] < pivot:
# increment index of smaller element
i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )'''
def partition(arr,beg,end):
left=beg
right=end
lock=beg
while(left<right):
while(arr[lock]<=arr[right] and right!=lock):
right -=1
if lock==right:
return lock
if(arr[lock]>arr[right]):
temp=arr[lock]
arr[lock]=arr[right]
arr[right]=temp
lock=right
while(arr[lock]>=arr[left] and left!=lock):
left +=1
if lock==left:
return lock
if(arr[lock]<arr[left]):
temp=arr[lock]
arr[lock]=arr[left]
arr[left]=temp
lock=left
return lock
l=[10,7,8,9,1,5]
print(l)
a=partition(l,0,5)
print(a)
print(l)
def quicksort(arr,low,high):
if low<high:
print(arr)
pi=partition(arr,low,high)
print(pi)
quicksort(arr,low,pi-1)
quicksort(arr,pi+1,high)
quicksort(l,0,5)
print(l)