Java Bubble Sorting

Source: Internet
Author: User

/**
* Created by rabbit on 2014-5-9.
*/
Class ArrayTest2 {
Public static void BubbleSort (int [] arr) // create a Bubble sorting method

{
For (int x = 0; x <arr. length-1; x ++)
{
For (int y = 0; y <arr. length-x-1; y ++) //-x is used to reduce the element-1 of each comparison in order to avoid cross-border. Exceeds the subscript range of the array.
{
If (arr [y] <arr [y + 1])
{
Int temp = arr [y];
Arr [y] = arr [y + 1];
Arr [y + 1] = temp;
}
}
}
}
Public static void PrintArr (int [] arr) // method for creating an output array element
{
For (int x = 0; x <arr. length; x ++)
{
If (x! = Arr. length-1)
{
System. out. print (arr [x] + ",");
} Else
System. out. print (arr [x]);
}
System. out. println ();
}
Public static void main (String [] args)
{
Int [] arr = {,-1}; // create an array and enter a value

PrintArr (arr); // call the Output Method
BubbleSort (arr); // call the Bubble sorting method
PrintArr (arr); // call the Output Method


}
}

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.