Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created February 15, 2012 20:02
Show Gist options
  • Save mizchi/1838619 to your computer and use it in GitHub Desktop.
Save mizchi/1838619 to your computer and use it in GitHub Desktop.
def qsort (arr, left=0, right=arr.length-1, pl=left, pr=right)
x = arr[(left+right)/2]
begin
pl+=1 while arr[pl] < x
pr-=1 while arr[pr] > x
arr[pl], arr[pr] = arr[pr], arr[pl]
end while arr[pl] != arr[pr]
qsort(arr, left, pl-1) if pl-left > 1
qsort(arr, pr+1, right) if right-pr > 1
end
arr =(1..10).sort_by {rand}
qsort(arr)
p "result:#{arr}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment