Created
February 5, 2013 07:18
-
-
Save sampottinger/4712822 to your computer and use it in GitHub Desktop.
Crude implementation in QuickSort
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(target, i, j): | |
temp = target[i] | |
target[i] = target[j] | |
target[j] = temp | |
def quick_sort(target, low_index, high_index): | |
orig_low_index = low_index | |
orig_high_index = high_index | |
sub_array_size = high_index - low_index | |
pivot_index = low_index + sub_array_size / 2 | |
pivot_val = target[pivot_index] | |
if low_index >= high_index: | |
return | |
while low_index <= high_index: | |
# Look for an out of place element on the left | |
while target[low_index] < pivot_val: | |
low_index += 1 | |
# Look for an out of place element on the right | |
while target[high_index] > pivot_val: | |
high_index -= 1 | |
swap(target, low_index, high_index) | |
low_index += 1 | |
high_index -= 1 | |
quick_sort(target, orig_low_index, high_index) | |
quick_sort(target, low_index, orig_high_index) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment