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)
| 13 | |
| 14 | |
| 15 | def 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 | |
| 42 | if __name__ == '__main__': |