Created
April 4, 2013 05:29
-
-
Save nikuuchi/5308053 to your computer and use it in GitHub Desktop.
quick sortを作ってみた in JavaScript
This file contains hidden or 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
| 'use strict'; | |
| function quickSort(arr) { | |
| if (arr.length === 0) { | |
| return arr; | |
| } else { | |
| var p = arr[0], | |
| xs = arr.slice(1); | |
| var gts = [], | |
| lts = [], | |
| i = 0; | |
| for(i = 0; i < xs.length; i++) { | |
| if ( xs[i] < p ) { | |
| lts.push(xs[i]); | |
| } else { | |
| gts.push(xs[i]); | |
| } | |
| } | |
| return quickSort(lts).concat([p], quickSort(gts)); | |
| } | |
| } | |
| console.log([4, 6, 3, 5, 1, 2, 7]); | |
| console.log(quickSort([4, 6, 3, 5, 1, 2, 7])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment