Forum Moderators: open
Here is my code
function Merge(arr1, arr2){
totallength = arr1.length + arr2.length;
arr3 = new arr[totallength];
firstcounter = 0;
secondcounter = 0;
for (i = 0; i < arr3.length; i++) {
// Deciding whether to take next smallest number from
// first array or second.
if ((secondcounter == arr2.length) ¦¦
((firstcounter < arr1.length) &&
(arr1[firstcounter] < arr2[secondcounter]))) {
arr3[i] = arr1[firstcounter];
firstcounter++;
}
else {
arr3[i] = arr2[secondcounter];
secondcounter++;
}
}
return arr3;
}
Html is simply
<html>
<head>
<title>Array Sort</title>
<script src="sort.js" language="JavaScript" type="text/javascript"></script>
</head>
<body>
<script>
var arr1 = [3, 7, 12, 15, 22, 45, 56];
var arr2 = [1, 2, 5, 17, 20];
merge(arr1, arr2);
</script>
</body>
</html>