Bubble algorithm is easy to understand

Source: Internet
Author: User

/* Do you really understand Bubble sorting? Or is it backed up? Is there really only one way to sort bubbles?
* Isn't it better to simply solve some complicated things?

* Although the methods are different, the thoughts are similar. I hope you can understand them carefully ......
*/

 

Namespace Sort
{
Public   Class Sort
{

//Bubble sort 1
//Isn't it easy to understand? It doesn't matter. Take a look at the next method, which is absolutely easy to understand.
Public VoidBubblesort (Int[])
{

// Define a temporary variable. Should I be familiar with the C language I have learned in order to exchange locations?
Int TMP;
For ( Int I =   0 ; I < A. Length -   1 ; I ++ )
{
Int Idx = I; // Is it also a way to use the index in the array?
For ( Int J = Idx +   1 ; J < A. length; j ++ )
If (A [idx] < A [J])
Idx = J; // In fact, it is to find the largest index.

//The following is the exchange process.
TMP=A [I];
A [I]=A [idx];
A [idx]=TMP;
}

}

//Bubble sort 2
//If you think this is hard for you, let's take a look at the following:
Public VoidBubblesort (Int[])
{

// Define a temporary variable. Should I be familiar with the C language I have learned in order to exchange locations?
Int TMP;
For ( Int I =   0 ; I < A. Length -   1 ; I ++ )
{
// Int idx = I; can this sentence be omitted?

// Is this easier to understand?
For ( Int J = I +   1 ; J < A. length; j ++ )
{

// If you are bigger than me, I will exchange it with you. If you are younger than me, I will continue to find something bigger than me. If you cannot find it, I will be the biggest, right?
If (A [I] < A [J])
{
TMP = A [J];
A [J] = A [I];
A [I] = TMP;
}
}

}

}

// recursive algorithm , simulating bubble 3
// slightly round, if you understand the first two types, it will be easier to see them again
Public void bubblesort ( int [] A)
{

//Call the following method
Bubble (,0, A. Length );
}

VoidBubble (Int[],IntStart,IntEnd)
{

// note: if this condition is not met, it will be infinitely recursive.
If (start = end)
{< br> return ;
}< br> int TMP;

// Similar to the two methods above
For ( Int I = Start; I < End -   1 ; I ++ )
{
If (A [start] < A [I +   1 ])
{
TMP = A [start];
A [start] = A [I +   1 ];
A [I +   1 ] = TMP;
}
}

//Legendary Recursion
Bubble (A, start+ 1, End );
}

}

}

 

 

From: http://www.cnblogs.com/shaodong/archive/2010/05/08/shaodong.html

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.