forked from mejtfk/Algorithms-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomized_quicksort.py
More file actions
42 lines (35 loc) · 888 Bytes
/
randomized_quicksort.py
File metadata and controls
42 lines (35 loc) · 888 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
41
42
import random
'''
Algorithm used from pseudo-code from Introduction to Algorthms Third Edition, MIT Press
'''
def swap(A,i,j):
x=A[i]
A[i]=A[j]
A[j]=x
return A
def partition(A, p, r):
x=A[r]
i=p-1
for j in (range(p,r)):
if A[j] <= x:
i+=1
swap(A,i,j)#exchange A[i] with A[j]
swap(A,i+1,r)#exchange A[i+1] with A[r]
return i+1
def randomized_partition (A, p, r):
i=random.randrange(p,r)
swap(A, r, i)
return partition(A, p, r)
def randomized_quicksort(A, p, r):
if p < r:
q = randomized_partition(A, p, r)
randomized_quicksort(A, p, q-1)
randomized_quicksort(A, q+1, r)
if __name__ == '__main__':
A=[2,8,7,1,3,5,6,4]
print("Before sorting: ", end='')
print(A)
print()
print("After sorting: ", end='')
randomized_quicksort(A, 0, len(A)-1)
print(A)