![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
//数组组合和冒泡排序JS版
//打印数组
function PrintArr(arr)
{
for (var i = 0; i < arr.length; i++)
{
document.write(arr[i] + '<br />');
}
}
//连接2个数组
function ConnectArr(arr1, arr2)
{
var arr = new Array(arr1.length + arr2.length);
for (var i = 0; i < arr.length; i++)
{
if (i < arr1.length)
{
arr[i] = arr1[i];
}
else
{
arr[i] = arr2[i - arr1.length];
}
}
return arr;
}
//冒泡排序(从小到大排序)
function SortArr(arr)
{
var tmp;
for (var i = 0; i < arr.length ; i++)
{
for (var j = 0; j < i; j++)
{
if (arr[j] > arr[i])
{
tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
}
return arr;
}
//测试结果
var arr1 = [1, 3, 4, 2, 8];
var arr2 = [0, 9, 5, 7, 6];
document.write('arr1:<br />');
PrintArr(arr1);
document.write('arr2:<br />');
PrintArr(arr2);
var arr3 = ConnectArr(arr1, arr2);
document.write('arr3:<br />');
PrintArr(arr3);
var arr4 = SortArr(arr3);
document.write('arr4:<br />');
PrintArr(arr4);