Copy codeThe Code is as follows:
/* 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; // The index is used 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 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; 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;
}
}
}
}
// Recursive algorithm, simulating 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 );
}
}
}