常见排序算法的实现和比较

  1     class Program
  2     {
  3 
  4         static void Main(string[] args)
  5         {
  6             int[] arr = new int[] { 2, 4, 1, 7, 4, 9, 6, 0, 1, 5, 8, 3 };
  7             List<int> list = MergeSort(arr.ToList<int>());
  8 
  9             foreach (int num in list)
 10                 Console.WriteLine(num);
 11         }
 12 
 13 
 14         /// <summary>
 15         /// 7.
 16         /// 堆排序
 17         /// 不稳定
 18         /// nlogn
 19         /// </summary>
 20         public static void HeapSort(int[] arr)
 21         {
 22             if (arr == null || arr.Length < 1)
 23                 return;
 24 
 25             //构建堆
 26             for (int i = arr.Length / 2; i >= 0; i--)
 27             {
 28                 AdjustHeap(arr, i, arr.Length);
 29             }
 30 
 31             //堆排序
 32             for (int i = arr.Length - 1; i > 0; i--)
 33             {
 34                 Swap(arr, 0, i);
 35                 AdjustHeap(arr, 0,i);
 36             }
 37         }
 38 
 39         /// <summary>
 40         /// 调整大根堆
 41         /// </summary>
 42         public static void AdjustHeap(int[] arr,int index,int len)
 43         {
 44             int left = index * 2 + 1;
 45             int right = index * 2 + 2;
 46             int large = index;
 47 
 48             if(left< len && arr[left]>arr[large])
 49                 large = left;
 50 
 51             if (right < len && arr[right] > arr[large])
 52                 large = right;
 53 
 54             if (large != index)
 55             {
 56                 Swap(arr, large, index);
 57                 AdjustHeap(arr, large, len);
 58             }
 59         }
 60 
 61         /// <summary>
 62         /// 6.
 63         /// 归并排序
 64         /// 稳定
 65         /// nlogn
 66         /// </summary>
 67         public static List<int> MergeSort(List<int> list)
 68         {
 69             if (list == null || list.Count <= 1)
 70                 return list;
 71 
 72             List<int> left = new List<int>();
 73             List<int> right = new List<int>();
 74 
 75             int mid = list.Count / 2;
 76 
 77             for (int i = 0; i < mid; i++)
 78                 left.Add(list[i]);
 79 
 80             for (int i = mid; i < list.Count; i++)
 81                 right.Add(list[i]);
 82 
 83             left = MergeSort(left);
 84             right = MergeSort(right);
 85 
 86             return Merge(left, right);
 87         }
 88 
 89         public static List<int> Merge(List<int> left,List<int> right)
 90         {
 91             int lenL = left.Count;
 92             int lenR = right.Count;
 93             List<int> rs = new List<int>();
 94 
 95             while (left.Count > 0 && right.Count > 0)
 96             {
 97                 if (left[0] < right[0])
 98                 {
 99                     rs.Add(left[0]);
100                     left.RemoveAt(0);
101                 }
102                 else
103                 {
104                     rs.Add(right[0]);
105                     right.RemoveAt(0);
106                 }
107             }
108 
109             rs.AddRange(right.Count > 0 ? right : left);
110 
111             return rs;
112         }
113 
114         /// <summary>
115         /// 5.
116         /// 希尔排序
117         /// 不稳定
118         /// nlogn
119         /// </summary>
120         public static void ShellSort(int[] arr)
121         {
122             if (arr == null || arr.Length < 1)
123                 return;
124 
125             int gap = arr.Length / 2;
126 
127             for (; gap > 0; gap /= 2)
128             {
129                 for (int i = gap; i < arr.Length; i++)
130                 {
131                     int j = i;
132                     int temp = arr[i];
133 
134                     while (j-gap >= 0 && arr[j - gap] > temp)
135                     {
136                         arr[j] = arr[j - gap];
137                         j -= gap;
138                     }
139 
140                     arr[j] = temp;
141                 }
142             }
143         }
144 
145         /// <summary>
146         /// 4.
147         /// 快速排序
148         /// 不稳定
149         /// nlogn
150         /// </summary>
151         public static void QuickSort(int[] arr,int start,int end)
152         {
153             if (arr == null || arr.Length < 1 || start>=end)
154                 return;
155 
156             int mid = Partition(arr, start, end);
157 
158             QuickSort(arr, start, mid - 1);
159             QuickSort(arr, mid + 1, end);
160         }
161 
162         /// <summary>
163         /// Partition方法
164         /// </summary>
165         public static int Partition(int[] arr, int start, int end)
166         {
167             int key = arr[start];
168 
169             while (start < end)
170             {
171                 while (start < end && arr[end] >= key)
172                     end--;
173                 arr[start] = arr[end];
174 
175                 while (start < end && arr[start] <= key)
176                     start++;
177                 arr[end] = arr[start];
178             }
179 
180             arr[start] = key;
181 
182             return start;
183         }
184 
185         /// <summary>
186         /// 3.
187         /// 选择排序
188         /// 不稳定
189         /// n^2
190         /// </summary>
191         public static void SelectionSort(int[] arr)
192         {
193             if (arr == null || arr.Length < 1)
194                 return;
195 
196             for (int i = 0; i < arr.Length - 1; i++)
197             {
198                 int maxIndex = i;
199 
200                 for (int j = i + 1; j < arr.Length; j++)
201                 {
202                     maxIndex = arr[j] < arr[maxIndex] ? j : maxIndex;
203                 }
204 
205                 Swap(arr, i, maxIndex);
206             }
207         }
208 
209         /// <summary>
210         /// 2.
211         /// 插入排序
212         /// 稳定
213         /// n^2
214         /// </summary>
215         public static void InsertSort(int[] arr)
216         {
217             if (arr == null || arr.Length < 1)
218                 return;
219 
220             for (int i = 1; i < arr.Length; i++)
221             {
222                 for (int j = i - 1; j >= 0; j--)
223                 {
224                     if (arr[j] > arr[j + 1])
225                         Swap(arr, j, j + 1);
226                     else
227                         break;
228                 }
229             }
230         }
231 
232         /// <summary>
233         /// 1.
234         /// 冒泡排序
235         /// 稳定
236         /// n^2
237         /// </summary>
238         public static void BubbleSort(int[] arr)
239         {
240             if (arr == null || arr.Length < 1)
241                 return;
242 
243             for (int i = 0; i < arr.Length; i++)
244             {
245                 bool flag = true;
246 
247                 for (int j = 0; j < arr.Length - i - 1; j++)
248                 {
249                     if (arr[j] > arr[j + 1])
250                     {
251                         flag = false;
252                         Swap(arr, j, j + 1);
253                     }
254                 }
255 
256                 if (flag) return;
257             }
258         }
259 
260         /// <summary>
261         /// 交换数组中的两个数
262         /// </summary>
263         public static void Swap(int[] arr, int indexA, int indexB)
264         {
265             int temp = arr[indexA];
266             arr[indexA] = arr[indexB];
267             arr[indexB] = temp;
268         }
269     }

 

posted @ 2015-09-21 15:46  叫我霍啊啊啊  阅读(174)  评论(0编辑  收藏  举报