Created
June 29, 2011 14:33
-
-
Save hirak/1053947 to your computer and use it in GitHub Desktop.
merge sort patch
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
--- sort.js 2011-06-29 23:34:27.000000000 +0900 | |
+++ sort2.js 2011-06-29 23:34:37.000000000 +0900 | |
@@ -1,10 +1,10 @@ | |
var sort = function(list, comparer) { | |
- if (!comparer) comparer = function(x, y) { return y - x; }; | |
+ if (!comparer) comparer = function(x, y) { return x - y; }; | |
return (function self(list) { | |
if (list.length < 2) return list; | |
var left = self(list.splice(0, list.length >>> 1)), right = self(list.splice(0)); | |
while (left.length && right.length) | |
- list.push(comparer(left[0], right[0]) > 0 ? left.shift() : right.shift()); | |
+ list.push(comparer(left[0], right[0]) > 0 ? right.shift() : left.shift()); | |
return list.concat(left, right); | |
})(list); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment