Skip to content

Instantly share code, notes, and snippets.

@acapilleri
Last active August 29, 2015 14:26
Show Gist options
  • Save acapilleri/357aecc7a6cafa3749a7 to your computer and use it in GitHub Desktop.
Save acapilleri/357aecc7a6cafa3749a7 to your computer and use it in GitHub Desktop.
Ruby Quick Sort
def quicksort(ary)
size = ary.count
return ary if size <= 1
i, j = 0, size - 1
pivot = ary[(size / 2)]
while i < j
i += 1 while ary[i] < pivot
j -= 1 while ary[j] > pivot
ary[i], ary[j] = ary[j], ary[i]
end
quicksort(ary[0..j-1]) + quicksort(ary[j..size-1])
end
a = (1..400).to_a.shuffle
p a
p quicksort a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment