Created
April 20, 2020 03:21
-
-
Save amandaroos/ffa9a85125e8468a5279df87e1de0a3a to your computer and use it in GitHub Desktop.
Quicksort 2
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
"""Implement quick sort in Python. | |
Input a list. | |
Output a sorted list.""" | |
def quicksort(array): | |
low = 0 | |
high = len(array)-1 | |
partition(array, low, high) | |
return array | |
def partition(array, low, high): | |
if low >= high: | |
return None | |
pivot = array[high] | |
testIndex = low - 1 | |
for i in range(low, high): | |
if array[i] <= pivot: | |
testIndex = testIndex + 1 | |
array[testIndex], array [i] = array[i], array[testIndex] | |
array[testIndex + 1], array[high] = array[high], array[testIndex + 1] | |
testIndex = testIndex + 1 | |
partition(array, low, testIndex - 1) | |
partition(array, testIndex + 1, high) | |
test = [21, 4, 1, 3, 9, 20, 25, 6, 21, 14] | |
print quicksort(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment