C # Do you still want to perform Bubble sorting?

Source: Internet
Author: User

 

 

We all know that two for loops are done. How do you remember these two loops?

Outer layer: loop array length; I <array length-1 // starts from 0;

Inner Layer: Number of cyclic sorting; j <array length-1-I;

 

 

 

Memo code:

 

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace helloaccp
{
/// <Summary>
/// This program demonstrates the use of Double Loops to implement the Bubble Sorting Algorithm of Arrays
/// </Summary>
Class Program
{
Static void main (string [] ARGs)
{
Int [] scores = new int [5];
Int I, j; // cyclic variable
Int temp; // Temporary Variable

// Read the score
Console. writeline ("Enter the score of five students :");
For (I = 0; I <5; I ++)
{
Console. writeline ("Enter the score of {0} students:", I + 1 );
Scores [I] = int. parse (console. Readline (); // type conversion
}

// Start sorting
For (I = 0; I <scores. Length-1; I ++)
{
For (j = 0; j <scores. Length-1-I; j ++)
{
If (scores [J]> scores [J + 1])
{
// Exchange Element
Temp = scores [J];
Scores [J] = scores [J + 1];
Scores [J + 1] = temp;
}
}
}

// Output after sorting
Console. writeline ("sorted score :");
For (I = 0; I <5; I ++)
{
Console. Write ("{0} \ t", scores [I]);
}

Console. Readline ();
}
}

}


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.