C# 排序
1using System;
2
3namespace 算法
4{
5 /**//// <summary>
6 /// 排序
7 /// </summary>
8 public static class Sort
9 {
10 ==== 选择排序 ====#region ==== 选择排序 ====
11 /**//// <summary>
12 /// 选择排序
13 /// </summary>
14 /// <param name="list"></param>
15 public static void SelectSort(int[] list)
16 {
17 for (int i = 0; i < list.Length; i++)
18 {
19 int min = i;
20 for (int j = i + 1; j < list.Length; j++)
21 if (list[j] < list[min])
22 min = j;
23
24 if (min != i)
25 {
26 int temp = list[min];
27 list[min] = list[i];
28 list[i] = temp;
29 }
30 }
31 }
32 #endregion
33
34 ==== 冒泡排序 ====#region ==== 冒泡排序 ====
35 /**//// <summary>
36 /// 冒泡排序
37 /// </summary>
38 /// <param name="list"></param>
39 public static void BubbleSort(int[] list)
40 {
41 for (int i = 0; i < list.Length; i++)
42 {
43 for (int j = 0; j < list.Length - i - 1; j++)
44 {
45 if (list[j] > list[j + 1])
46 {
47 int temp = list[j];
48 list[j] = list[j + 1];
49 list[j + 1] = temp;
50 }
51 }
52 }
53 }
54 #endregion
55
56 ==== 插入排序 ====#region ==== 插入排序 ====
57 /**//// <summary>
58 /// 直接插入排序
59 /// </summary>
60 /// <param name="list"></param>
61 public static void InsertSort(int[] list)
62 {
63 for (int i = 1; i < list.Length; i++)
64 {
65 int temp = list[i];
66 int j = i - 1;
67 while (j >= 0 && list[j] > temp)
68 {
69 list[j + 1] = list[j];
70 j--;
71 }
72 list[j + 1] = temp;
73 }
74 }
75
76 /**//// <summary>
77 /// 折半插入排序
78 /// </summary>
79 /// <param name="list"></param>
80 public static void BinarySort(int[] list)
81 {
82 for (int i = 1; i < list.Length; i++)
83 {
84 int low = 0;
85 int high = i - 1;
86 int temp = list[i];
87
88 //查找
89 while (low <= high)
90 {
91 int mid = (low + high) / 2;
92
93 if (temp < list[mid])
94 high = mid - 1;
95 else
96 low = mid + 1;
97 }
98
99 //向后移
100 for (int j = i - 1; j >= low; j--)
101 list[j + 1] = list[j];
102
103 list[low] = temp;
104 }
105 }
106 #endregion
107
108 ==== 快速排序 ====#region ==== 快速排序 ====
109 /**//// <summary>
110 /// 快排 一次划分
111 /// </summary>
112 /// <param name="list">划分的数组</param>
113 /// <param name="i">数组低端上标</param>
114 /// <param name="j">数组高端下标</param>
115 /// <returns>返回划分后基准的位置</returns>
116 private static int Partition(int[] list, int i, int j)
117 {
118 int key = list[i];
119
120 while (i < j)
121 {
122 //j 向左扫描
123 while (list[j] >= key && i < j)
124 j--;
125
126 if (i < j)
127 list[i++] = list[j];
128
129 //i 向右扫描
130 while (list[i] <= key && i < j)
131 i++;
132
133 if (i < j)
134 list[j--] = list[i];
135 }
136
137 list[i] = key;
138 return i;
139 }
140
141 /**//// <summary>
142 /// 快速排序
143 /// </summary>
144 /// <param name="list"></param>
145 public static void QuickSort(int[] list, int low, int high)
146 {
147 if (low < high - 1)
148 {
149 int key = Partition(list, low, high);
150 QuickSort(list, low, key - 1);
151 QuickSort(list, key + 1, high);
152 }
153 }
154 #endregion
155
156 ==== 堆排序 ====#region ==== 堆排序 ====
157 /**//// <summary>
158 /// 堆排 调整成堆
159 /// </summary>
160 /// <param name="list"></param>
161 /// <param name="i"></param>
162 /// <param name="m"></param>
163 private static void Adjust(int[] list, int i, int m)
164 {
165 int temp = list[i];
166 int j = i * 2 + 1;
167
168 while (j <= m)
169 {
170 //比较左右孩子
171 if (j < m)
172 if (list[j] < list[j + 1])
173 j = j + 1;
174
175 //比较根结点和大孩子
176 if (temp < list[j])
177 {
178 list[i] = list[j];
179 i = j;
180 j = 2 * i + 1;
181 }
182 else
183 {
184 j = m + 1;
185 }
186 }
187
188 list[i] = temp;
189 }
190
191 /**//// <summary>
192 /// 堆排序
193 /// </summary>
194 /// <param name="list"></param>
195 public static void HeapSort(int[] list)
196 {
197 //建立初始堆
198 for (int i = (list.Length - 1) / 2; i >= 0; i--)
199 Adjust(list, i, list.Length - 1);
200
201 //交换根结点和堆的最后一个结点
202 for (int i = list.Length - 1; i >= 1; i--)
203 {
204 int temp = list[0];
205 list[0] = list[i];
206 list[i] = temp;
207 Adjust(list, 0, i - 1);
208 }
209 }
210 #endregion
211 }
212}
2
3namespace 算法
4{
5 /**//// <summary>
6 /// 排序
7 /// </summary>
8 public static class Sort
9 {
10 ==== 选择排序 ====#region ==== 选择排序 ====
11 /**//// <summary>
12 /// 选择排序
13 /// </summary>
14 /// <param name="list"></param>
15 public static void SelectSort(int[] list)
16 {
17 for (int i = 0; i < list.Length; i++)
18 {
19 int min = i;
20 for (int j = i + 1; j < list.Length; j++)
21 if (list[j] < list[min])
22 min = j;
23
24 if (min != i)
25 {
26 int temp = list[min];
27 list[min] = list[i];
28 list[i] = temp;
29 }
30 }
31 }
32 #endregion
33
34 ==== 冒泡排序 ====#region ==== 冒泡排序 ====
35 /**//// <summary>
36 /// 冒泡排序
37 /// </summary>
38 /// <param name="list"></param>
39 public static void BubbleSort(int[] list)
40 {
41 for (int i = 0; i < list.Length; i++)
42 {
43 for (int j = 0; j < list.Length - i - 1; j++)
44 {
45 if (list[j] > list[j + 1])
46 {
47 int temp = list[j];
48 list[j] = list[j + 1];
49 list[j + 1] = temp;
50 }
51 }
52 }
53 }
54 #endregion
55
56 ==== 插入排序 ====#region ==== 插入排序 ====
57 /**//// <summary>
58 /// 直接插入排序
59 /// </summary>
60 /// <param name="list"></param>
61 public static void InsertSort(int[] list)
62 {
63 for (int i = 1; i < list.Length; i++)
64 {
65 int temp = list[i];
66 int j = i - 1;
67 while (j >= 0 && list[j] > temp)
68 {
69 list[j + 1] = list[j];
70 j--;
71 }
72 list[j + 1] = temp;
73 }
74 }
75
76 /**//// <summary>
77 /// 折半插入排序
78 /// </summary>
79 /// <param name="list"></param>
80 public static void BinarySort(int[] list)
81 {
82 for (int i = 1; i < list.Length; i++)
83 {
84 int low = 0;
85 int high = i - 1;
86 int temp = list[i];
87
88 //查找
89 while (low <= high)
90 {
91 int mid = (low + high) / 2;
92
93 if (temp < list[mid])
94 high = mid - 1;
95 else
96 low = mid + 1;
97 }
98
99 //向后移
100 for (int j = i - 1; j >= low; j--)
101 list[j + 1] = list[j];
102
103 list[low] = temp;
104 }
105 }
106 #endregion
107
108 ==== 快速排序 ====#region ==== 快速排序 ====
109 /**//// <summary>
110 /// 快排 一次划分
111 /// </summary>
112 /// <param name="list">划分的数组</param>
113 /// <param name="i">数组低端上标</param>
114 /// <param name="j">数组高端下标</param>
115 /// <returns>返回划分后基准的位置</returns>
116 private static int Partition(int[] list, int i, int j)
117 {
118 int key = list[i];
119
120 while (i < j)
121 {
122 //j 向左扫描
123 while (list[j] >= key && i < j)
124 j--;
125
126 if (i < j)
127 list[i++] = list[j];
128
129 //i 向右扫描
130 while (list[i] <= key && i < j)
131 i++;
132
133 if (i < j)
134 list[j--] = list[i];
135 }
136
137 list[i] = key;
138 return i;
139 }
140
141 /**//// <summary>
142 /// 快速排序
143 /// </summary>
144 /// <param name="list"></param>
145 public static void QuickSort(int[] list, int low, int high)
146 {
147 if (low < high - 1)
148 {
149 int key = Partition(list, low, high);
150 QuickSort(list, low, key - 1);
151 QuickSort(list, key + 1, high);
152 }
153 }
154 #endregion
155
156 ==== 堆排序 ====#region ==== 堆排序 ====
157 /**//// <summary>
158 /// 堆排 调整成堆
159 /// </summary>
160 /// <param name="list"></param>
161 /// <param name="i"></param>
162 /// <param name="m"></param>
163 private static void Adjust(int[] list, int i, int m)
164 {
165 int temp = list[i];
166 int j = i * 2 + 1;
167
168 while (j <= m)
169 {
170 //比较左右孩子
171 if (j < m)
172 if (list[j] < list[j + 1])
173 j = j + 1;
174
175 //比较根结点和大孩子
176 if (temp < list[j])
177 {
178 list[i] = list[j];
179 i = j;
180 j = 2 * i + 1;
181 }
182 else
183 {
184 j = m + 1;
185 }
186 }
187
188 list[i] = temp;
189 }
190
191 /**//// <summary>
192 /// 堆排序
193 /// </summary>
194 /// <param name="list"></param>
195 public static void HeapSort(int[] list)
196 {
197 //建立初始堆
198 for (int i = (list.Length - 1) / 2; i >= 0; i--)
199 Adjust(list, i, list.Length - 1);
200
201 //交换根结点和堆的最后一个结点
202 for (int i = list.Length - 1; i >= 1; i--)
203 {
204 int temp = list[0];
205 list[0] = list[i];
206 list[i] = temp;
207 Adjust(list, 0, i - 1);
208 }
209 }
210 #endregion
211 }
212}