Skip to content

Commit 4df07ba

Browse files
committed
Added quick sort
1 parent a7918ba commit 4df07ba

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pygorithms/sorting/quick_sort.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Author: OMKAR PATHAK
2+
# Created On: 31st July 2017
3+
4+
# Best = Average = O(nlog(n)), Worst = O(n ^ 2)
5+
6+
# quick_sort algorithm
7+
def sort(arr):
8+
if len(arr) <= 1:
9+
return arr
10+
pivot = arr[len(arr) // 2]
11+
left = [x for x in arr if x < pivot]
12+
middle = [x for x in arr if x == pivot]
13+
right = [x for x in arr if x > pivot]
14+
return sort(left) + middle + sort(right)
15+
16+
# time complexities
17+
def bestcase_complexity():
18+
return 'O(nlogn)'
19+
20+
def averagecase_complexity():
21+
return 'O(nlogn)'
22+
23+
def worstcase_complexity():
24+
return 'O(n ^ 2)'
25+
26+
# easily retrieve the source code of the sort function
27+
def get_code():
28+
import inspect
29+
return inspect.getsource(sort)

0 commit comments

Comments
 (0)