Array combination and Bubble Sorting JS Edition

Source: Internet
Author: User
Code
// Array combination and Bubble Sorting JS Edition
// Print Array
Function Printarr (ARR)
{
  For ( VaR I =   0 ; I < Arr. length; I ++ )
{
Document. Write (ARR [I] +   ' <Br/> ' );
}
}
// Connect two Arrays
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;
}
// Bubble sort (Ascending Order)
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;
}
// Test Results
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.