排序算法
1.冒泡排序法
冒泡排序算法核心循环代码
1 for (int i = 0; i < num.Length - 1; i++)
2 {
3 for (int j = 0; j < num.Length - 1 - i; j++)
4 {
5 if (num[j] > num[j + 1])
6 {
7 int temp = num[j];
8 num[j] = num[j + 1];
9 num[j + 1] = temp;
10 }
11 }
12 }
2 {
3 for (int j = 0; j < num.Length - 1 - i; j++)
4 {
5 if (num[j] > num[j + 1])
6 {
7 int temp = num[j];
8 num[j] = num[j + 1];
9 num[j + 1] = temp;
10 }
11 }
12 }
2.选择排序算法
代码
1 private static int[] SelectionSort(int[] x)
2 {
3 int[] temp = x;
4 int big = 0;
5 int index = 0;
6
7 for (int i = 0; i < x.Length; i++)
8 {
9 index = i;
10
11 for (int j = i+1; j < x.Length; j++)
12 {
13 if (temp[j] > temp[index])
14 {
15 index = j;
16 }
17 }
18
19 if (i != index)
20 {
21 big = temp[index];
22 temp[index] = temp[i];
23 temp[i] = big;
24 }
25 }
26
27 return temp;
28 }
2 {
3 int[] temp = x;
4 int big = 0;
5 int index = 0;
6
7 for (int i = 0; i < x.Length; i++)
8 {
9 index = i;
10
11 for (int j = i+1; j < x.Length; j++)
12 {
13 if (temp[j] > temp[index])
14 {
15 index = j;
16 }
17 }
18
19 if (i != index)
20 {
21 big = temp[index];
22 temp[index] = temp[i];
23 temp[i] = big;
24 }
25 }
26
27 return temp;
28 }