Bubble sort (Java)

Source: Internet
Author: User


 BubbleSort (bubblesort)The basic concept is: compare two adjacent numbers in sequence, put the decimal points in front, and put the large numbers in the back. That is, in the first step: first compare the number of 1st and the number of 2nd, and put the decimal number before and after the large number. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. So far, the first stop is over, and the maximum number is placed at the end. In the second round: The comparison starts from the first logarithm (because the number of 2nd is exchanged with the number of 3rd, the number of 1st is no less than 2nd). Before and after placing the decimal number, always compare to the second to the last (the largest position in the last), and the second stop, get a new maximum number at the penultimate position (in fact, it is the second largest number in the entire series ). In this case, repeat the above process until the sorting is completed.

Bubble Sorting algorithm Implementation (Java):

Public static void bubblesort (INT [] list) {
int temp;
for (int K = 1; k for (INT I = 0; I If (list [I]> list [I + 1]) {
// swap list [I] With list [I + 1]
temp = list [I];
list [I] = list [I + 1];
list [I + 1] = temp;
}< br> }< br> }< span style =" white-space: pre ">
}



Performance Analysis of bubble sort Algorithms

If the initial status of the record sequence is "positive", the Bubble sorting process only needs to sort the sequence. In the sorting process, only n-1 exchanges are required and records are not moved. Otherwise, if the initial status of the record sequence is "backward", You need to perform n (n-1)/2 comparisons and record movement. Therefore, the total time complexity of Bubble Sorting is O (n * n ).

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.