Created
September 19, 2016 03:43
-
-
Save nullset2/d18b8909028eff69715da4866a62dbb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def swap(array, first, second): | |
| array[first], array[second] = array[second], array[first] | |
| def quick_sort(array): | |
| return _quick_sort(array, 0, len(array) - 1) | |
| def _quick_sort(array, start, end): | |
| if start < end: | |
| pivot_index = partition(array, start, end) | |
| _quick_sort(array, start, pivot_index - 1) | |
| _quick_sort(array, pivot_index + 1, end) | |
| return | |
| else: | |
| return 0 | |
| def partition(array, start, end): | |
| pivot = array[start] | |
| i = start + 1 | |
| for j in range(start + 1, end + 1): | |
| if array[j] < pivot: | |
| swap(array, j, i) | |
| i = i + 1 | |
| swap(array, start, i - 1) | |
| return i - 1 | |
| input_array = [20, 18, 99, 3, 65, 23, 55, 4] | |
| quick_sort(input_array) | |
| print input_array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment