The so-called array, is the same data type of elements in a certain order of the set, that is, the finite data type of the same variable with a name, and then use the number to distinguish their variable collection, the name is called the array name, the number is called subscript.
Each variable that makes up an array is called the component or element of the array.
The index of the elements in the array starts with 0, which is a "0", a "1" ....
Quote: Bubble sort
The bubbling sorting algorithm works as follows:
Compares the adjacent elements . If the first one is bigger than the second one, swap them both.
Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
repeat the above steps for all elements , except for the last one.
Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
Examples for small to large sort,
Original Array to sort | 6 | 2 | 4 | 1 | 5 | 9 |
First trip sort (outer loop)
First 22 comparison 6 > 2 swap (inner loop)
Pre-swap Status | 6 | 2 | 4 | 1 | 5 | 9 |
Post-swap Status | 2 | 6 | 4 | 1 | 5 | 9 |
Second 22 comparison, 6 > 4 swap
Pre-swap Status | 2 | 6 | 4 | 1 | 5 | 9 |
Post-swap Status | 2 | 4 | 6 | 1 | 5 | 9 |
Third 22 comparison, 6 > 1 swap
Pre-swap Status | 2 | 4 | 6 | 1 | 5 | 9 |
Post-swap Status | 2 | 4 | 1 | 6 | 5 | 9 |
Fourth time 22 comparison, 6 > 5 swap
Pre-swap Status | 2 | 4 | 1 | 6 | 5 | 9 |
Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |
Fifth time 22 comparison, 6 < 9 no swap
Pre-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |
Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |
Second trip sort (outer loop)
First 22 comparison 2 < 4 no swap
Pre-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |
Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |
Second 22 comparison, 4 > 1 swap
Pre-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |
Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |
Third 22 Comparisons, 4 < 5 non-exchangeable
Pre-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |
Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |
Fourth time 22 comparison, 5 < 6 no swap
Pre-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |
Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |
Third trip sort (outer loop)
First time 22 comparison 2 > 1 swap
Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |
Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |
Second 22 comparison, 2 < 4 non-exchangeable
Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |
Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |
Third 22 Comparisons, 4 < 5 non-exchangeable
Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |
Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |
Four-trip sort (outer loop) No swap
Five-trip sort (outer loop) No swap
Sort finished, output final result 1 2 4 5 6 9
For example: Enter a score of 10 people, remove two highest points, two minimum points, and average.
int temp = 0;
Console.WriteLine ("Please enter the number of people:");
int n = Convert.ToInt32 (Console.ReadLine ());
Int[] A = new int[n];
if (n >= 5)
{
for (int i = 0; i < a.length; i++)//length subscript is starting from 1
{
Console.WriteLine ("Please enter" + (i + 1) + "Personal score:");
A[i] = Convert.ToInt32 (Console.ReadLine ());
}
for (int i = 0; i < a.length; i++)//bubbling, from small to large
{
for (int j = 0; J < a.length-i-1; j + +)
{
if (A[j] > a[j + 1])
{
temp = A[j];
A[J] = a[j + 1];
A[j + 1] = temp;
}
}
}
int sum = 0;
Double aver;
for (int i = 0; i < a.length; i++)
{
Sum + = A[i];
}
aver = sum/n-4;
Console.WriteLine ("Two lowest points removed:" + a[0] + "and" + a[1] +
", the two highest points removed:" + a[n-2] + "and" + a[n-1] + "; average score:" + aver);
}
Else
{
Console.WriteLine ("Enter the number of people greater than or equal to 5!! ");
}
Console.ReadLine ();
Array---bubble sort