Java Data Structure -- & gt; Bubble Sorting

Source: Internet
Author: User

The basic idea of Bubble Sort sorting is to think of the array to be sorted as a descending order, the record with smaller keywords as lighter, and the record with larger keywords as heavier, the value of a smaller keyword is regarded as a bubble in the water floating up, a larger keyword such as a rock sinking, when the bubbles are floating to the corresponding position, the sorting ends.

Algorithm performance analysis

(1) time complexity. The total number of times is 3/2 (n-1) * n.

(2) spatial complexity. The space complexity of only one auxiliary unit is O (1)

(3) algorithm stability. Bubble Sorting is a stable sorting algorithm.

/*
* Kiss_My_Love
* 2012/8/20
* Bubble Sorting
**/
Public static Object [] bubbleSort (Object [] sort ){
For (int I = 1; I <sort. length; I ++ ){
For (int j = 0; j <sort. length-I; j ++ ){
If (Integer) sort [j]> (Integer) sort [j + 1]) {
Object temp = sort [j];
Sort [j] = sort [j + 1];
Sort [j + 1] = temp;
}
}
}
Return sort;
}

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.