MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / quick_sort

Function quick_sort

sorts/quick_sort.py:15–39  ·  view source on GitHub ↗

Pure implementation of quick sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending Examples: >>> quick_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> quick_sor

(ARRAY)

Source from the content-addressed store, hash-verified

13
14
15def quick_sort(ARRAY):
16 """Pure implementation of quick sort algorithm in Python
17
18 :param collection: some mutable ordered collection with heterogeneous
19 comparable items inside
20 :return: the same collection ordered by ascending
21
22 Examples:
23 >>> quick_sort([0, 5, 3, 2, 2])
24 [0, 2, 2, 3, 5]
25
26 >>> quick_sort([])
27 []
28
29 >>> quick_sort([-2, -5, -45])
30 [-45, -5, -2]
31 """
32 ARRAY_LENGTH = len(ARRAY)
33 if( ARRAY_LENGTH <= 1):
34 return ARRAY
35 else:
36 PIVOT = ARRAY[0]
37 GREATER = [ element for element in ARRAY[1:] if element > PIVOT ]
38 LESSER = [ element for element in ARRAY[1:] if element <= PIVOT ]
39 return quick_sort(LESSER) + [PIVOT] + quick_sort(GREATER)
40
41
42if __name__ == '__main__':

Callers 1

quick_sort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected