JAVA basics: JAVA Implementation of the Bubble Sorting Algorithm

Source: Internet
Author: User

Package Utils. Sort;

/**

* @ Author Linyco

* Sort the Array Using the Bubble sorting method. The elements in the array must implement the Comparable interface.

*/Public class BubbleSort implements SortStrategy

{

/**

* Sort the elements in the array obj by the Bubble Sorting Algorithm.

*/

Public void sort (Comparable [] obj)

{If (obj = null)

{Throw new NullPointerException ("The argument can not be null! ");

} Comparable tmp;

For (int I = 0; I <obj. length; I ++)

{// Remember, each time the ratio starts from the first one. The final comparison is unnecessary.

For (int j = 0; j <obj. length-I-1; j ++)

{// Compare the adjacent elements. If the backend is small, it is switched.

If (obj [j]. compareTo (obj [j + 1])> 0)

{Tmp = obj [j];

Obj [j] = obj [j + 1];

Obj [j + 1] = tmp;

}}

}}}


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.