The earliest time to write Bubble Sorting was when I went to school, I usedC LanguageThis is useless.AlgorithmThen, I can see that many people on the Internet are obsessed with this algorithm and even use it to evaluateProgramI tried to write it again with C.
The last three lines of comment in the program are used to avoid repeated sorting of sorted arrays.
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Namespace Bubblesortcsharp
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Int [] Arr = { 18 , 3 , 8 , 2 ,7 , 9 , 9 , 9 , 5 , 11 , 1 , 10 , 30 , 20 };
Bubblesort = New Bubblesort ();
Bubblesort. bubblesortcsharp (ARR );
// Bubblesort. bubblesortcsharp (ARR ); // Compare the execution of sorted arrays and unordered Arrays
For ( Int I = 0 ; I <arr. length; I ++)
{
System. Console. writeline (ARR [I]. tostring ());
}
System. Console. Read ();
}
Class Bubblesort
{
Public Void Bubblesortcsharp ( Int [] ARR)
{
For ( Int I = 0 ; I <arr. length; I ++)
{
// Bool flag = true;
For ( Int J = 0 ; J <arr. Length-I- 1 ; J ++)
{
If (ARR [J]> arr [J + 1 ])
{
Swap ( Ref Arr [J], Ref Arr [J + 1 ]);
// Flag = false;
}
}
// If (FLAG) break;
}
}
protected void swap ( ref int X, ref int Y)
{< br> X = x + y;
Y = x-y;
X = x-y;
}
}
}}