废话不多说,代码如下,基本的解释也有
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SuanFa { class Program { private static int min; static void Main(string[] args) { int[] ints = { 2, 8, 22, 88, 9, 6, 2, 7 }; Sort(ints); Print(ints); int[] ints2 = { 2, 8, 22, 88, 9, 6, 2, 7 }; SortMaoPao(ints2); Print(ints2); int[] ints3 = { 2, 8, 22, 88, 9, 6, 2, 7 }; SortKuaiSu(ints3, 1, 7); Print(ints3); int[] ints4 = { 2, 8, 22, 88, 9, 6, 2, 7 }; SortCharu(ints4); Print(ints4); int[] ints5 = { 2, 8, 22, 88, 9, 6, 2, 7 }; SortXiEr(ints5); Print(ints5); int[] ints6 = { 2, 8, 22, 88, 9, 6, 2, 7 }; Print(SortBingGui(ints6)); Console.Read(); } public static void Print(int[] ints) { foreach (int i in ints) { Console.Write(i + ","); } Console.WriteLine(); } /// <summary> /// 选择排序 /// </summary> /// <param name="arr"></param> public static void Sort(int[] arr) { for (int i = 0; i < arr.Length - 1; ++i) { min = i; for (int j = i + 1; j < arr.Length; ++j) { if (arr[j] < arr[min]) min = j; } int t = arr[min]; arr[min] = arr[i]; arr[i] = t; } } /// <summary> /// 冒泡排序 /// </summary> /// <param name="arr"></param> public static void SortMaoPao(int[] arr) { int i, j, temp; bool done = false; j = 1; while ((j < arr.Length) && (!done))//判断长度 { done = true; for (i = 0; i < arr.Length - j; i++) { if (arr[i] > arr[i + 1]) { done = false; temp = arr[i]; arr[i] = arr[i + 1];//交换数据 arr[i + 1] = temp; } } j++; } } private static void swap(ref int l, ref int r) { int temp; temp = l; l = r; r = temp; } /// <summary> /// 快速排序 /// </summary> /// <param name="list"></param> /// <param name="low">起始位置</param> /// <param name="high">结束位置</param> public static void SortKuaiSu(int[] list, int low, int high) { int pivot;//存储分支点 int l, r; int mid; if (high <= low) return; else if (high == low + 1) { if (list[low] > list[high]) swap(ref list[low], ref list[high]); return; } mid = (low + high) >> 1; pivot = list[mid]; swap(ref list[low], ref list[mid]); l = low + 1; r = high; do { while (l <= r && list[l] < pivot) l++; while (list[r] >= pivot) r--; if (l < r) swap(ref list[l], ref list[r]); } while (l < r); list[low] = list[r]; list[r] = pivot; if (low + 1 < r) SortKuaiSu(list, low, r - 1); if (r + 1 < high) SortKuaiSu(list, r + 1, high); } /// <summary> /// 插入插叙 /// </summary> /// <param name="arr"></param> public static void SortCharu(int[] arr) { for (int i = 1; i < arr.Length; i++) { int t = arr[i]; int j = i; while ((j > 0) && (arr[j - 1] > t)) { arr[j] = arr[j - 1];//交换顺序 --j; } arr[j] = t; } } /// <summary> /// 希尔排序 /// </summary> /// <param name="arr"></param> public static void SortXiEr(int[] list) { int inc; for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ; for (; inc > 0; inc /= 3) { for (int i = inc + 1; i <= list.Length; i += inc) { int t = list[i - 1]; int j = i; while ((j > inc) && (list[j - inc - 1] > t)) { list[j - 1] = list[j - inc - 1]; j -= inc; } list[j - 1] = t; } } } /// <summary> /// 归并排序之归:归并排序入口 /// Updated by Lihua at 05/06/2009 /// </summary> /// <param name="data">无序数组</param> /// <returns>有序数组</returns> /// <author>lihua</author> /// <copyright>www.zivsoft.com</copyright> static int[] SortBingGui(int[] data) { //若data为null,或只剩下1 or 0个元素,返回,不排序 if (null == data || data.Length <= 1) { return data; } //取数组中间下标 //int middle = data.Length / 2; //方法一:除2取整数 int middle = data.Length >> 1; //方法二:位移 (感谢读者hyper给出的这个效率稍高的方法) //初始化临时数组let,right,并定义result作为最终有序数组,若数组元素奇数个,将把多余的那元素空间预留在right临时数组 int[] left = new int[middle], right = new int[data.Length - middle], result = new int[data.Length]; //下面这句对性能有些影响,所以在上面有了改进,直接用data.Length-middle初始化right数组 //if (data.Length % 2 != 0) right = new int[middle + 1]; //int i = 0, j = 0; //foreach (int x in data)//开始排序 //{ // if (i < middle)//填充左数组 // { // left[i] = x; // i++; // } // else//填充右数组 // { // right[j] = x; // j++; // } //} //上面的foreach被改成了for循环 //for (int i = 0; i < data.Length; i++) //{ // if (i < middle)//用middle,不用left.Length // { // left[i] = data[i]; // } // else // { // right[i - middle] = data[i]; //此处i-middle,让我省掉定义一个j,性能有所提高 // } //} //经调查,用Array.Copy的确比上面的for循环优化很多 Array.Copy(data, 0, left, 0, middle);//拷贝左数组 Array.Copy(data, left.Length, right, 0, right.Length); //拷贝右数组 left = SortBingGui(left);//递归左数组 right = SortBingGui(right);//递归右数组 result = Merge(left, right);//开始排序 //this.Write(result);//输出排序,测试用(lihua debug) return result; } /// <summary> /// 归并排序之并:排序在这一步 /// </summary> /// <param name="a">左数组</param> /// <param name="b">右数组</param> /// <returns>合并左右数组排序后返回</returns> static int[] Merge(int[] a, int[] b) { //定义结果数组,用来存储最终结果 int[] result = new int[a.Length + b.Length]; int i = 0, j = 0, k = 0; while (i < a.Length && j < b.Length) { if (a[i] < b[j])//左数组中元素小于右数组中元素 { result[k++] = a[i++];//将小的那个放到结果数组 } else//左数组中元素大于右数组中元素 { result[k++] = b[j++];//将小的那个放到结果数组 } } while (i < a.Length)//这里其实是还有左元素,但没有右元素 { result[k++] = a[i++]; } while (j < b.Length)//右右元素,无左元素 { result[k++] = b[j++]; } return result;//返回结果数组 } } }