버블 정렬
1 정적 무효 메인 ( 문자열 [] 인수)
이 {
3
4 INT [A는 = 새로운 지능 [ 10 ];
5
6 대 ( INT가 나는 = 0 ; I <a.Length; I ++ )
7 {
8 A [I] = Convert.ToInt32 (Console.ReadLine ());
9 }
10
11 대 ( INT m = 0 ; m <a.Length; m ++ )
12 {
13 대 ( INT N- = 0 <- A.length N- . 1 - N ++ )
14 {
15 IF (A [N- + [N-] A> . 1 ]) // 작은 다수의 더 많은 행 미만 담당보다 제 반대로
16 {
. 17 INT TEMP;
18이다 TEMP = ; A [N-]
. 19 A [N-] = A [N- + 1이다. ]
(20)가 A [N- +는 1. ] = TEMP;
21이다 }
22이다 }
23은 }
24
25 Console.WriteLine ( " =================== " );
26 대 ( INT를 L = 0 ; L <a.Length L ++ )
27 {
28 Console.WriteLine (a [1]);
29
30 }
31
32 Console.ReadKey ();
33
34
35 }
선택 정렬
1 static void Main(string[] args)
2 {
3
4 int[] a = new int[10];
5
6 for (int i = 0; i < a.Length; i++)
7 {
8 a[i] = Convert.ToInt32(Console.ReadLine());
9 }
10
11
12 int min_index;
13
14
15 for (int n = 0; n < a.Length - 1; n++)
16 {
17 min_index = n; //设置第一个数为最小值
18
19 //从第二个数到最后一个数进行循环
20 for (int j = n + 1; j < a.Length; j++)
21 {
22 //把从第二个数到最后的每个数和第一个数比较,如果比他小,就把索引赋值
23 if (a[j] < a[min_index])
24 {
25 min_index = j;
26 }
27
28 //找到最小项交换,即将这一项移到列表中的正确位置
29 if (min_index != n)
30
31 {
32 int x;
33 x = a[n];
34 a[n] = a[min_index];
35 a[min_index] = x;
36 }
37 }
38
39 }
40
41
42 Console.WriteLine("===================");
43 for (int l = 0; l < a.Length; l++)
44 {
45 Console.WriteLine(a[l]);
46
47 }
48
49 Console.ReadKey();
50
51
52 }
插入排序
1 static void Main(string[] args)
2 {
3
4 int[] a = new int[10];
5
6 for (int i = 0; i < a.Length; i++)
7 {
8 a[i] = Convert.ToInt32(Console.ReadLine());
9 }
10
11 //循环从第二个数组元素开始,因为a[0]作为最初已排序部分
12 for (int j = 1; j < a.Length; j++)
13 {
14 //temp标记为未排序第一个元素
15 int temp = a[j];
16 int x = j - 1;
17
18 /*将temp与已排序元素从小到大比较,寻找temp应插入的位置*/
19 while (x >= 0 && a[x] > temp)
20 {
21 a[x + 1] = a[x];
22 x--;
23 }
24
25 a[x + 1] = temp;
26
27 }
28
29
30 Console.WriteLine("===================");
31 for (int l = 0; l < a.Length; l++)
32 {
33 Console.WriteLine(a[l]);
34
35 }
36
37 Console.ReadKey();
38
39
40 }