Created
January 5, 2015 07:54
-
-
Save vamshisuram/b6ecaa3d56e6d0d7e400 to your computer and use it in GitHub Desktop.
// source http://jsbin.com/pogixi
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> | |
<script src="http://jashkenas.github.io/underscore/underscore-min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// merge sort | |
var i, arr = [], output = [], n=4; | |
for (i=0; i<n; i++) { | |
arr.push(Math.floor(Math.random() * 100 + 1)); | |
} | |
console.log('before', arr); | |
var iArr = angular.copy(arr); | |
console.log('copy', iArr); | |
function sort(inputArr) { | |
var len = inputArr.length; | |
if (len === 1) { | |
return inputArr; | |
} | |
var a = sort(inputArr.splice(0, len/2)); | |
var b = sort(inputArr); | |
var c = merge(a,b); | |
return c; | |
} | |
function merge(a, b) { | |
console.log('a,b', a, b); | |
var l = a.length + b.length; | |
var aLen = a.length, bLen = b.length; | |
var i=0, j=0, k, mergedArr = []; | |
for (k = 0; k < l; k++) { | |
// if (i < aLen && j < bLen ) { | |
// if(a[i] <= b[j]) { | |
// mergedArr[k] = a[i]; | |
// i++; | |
// } else { | |
// mergedArr[k] = b[j]; | |
// j++; | |
// } | |
// } else if (i > aLen && j < bLen) { | |
// mergedArr[k] = b[j]; | |
// j++; | |
// } else if (i < aLen && j > bLen) { | |
// mergedArr[k] = a[i]; | |
// i++; | |
// } else { | |
// // | |
// } | |
if(a[i] <= b[j]) { | |
mergedArr[k] = a[i]; | |
i++; | |
} else { | |
mergedArr[k] = b[j]; | |
j++; | |
} | |
} | |
return mergedArr; | |
} | |
var outputArr = sort(iArr); | |
console.log('outputArr', outputArr); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"><\/script> | |
<script src="//jashkenas.github.io/underscore/underscore-min.js"><\/script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
</body> | |
</html></script> | |
<script id="jsbin-source-javascript" type="text/javascript">// merge sort | |
var i, arr = [], output = [], n=4; | |
for (i=0; i<n; i++) { | |
arr.push(Math.floor(Math.random() * 100 + 1)); | |
} | |
console.log('before', arr); | |
var iArr = angular.copy(arr); | |
console.log('copy', iArr); | |
function sort(inputArr) { | |
var len = inputArr.length; | |
if (len === 1) { | |
return inputArr; | |
} | |
var a = sort(inputArr.splice(0, len/2)); | |
var b = sort(inputArr); | |
var c = merge(a,b); | |
return c; | |
} | |
function merge(a, b) { | |
console.log('a,b', a, b); | |
var l = a.length + b.length; | |
var aLen = a.length, bLen = b.length; | |
var i=0, j=0, k, mergedArr = []; | |
for (k = 0; k < l; k++) { | |
// if (i < aLen && j < bLen ) { | |
// if(a[i] <= b[j]) { | |
// mergedArr[k] = a[i]; | |
// i++; | |
// } else { | |
// mergedArr[k] = b[j]; | |
// j++; | |
// } | |
// } else if (i > aLen && j < bLen) { | |
// mergedArr[k] = b[j]; | |
// j++; | |
// } else if (i < aLen && j > bLen) { | |
// mergedArr[k] = a[i]; | |
// i++; | |
// } else { | |
// // | |
// } | |
if(a[i] <= b[j]) { | |
mergedArr[k] = a[i]; | |
i++; | |
} else { | |
mergedArr[k] = b[j]; | |
j++; | |
} | |
} | |
return mergedArr; | |
} | |
var outputArr = sort(iArr); | |
console.log('outputArr', outputArr); | |
</script></body> | |
</html> |
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
// merge sort | |
var i, arr = [], output = [], n=4; | |
for (i=0; i<n; i++) { | |
arr.push(Math.floor(Math.random() * 100 + 1)); | |
} | |
console.log('before', arr); | |
var iArr = angular.copy(arr); | |
console.log('copy', iArr); | |
function sort(inputArr) { | |
var len = inputArr.length; | |
if (len === 1) { | |
return inputArr; | |
} | |
var a = sort(inputArr.splice(0, len/2)); | |
var b = sort(inputArr); | |
var c = merge(a,b); | |
return c; | |
} | |
function merge(a, b) { | |
console.log('a,b', a, b); | |
var l = a.length + b.length; | |
var aLen = a.length, bLen = b.length; | |
var i=0, j=0, k, mergedArr = []; | |
for (k = 0; k < l; k++) { | |
// if (i < aLen && j < bLen ) { | |
// if(a[i] <= b[j]) { | |
// mergedArr[k] = a[i]; | |
// i++; | |
// } else { | |
// mergedArr[k] = b[j]; | |
// j++; | |
// } | |
// } else if (i > aLen && j < bLen) { | |
// mergedArr[k] = b[j]; | |
// j++; | |
// } else if (i < aLen && j > bLen) { | |
// mergedArr[k] = a[i]; | |
// i++; | |
// } else { | |
// // | |
// } | |
if(a[i] <= b[j]) { | |
mergedArr[k] = a[i]; | |
i++; | |
} else { | |
mergedArr[k] = b[j]; | |
j++; | |
} | |
} | |
return mergedArr; | |
} | |
var outputArr = sort(iArr); | |
console.log('outputArr', outputArr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment