C/C ++ algorithm learning notes-Bubble Method

Source: Internet
Author: User

 

Bubble Method:

This is the most primitive and well-known slowest algorithm. Its name comes from because its work seems to be bubbling:

 

[Cpp]
# Include <iostream. h>
 
Void BubbleSort (int * pData, int Count)
 
{
 
Int iTemp;
 
For (int I = 1; I <Count; I ++)
 
{
 
For (int j = Count-1; j> = I; j --)
 
{
 
If (pData [j] <pData [J-1])
 
{
 
ITemp = pData [J-1];
 
PData [J-1] = pData [j];
 
PData [j] = iTemp;
 
}
 
}
 
}
 
}
 

 
Void main ()
 
{
 
Int data [] = {10, 9, 8, 7, 6, 5, 4 };
 
BubbleSort (data, 7 );
 
For (int I = 0; I <7; I ++)
 
Cout <data [I] <"";
 
Cout <"/n ";
 
}

# Include <iostream. h>

Void BubbleSort (int * pData, int Count)

{

Int iTemp;

For (int I = 1; I <Count; I ++)

{

For (int j = Count-1; j> = I; j --)

{

If (pData [j] <pData [J-1])

{

ITemp = pData [J-1];

PData [J-1] = pData [j];

PData [j] = iTemp;

}

}

}

}

 

Void main ()

{

Int data [] = {10, 9, 8, 7, 6, 5, 4 };

BubbleSort (data, 7 );

For (int I = 0; I <7; I ++)

Cout <data [I] <"";

Cout <"/n ";

}

 

Figure:

-----------------------------------------------------------------------------

Before comparison | first time | second time | third time | fourth time | fifth time | sixth time

10 10 10 10 10 10 4

9 9 9 9 9 4 10

8 8 8 8 4 9 9

7 7 7 4 8 8 8

6 6 4 7 7 7 7

5 4 6 6 6 6

4 5 5 5 5 5

-----------------------------------------------------------------------------

 

From the description of the bubble image, the 4 element gradually emerged as a bubble.

We sorted seven elements in reverse order, and the worst case was all. It would take six times for the element 4 to come up. Therefore, the worst case for n elements needs to be moved: 1 + 2 + 3 +... + (n-1) = 1/2 * n (n-1) times.

 

Reverse Order (worst case)

First round: 10, 9, 8, 7-> 10, 9, 7-> 10, 7, 9-> 7, 10, 9, 8 (three exchanges)

Round 2: 7, 10, 9-> 7, 10, 8-> 7, 8, 9 (2 exchanges)

First round: 7, 8, 10, 9-> 7, 8, 9, 10 (switching once)

Cycles: 6

Number of exchanges: 6

 

Others:

First round:,->, (exchange twice)

Round 2: 7, 8, 10, 9-> 7, 8, 10, 9-> 7, 8, 10, 9 (0 exchanges)

First round: 7, 8, 10, 9-> 7, 8, 9, 10 (switching once)

Cycles: 6

Number of exchanges: 3

We have given the program section above, and now we analyze it: here, the main part that affects our algorithm performance is loop and exchange. Obviously, the more times, the worse the performance. From the above program, we can see that the number of cycles is fixed, which is 1 + 2 +... + n-1. The formula is 1/2 * (n-1) * n.

Note that the O method is defined as follows:

If there is a constant K and the starting point n0, so when n> = n0, f (n) <= K * g (n), f (n) = O (g (n )). (Don't say you didn't learn mathematics well. It is very important for programming mathematics !!!)

Now let's look at 1/2 * (n-1) * n. When K = 1/2, n0 = 1, g (n) = n * n, 1/2 * (n-1) * n <= 1/2 * n = K * g (n ). So f (n) = O (g (n) = O (n * n ). So the complexity of our program loop is O (n * n ).

Let's look at the exchange. We can see from the table following the program that the two cases share the same loop and the exchange is different. In fact, the exchange itself has a great relationship with the degree of order of the data source. when the data is in reverse order, the number of exchanges is the same as the number of cycles (each cycle will be exchanged ), the complexity is O (n * n ). When the data is in positive order, there will be no exchange. The complexity is O (0 ). It is in the intermediate state in disordered order. For this reason, we usually compare algorithms by the number of cycles.

 

 

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.