Bubble Sorting Algorithm (C # implementation)

Source: Internet
Author: User

Simple Bubble SortingAlgorithm,CodeAs follows:

 //  Bubble Sorting (traversal starts from the starting position of the array, with a large base: sinking a large number)  
Private Static Void Bubblesortfunction ( Int [] Array)
{
Try
{
Int Length = Array. length;
Int Temp;
Bool Hasexchangeaction; // Record whether two adjacent numbers in this large loop have been exchanged (if not, the array is already ordered)

For ( Int I = 0 ; I < Length - 1 ; I ++ ) // Array has N number, then the use of N-1 large loop can be completed
{
Hasexchangeaction
= False ; // Each large loop assumes that the array is ordered.

For ( Int J = 0 ; J < Length - I - 1 ; J ++ ) // Traverse from the array subscript 0, (length-I-1 is to partition the large number already exclusive)
{
If (Array [J] > Array [J + 1 ]) // Compare two adjacent numbers. If the preceding number is greater than the following number, swap the adjacent two numbers.
{
Temp
= Array [J];
Array [J]
= Array [J + 1 ];
Array [J
+ 1 ] = Temp;
Hasexchangeaction
= True ; // Swapping
}
}

If ( ! Hasexchangeaction) // If there is no swapping, the array is already ordered and the loop jumps out.
{
Break ;
}
}
}
Catch (Exception ex)
{}
}

.....

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.