Function to find the partition position :param array: the unsorted array :type array: List :param low: smaller pivot :type low: int :param high: greater pivot :type high: int
(array, low, high)
| 1 | def partition(array, low, high): |
| 2 | """ |
| 3 | Function to find the partition position |
| 4 | |
| 5 | :param array: the unsorted array |
| 6 | :type array: List |
| 7 | :param low: smaller pivot |
| 8 | :type low: int |
| 9 | :param high: greater pivot |
| 10 | :type high: int |
| 11 | |
| 12 | """ |
| 13 | # choose the rightmost element as pivot |
| 14 | pivot = array[high] |
| 15 | |
| 16 | # pointer for greater element |
| 17 | i = low - 1 |
| 18 | |
| 19 | # traverse through all elements |
| 20 | # compare each element with pivot |
| 21 | for j in range(low, high): |
| 22 | if array[j] <= pivot: |
| 23 | |
| 24 | # If element smaller than pivot is found |
| 25 | # swap it with the greater element pointed by i |
| 26 | i = i + 1 |
| 27 | |
| 28 | # Swapping element at i with element at j |
| 29 | (array[i], array[j]) = (array[j], array[i]) |
| 30 | |
| 31 | # Swap the pivot element with the greater element specified by i |
| 32 | (array[i + 1], array[high]) = (array[high], array[i + 1]) |
| 33 | |
| 34 | # Return the position from where partition is done |
| 35 | return i + 1 |
| 36 | |
| 37 | def quickSort(array, low, high): |
| 38 | """ |