forked from joshmadakor1/Algorithms-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.py
More file actions
42 lines (34 loc) · 1.2 KB
/
QuickSort.py
File metadata and controls
42 lines (34 loc) · 1.2 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
'''
QuickSort:
Given an input array, perform a quicksort on it and return the sorted array
Best Time: O(NlogN), where N are the number of nodes in the list
Worst Time: O(N^2)
Space: O(NlogN) due to the recursive call stack
Last Practice: 2022-03-26 07:54:58
'''
def quickSort(array):
quickSortHelper(array, 0, len(array) - 1)
return array
def quickSortHelper(array, start, end):
if start > end: return
pivot = start
left = start + 1
right = end
while left <= right:
if array[left] > array[pivot] and array[right] < array[pivot]:
swap(array, left, right)
if array[left] <= array[pivot]:
left += 1
if array[right] >= array[pivot]:
right -= 1
swap(array, right, pivot)
leftSubArrayIsSmaller = ((right - 1) - left) < (end - (right + 1))
if leftSubArrayIsSmaller:
quickSortHelper(array, start, right - 1)
quickSortHelper(array, right + 1, end)
else:
quickSortHelper(array, right + 1, end)
quickSortHelper(array, start, right - 1)
def swap(array, left, right):
array[left], array[right] = array[right], array[left]
print(quickSort([9,8,7,6,5,4,3,2,1,0,-1,-2]))