We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a7918ba commit 4df07baCopy full SHA for 4df07ba
pygorithms/sorting/quick_sort.py
@@ -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
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