Bubble sort of sorting algorithm

Source: Internet
Author: User

Basic ideas

First the first element and the second element are relatively large, assuming the first one. The two are exchanged, otherwise they are not exchanged; then the second element is compared to the third one. Assuming the second large, then the two exchange, otherwise do not exchange ... has been going on in this way. Finally the biggest element was exchanged to the last. A trip to bubble sort is complete.

Code
publicvoidBubbleSort(intint n) {    int temp;    for (int1; i < n; i++) {        for (int1; j >= i; j--) {            if1] > a[j]) {                1];                1] = a[j];                a[j] = temp;            }        }    }}
Performance analysis

The number of elements in the main bubble sort is independent of the initial arrangement of the elements to be sorted in the input sequence. However, the number of times the data is exchanged is related to the initial arrangement of the elements to be ordered, preferably without a single exchange. In the worst case, each time the comparison needs to be exchanged.

Bubble sort of sorting algorithm

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.