Public void bubblesort (INT [] r)
{
Int I, j, temp;
// Exchange flag
Bool exchange;
// Sort the R. Length-1 bits at most
For (I = 0; I <R. length; I ++)
{
// The exchange flag should be false before the sorting starts.
Exchange = false;
For (j = R. Length-2; j> = I; j --)
{
// Exchange conditions
If (R [J + 1] <R [J])
{
Temp = R [J + 1];
R [J + 1] = R [J];
R [J] = temp;
// If an exchange occurs, the exchange flag is set to true.
Exchange = true;
}
}
// This sort order has not been exchanged and is terminated in advance
Algorithm
If (! Exchange)
{
Break;
}
}
}
II,
Using system;
Namespace bubblesorter
{
Public class bubblesorter
{
Public void sort (INT [] list)
{
Int I, j, temp;
Bool done = false;
J = 1;
While (j <list. Length )&&(! Done ))
{
Done = true;
For (I = 0; I <list. Length-J; I ++)
{
If (list [I]> list [I + 1])
{
Done = false;
Temp = list [I];
List [I] = list [I + 1];
List [I + 1] = temp;
}
}
J ++;
}
}
}
Public class mainclass
{
Public static void main ()
{
Int [] iarrary = new int };
Bubblesorter SH = new bubblesorter ();
Sh. Sort (iarrary );
For (INT m = 0; m <iarrary. length; m ++)
Console. Write ("{0}", iarrary [m]);
Console. writeline ();
}
}
}