/* 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 ......
**/
Using system;
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 void bubblesort (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 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;// Actually, it is to find the index with the largest number.
// 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 void bubblesort (INT [])
{
// define a temporary variable. Should I be familiar with the C language I have learned to exchange locations?
int TMP;
for (INT I = 0; I <. length-1; I ++)
{< br> // 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 with you. If you are younger than me, you 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;
}
}
}
}
// RecursionAlgorithm, Simulate bubble three
// A little bit around. It would be easier if you understood the first two methods.
Public void bubblesort (INT [])
{
// Call the following method
Bubble (A, 0, A. Length );
}
Void bubble (INT [] A, int start, int end)
{
// Note: Infinite recursion is possible without this judgment condition.
If (Start> = end)
{
Return;
}
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;
}
}
// Recursion
Bubble (A, start + 1, end );
}
}
}