Created
July 13, 2015 13:26
-
-
Save firstspring1845/74f329b20475fb95970a to your computer and use it in GitHub Desktop.
クイックソートです
This file contains 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
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