-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.py
40 lines (31 loc) · 934 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
35
36
37
38
39
40
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
quicksort.py : quick sort, also known as a partition sort,
which uses a "Divide and Conquer" approach.
Time complexity:
Worst: O(n**2)
Best: O(nlogn)
Space Complexity: O(n)
"""
__author__ = "John Jennings"
def quickSort(arr, low_index, high_index):
# base case
if len(arr) == 1:
return arr
# recursive case
if low_index < high_index:
part_index = partition(arr, low_index, high_index)
quickSort(arr, low_index, part_index-1)
quickSort(arr, part_index+1, high_index)
return arr
def partition(arr, low_index, high_index):
i = low_index
pivot = arr[high_index]
for j in range(low_index, high_index):
if arr[j] <= pivot:
arr[i], arr[j] = arr[j], arr[i]
i += 1
arr[i], arr[high_index] = arr[high_index], arr[i]
return i
print(quickSort([2,4,56,32,64,746,0], 0, 6))