/* 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