-------- 2016-10-23
***************************************************************************************************
對比相鄰的元素值,滿足條件就交換兩個元素的位置,把較小的移到數組前邊,較大的數移到數組後邊,這樣較小的元素就像氣泡上升到前面,較大的像氣泡移到後邊。
1 class Program
2 {
3 static void Maopao(int[] arr)
4 {
5 bool f = false;
6 do
7 {
8 f = false;
9 for (int i = 0; i < arr.Length - 1; i++)
10 {
11 if (arr[i] > arr[i + 1])
12 {
13 int temp = arr[i];
14 arr[i] = arr[i + 1];
15 arr[i + 1] = temp;
16 f = true;
17 }
18 }
19 } while (f);
20 }
21 static void Main(string[] args)
22 {
23 int[] list = new int[] { 2, 5, 1, 8, 45, 23, 20, 12 };
24 Maopao(list);
25 foreach (var item in list)
26 {
27 Console.WriteLine(item);
28 }
29 Console.ReadKey();
30 }
31 }
1 class Program
2 {
3 static void Maopao(int[] arr)
4 {
5 for (int i = 1; i < arr.Length; i++)//
6 {
7 for (int j = 0; j < arr.Length-i; j++)
8 {
9 if(arr[j] > arr[j+1])
10 {
11 int temp = arr[j];
12 arr[j] = arr[j + 1];
13 arr[j + 1] = temp;
14 }
15 }
16 }
17 }
18 static void Main(string[] args)
19 {
20 int[] list = new int[] { 2, 5, 1, 8, 45, 23, 20, 12 };
21 Maopao(list);
22 foreach (var item in list)
23 {
24 Console.WriteLine(item);
25 }
26 Console.ReadKey();
27 }
28 }
(本人的理解)操作一:將第一位數假設為最大值或者最小值與後邊的數進行比較,而後將最大的值或者最小的值和第一位的數交換
操作二:將第二位數和他後邊的數進行比較交換,重復操作一。
以此類推,排序。
【選擇排序(Selection sort)是一種簡單直觀的排序算法。它的工作原理是每一次從待排序的數據元素中選出最小(或最大)的一個元素,存放在序列的起始位置,直到全部待排序的數據元素排完。 選擇排序是不穩定的排序方法(比如序列[5, 5, 3]第一次就將第一個[5]與[3]交換,導致第一個5挪動到第二個5後面)。】
1 class Program
2 {
3 static void SelectSort(int[] arr)
4 {
5 int length = arr.Length;
6 for (int i = 0; i < length-1; i++)
7 {
8 int temp=arr[i];//元素值
9 int em=i;//索引
10 for (int j = i+1; j < length; j++)
11 {
12 if (arr[j] < temp)
13 {
14 temp = arr[j];
15 arr[j] = arr[em];
16 arr[em] = temp;
17 }
18 }
19 }
20 }
21 static void Main(string[] args)
22 {
23 int[] list = new int[] { 2, 5, 1, 8, 45, 23, 20, 12 };
24 SelectSort(list);
25 foreach (var item in list)
26 {
27 Console.WriteLine(item);
28 }
29 Console.ReadKey();
30 }
31 }
1 class Program
2 {
3 static void SelectSort(int[] arr)
4 {
5 int length = arr.Length;
6 for (int i = 0; i < length-1; i++)
7 {
8 int min=arr[i];//元素值
9 int em=i;//索引
10 for (int j = i+1; j < length; j++)
11 {
12 if (arr[j] < min)
13 {
14 min = arr[j];
15 em = j;
16 }
17 }
18 if(em != i)
19 {
20 int temp = arr[i];
21 arr[i] = arr[em];
22 arr[em] = temp;
23 }
24 }
25 }
26 static void Main(string[] args)
27 {
28 int[] list = new int[] { 2, 5, 1, 8, 45, 23, 20, 12 };
29 SelectSort(list);
30 foreach (var item in list)
31 {
32 Console.WriteLine(item);
33 }
34 Console.ReadKey();
35 }
36 }
直接排序是從第一位元素開始,依次取第二個數及其後邊的數與前邊的數比較進行分批次排序的。
1 class Program
2 {
3 static void DirectSort(int[] arr)
4 {
5 int length = arr.Length;
6 bool f = false;
7 for (int i = 1; i < length; i++)
8 {
9 int temp = arr[i];//保留i的位置,避免被覆蓋
10 f = false;
11 //拿到i位置的元素,和前面所有元素比較,發現比i大的就向後移動
12 for (int j = i-1; j >= 0; j--)//從後向前
13 {
14 if (arr[j] > temp)
15 {
16 arr[j + 1] = arr[j];//向後移動
17 }
18 else
19 {
20 arr[j + 1] = temp;
21 f = true;
22 break;
23 }
24 }
25 if(f==false)
26 {
27 arr[0] = temp;
28 }
29 }
30 }
31 static void Main(string[] args)
32 {
33 int[] list = new int[] { 2, 5, 1, 8, 45, 23, 20, 12 };
34 DirectSort(list);
35 foreach (var item in list)
36 {
37 Console.WriteLine(item);
38 }
39 Console.ReadKey();
40 }
41 }
操作一:一列數,先把第一位數看做是基准數A(標兵),把小於A的數放在A的左邊,大於A的數放在右邊;
操作二:再將左邊和右邊的數列分別重復操作一
以此類推,直至排好
1 class Program
2 {
3 /// <summary>
4 /// 對數組arrea中索引從left到right之間的數做排序
5 /// </summary>
6 /// <param name="arrea">要排序的數組</param>
7 /// <param name="left">要排序數據的開始索引</param>
8 /// <param name="right">要排序數據的結束索引</param>
9 static void QuickSort(int[] arrea, int left, int right)
10 {
11 if (left < right)//left到right之間的數據做排序
12 {
13 //首先取得一個基准數,把比他小或者等於他的放在它的左邊,然後把比他大的放在它的右邊
14 int temp = arrea[left];
15 int i = left;
16 int j = right;//用來做循環的標志位
17 while (true && i < j)//當i==j時候,表示找到了一個中間位置,這個中間位置就是基准數應該所在的位置
18 {
19 //排序。從後向前進行比較,將後邊的比基准書小或者等於的放到前邊
20 while (true && i < j)//j不能無限制的小下去
21 {
22 if (arrea[j] <= temp)
23 {
24 arrea[i] = arrea[j];
25 break;
26 }
27 else
28 {
29 j--;
30 }
31 }
32 //從前往後,找一個比temp大的數字,放入後邊剛剛調走的地方
33 while (true && i < j)
34 {
35 if (arrea[i] > temp)
36 {
37 arrea[j] = arrea[i];
38 break;
39 }
40 else
41 {
42 i++;
43 }
44 }
45 }
46 //跳出循環,現在i==j,i是中間位置
47 arrea[i] = temp;
48 //對第一次大循環後的序列的左右兩個區間分別進行排序
49 QuickSort(arrea, left, i - 1);//對左邊進行排序
50 QuickSort(arrea, i + 1, right);//對右邊進行排序
51 }
52 }
53 static void Main(string[] args)
54 {
55 int[] data = new int[] { 42, 20, 17, 27, 13, 8, 17, 48 };
56 QuickSort(data, 0, data.Length - 1);
57 foreach (var item in data)
58 {
59 Console.WriteLine(item);
60 }
61 Console.ReadKey();
62 }
63 }
***********************未待完續,持續更新****************************
---恢復內容結束---