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 ();
}
}
}