C # Bubble Sorting Algorithm

Source: Internet
Author: User
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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.