Skip to content

Instantly share code, notes, and snippets.

@firstspring1845
Created July 13, 2015 13:26
Show Gist options
  • Save firstspring1845/74f329b20475fb95970a to your computer and use it in GitHub Desktop.
Save firstspring1845/74f329b20475fb95970a to your computer and use it in GitHub Desktop.
クイックソートです
function quicksort(arr){
if(arr.length < 2) return arr
var pivot = arr[Math.random() * arr.length >> 0]
var low = [], high = []
for(var i = 0; i < arr.length; i += 1){
if(arr[i] <= pivot) low.push(arr[i])
else high.push(arr[i])
}
if([low.length, high.length].indexOf(0) !== -1){
var ret = low.concat(high)
for(var i = 1; i < ret.length; i += 1) if(ret[i] !== ret[0]) return quicksort(ret)
return ret
}
return quicksort(low).concat(quicksort(high))
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment