Skip to content

Instantly share code, notes, and snippets.

@sampottinger
Created February 5, 2013 07:18
Show Gist options
  • Save sampottinger/4712822 to your computer and use it in GitHub Desktop.
Save sampottinger/4712822 to your computer and use it in GitHub Desktop.
Crude implementation in QuickSort
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