Sort algorithm (4)--bubble sort

Source: Internet
Author: User

Bubble sort (Bubble sort)

1. The traditional bubble sort algorithm

(1) Algorithm idea

When an ascending bubble sort is performed, the next two numbers are compared from the back to the next, which is exchanged if the previous number is greater than the following number. At the end of the first iteration, the smallest element in the sorted sequence A[1..N] is swapped to the a[1] position. After the n-1 cycle, you can complete the sort of a.

For A= (49, 38, 65, 97, 76, 13, 27, 49) to bubble sort, the exchange process for the 1th cycle element is as follows (two-way arrow: Swap):


The entire process of bubbling sort is shown in the following illustration:


(2) Pseudo-code

Bubble-sort (A)

1 for I←1 to Length[a]-1

2 Do for J←downto i + 1

3 do if a[j]<a[j-1]

4 then Exchange A[j]↔a[j-1]


(3) Code implementation

void bubblesort_1 (int a[],int n)
{
    int i,j;
    for (i=0;i<n;i++)
    {for
        (j=n-1;j>=i+1;j--)
        {
            if (a[j]<a[j-1])
            {
                int temp=a[j];
                A[J]=A[J-1];
                A[j-1]=temp}}}}

(4) algorithm analysis

A. The time complexity of bubbling is: Theta (n^2).

B. Bubble sort is a sort of in situ.

C. Bubble sort is a stable sort.


2. The improvement of bubble algorithm

(1) Improvement (i): Set Exchange identification

As can be seen from the example shown above, when the 4th loop is finished, array A is already ordered, and the subsequent loops are no longer swapped, but the traditional bubble sort still follows the loop. Here you can set up an Exchange identifier (CHANGE_FLAG), which stops the loop after a loop without any exchange operation, which reduces the number of loops. The improved implementation is as follows:

void bubblesort_2 (int a[],int n)
{
    int i,j;
    BOOL Change_flag=true;
    for (i=0;i<n&&change_flag;i++)       ///Only if there is an exchange occurring in the last loop, the loop
    {change_flag=false) continues
        ;              At the start of each loop, set Change_flag to False for
        (j=n-1;j>=i+1;j--)
        {
            if (a[j]<a[j-1])
            {
                int temp=a [j];
                A[J]=A[J-1];
                A[j-1]=temp;
                Change_flag=true;        There is a swap, Change_flag is set to True}}}
(2) Improvement (ii): Bidirectional bubble sort

Two-way bubbling sort, which makes forward and reverse two bubbles in each sequence, can get two final values (the largest and smallest) at a time, thus reducing the number of sorted journeys by almost half. Specifically implemented as follows:

void bubblesort_3 (int a[],int n)
{
    int low=0;
    int high=n-1;
    while (Low




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.