-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.py
More file actions
30 lines (26 loc) · 805 Bytes
/
Copy pathquick_sort.py
File metadata and controls
30 lines (26 loc) · 805 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
# quick_sort.py
"""
Quick Sort implementation in Python.
Time Complexity: O(N log N) average, O(N^2) worst case
Space Complexity: O(log N) auxiliary space for call stack
"""
def partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1
def quick_sort_helper(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort_helper(arr, low, pi - 1)
quick_sort_helper(arr, pi + 1, high)
def quick_sort(arr):
quick_sort_helper(arr, 0, len(arr) - 1)
return arr
arr = [64, 25, 12, 22, 11]
print("Array before sort:", arr)
print("Array after sort:", quick_sort(arr))