-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.py
39 lines (31 loc) · 1.07 KB
/
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
39
import sys
sys.setrecursionlimit(30)
unsorted = [5, 2, 7, 6, 1, 9, 4, 8, 12, 10, 3, 11]
def partition(leftPointer, rightPointer, currentPointer):
initPointer = leftPointer if currentPointer == 'R' else rightPointer
pivot = unsorted[initPointer]
while leftPointer != rightPointer:
if currentPointer == 'R':
if unsorted[rightPointer] < pivot:
unsorted[leftPointer] = unsorted[rightPointer]
leftPointer = leftPointer + 1
currentPointer = 'L'
else:
rightPointer = rightPointer - 1
elif currentPointer == 'L':
if unsorted[leftPointer] > pivot:
unsorted[rightPointer] = unsorted[leftPointer]
rightPointer = rightPointer - 1
currentPointer = 'R'
else:
leftPointer = leftPointer + 1
unsorted[leftPointer] = pivot
return pivot
def quickSort(left, right, pointer):
if left < right:
pivotPosition = partition(left, right, pointer)
quickSort(left, pivotPosition - 1, 'R')
quickSort(pivotPosition + 1, right, 'L')
print(unsorted)
quickSort(0, len(unsorted) - 1, 'R')
print(unsorted)