-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.py
34 lines (28 loc) · 907 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
import random
class quicksort:
def __init__(self,list):
self.list = list
length = len(list)
self.list = self.sort(list,0,length)
print(self.list)
def sort(self,list,low,high):
if low>=high:
return list
if low!=high:
self.pivot_index = random.randint(low,high-1)
self.pivot = list[self.pivot_index]
start = low
end=high-1
while(start<end):
while(list[start]<self.pivot):
start+=1
while(list[end]>self.pivot):
end-=1
if start<end:
list[start],list[end] = list[end],list[start]
self.pivot_index = end
list = self.sort(list,low,self.pivot_index)
list = self.sort(list,self.pivot_index+1,high)
return list
list = [1,7,6,4,8,0,2]
object = quicksort(list)