Several methods of ordering summary

Bubble Sort

 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             for (int m = 0; m < a.Length; m++)
12             {
13                 for ( int n-= 0 ; n-<A.length - . 1 ; n-++ )
 14                  {
 15                      IF ([n-] A> A [n-+ . 1 ])        // small to large number greater than, less than vice versa No. 
16                      {
 . 17                          int TEMP;
 18 is                          TEMP = A [n-]; 
 . 19                          A [n-] = A [n-+ . 1 ]; 
 20 is                          A [n-+ . 1 ] = TEMP;
 21 is                      }
 22 is                  }
 23 is             }
24 
25             Console.WriteLine("===================");
26             for (int l = 0; l < a.Length; l++)
27             {
28                 Console.WriteLine(a[l]);
29                 
30             }
31 
32             Console.ReadKey();
33 
34 
35         }
Bubble Sort

Selection Sort

 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         }
插入排序

 

 

Guess you like

Origin www.cnblogs.com/Johnfx-home/p/12385963.html