forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.py
More file actions
29 lines (23 loc) · 685 Bytes
/
Copy pathquick_sort.py
File metadata and controls
29 lines (23 loc) · 685 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
# Author: OMKAR PATHAK
# Created On: 31st July 2017
# Best = Average = O(nlog(n)), Worst = O(n ^ 2)
# quick_sort algorithm
def sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return sort(left) + middle + sort(right)
# time complexities
def bestcase_complexity():
return 'O(nlogn)'
def averagecase_complexity():
return 'O(nlogn)'
def worstcase_complexity():
return 'O(n ^ 2)'
# easily retrieve the source code of the sort function
def get_code():
import inspect
return inspect.getsource(sort)