In programming, the classic sorting method (algorithm) is divided into the following types:
1: insert sort: a. Insert sort directly B. Hill sort
2: Exchange sorting: a. Bubble Sorting B. Fast sorting
3: Select sort directly
4: Merge Sorting:. Merge Sorting
5: sort by allocation: a. sort by Box B. sort by base
Three types of sorting are commonly used: 1. Directly select sorting 2. Directly insert sorting 3. Bubble Sorting;
First: Bubble Sorting
Principle: For a series, we perform Round Robin and switching. the maximum or minimum number of rounds is placed at the end of the pair, and the cycle length is-1.
I. Bubble Sorting: Let the elements in the array compare two (I and I + 1), after n (I-1) over two, the elements in the array are sorted according to our expectation. To sort data from large to small, we use them for comparison.
Example: 10, 20, 30, 40, 50, 60, 70 raw data, 7 elements
Comparison of 20, 30, 40, 50, 60, 70, 10, 1st times
30, 40, 50, 60, 70, 20, 10, 2nd times
40, 50, 60, 70, 30, 20, 10, 3rd Times
50, 60, 70, 40, 30, 20, 10, 4th, 3 times
60, 70, 50, 40, 30, 20, 10, 5th times
70, 60, 50, 40, 30, 20, 10, 6th times
That is, n numbers need to be ranked by n-1; the number of times that the t-train compares is n-t. I = 0 first I = 1 second number of bytes: I + 1 t = I + 1 to write a loop: for (int j = 0; j <n-t; I ++) replace t with I: for (int j = 0; j <n-i-1; I ++)
Apply the formula]
For (int I = 0; I <numbers. length-1; I ++)
{
For (int j = 0; j <numbers. length-1-i; j ++)
{
If (numbers [j] <numbers [j + 1]) // use the "<" number for sorting from large to small; otherwise, use the ">" number.
{
Exchange Values with an intermediate variable
}
}
}
Exercise: an unordered array is arranged in ascending order.
<Span style = "font-size: 16px;"> using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
Int [] scores = {85, 78, 88, 86, 98, 67, 48, 77, 79, 81, 83, 55, 25, 15, 5 };
For (int I = 0; I <scores. Length-1; I ++)
{
For (int j = 0; j <scores. Length-1-I; j ++)
{
If (scores [j] <scores [j + 1]) // use the "<" number for sorting from large to small; otherwise, use the ">" number.
{
Int temp = scores [j];
Scores [j] = scores [j + 1];
Scores [j + 1] = temp;
}
}
}
Console. WriteLine ("the result is sorted in ascending order :");
For (int I = 0; I <scores. Length; I ++)
{
Console. WriteLine (scores [I]);
}
Console. ReadKey ();
}
}
} </Span>
Type 2: Select sorting
Principle: For a series, we select the largest or smallest number, put it at the end of the team, and cyclically go down. The cycle length is-1. Since there is no bubble sort, we need to compare each time, therefore, it is faster than Bubble sorting.
<Span style = "font-size: 16px;"> sort by public class
{
Private int min;
Public void Sort (int [] arr)
{
For (int I = 0; I <arr. length-1; I ++)
{
Min = I;
For (int j = I + 1; j <arr. length; j ++)
{
If (arr [j] <arr [min])
{
Min = j;
}
}
Int temp = arr [min];
Arr [min] = arr [I];
Arr [I] = temp;
}
}
} </Span>
Third: insert sorting
Principle: For a series, we start from the second number and compare it with the number above it.
Or the smallest number is placed at the beginning of the queue, thus forming an ordered queue, so it is faster than selecting to sort.
<Span style = "font-size: 16px;"> sort public class Inserts
{
Public void Sort (int [] arr)
{
For (int I = 1; I <arr. length; I ++)
{
Int temp = arr [I];
Int j = I;
While (j> 0) & (arr (J-1)> temp ))
{
Arr [j] = arr [J-1];
-- J;
}
Arr [j] = temp;
}
}
} </Span>
The preceding three methods are simple and easy to understand. The code is written during the learning notebook. It will inevitably cause some errors. please instruct me!
From Anchor2011_begin