Skip to content

Commit 6dbff6b

Browse files
authored
Merge pull request #133 from divsriv111/master
Added Quick Sort algorithm
2 parents 8b72a37 + 3c322f3 commit 6dbff6b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def quickSort(alist):
2+
quickSortHelper(alist,0,len(alist)-1)
3+
4+
def quickSortHelper(alist,first,last):
5+
if first<last:
6+
7+
splitpoint = partition(alist,first,last)
8+
9+
quickSortHelper(alist,first,splitpoint-1)
10+
quickSortHelper(alist,splitpoint+1,last)
11+
12+
13+
def partition(alist,first,last):
14+
pivotvalue = alist[first]
15+
16+
leftmark = first+1
17+
rightmark = last
18+
19+
done = False
20+
while not done:
21+
22+
while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
23+
leftmark = leftmark + 1
24+
25+
while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
26+
rightmark = rightmark -1
27+
28+
if rightmark < leftmark:
29+
done = True
30+
else:
31+
temp = alist[leftmark]
32+
alist[leftmark] = alist[rightmark]
33+
alist[rightmark] = temp
34+
35+
temp = alist[first]
36+
alist[first] = alist[rightmark]
37+
alist[rightmark] = temp
38+
39+
40+
return rightmark
41+
42+
alist = [54,26,93,17,77,31,44,55,20]
43+
quickSort(alist)
44+
print(alist)

0 commit comments

Comments
 (0)