C#经典排序

来源:互联网 发布:js中获取php变量 编辑:程序博客网 时间:2024/04/26 06:37

转载自:http://blog.csdn.net/asd237241291/article/details/8941676

1.选择排序

  1. class SelectionSorter      
  2. {      
  3.     private int min;      
  4.     public void Sort(int[] arr)      
  5.     {      
  6.         for (int i = 0; i < arr.Length - 1; ++i)      
  7.         {      
  8.             min = i;      
  9.             for (int j = i + 1; j < arr.Length; ++j)      
  10.             {      
  11.                 if (arr[j] < arr[min])      
  12.                     min = j;      
  13.             }      
  14.             int t = arr[min];      
  15.             arr[min] = arr[i];      
  16.             arr[i] = t;      
  17.         }      
  18.     }      
  19.  }   

2.冒泡排序

  1. class EbullitionSorter      
  2. {      
  3.     public void Sort(int[] arr)      
  4.     {      
  5.         int i, j, temp;      
  6.         bool done = false;      
  7.         j = 1;      
  8.         while ((j < arr.Length) && (!done))//判断长度      
  9.         {      
  10.             done = true;      
  11.             for (i = 0; i < arr.Length - j; i++)      
  12.             {      
  13.                 if (arr[i] > arr[i + 1])      
  14.                 {      
  15.                     done = false;      
  16.                     temp = arr[i];      
  17.                     arr[i] = arr[i + 1];//交换数据      
  18.                     arr[i + 1] = temp;      
  19.                 }      
  20.             }      
  21.             j++;      
  22.         }      
  23.     }        
  24. }   

3.快速排序

  1. class QuickSorter      
  2. {      
  3.     private void swap(ref int l, ref int r)      
  4.     {      
  5.         int temp;      
  6.         temp = l;      
  7.         l = r;      
  8.         r = temp;      
  9.     }      
  10.     public void Sort(int[] list, int low, int high)      
  11.     {      
  12.         int pivot;//存储分支点      
  13.         int l, r;      
  14.         int mid;      
  15.         if (high <= low)      
  16.             return;      
  17.         else if (high == low + 1)      
  18.         {      
  19.             if (list[low] > list[high])      
  20.                 swap(ref list[low], ref list[high]);      
  21.             return;      
  22.         }      
  23.         mid = (low + high) >> 1;      
  24.         pivot = list[mid];      
  25.         swap(ref list[low], ref list[mid]);      
  26.         l = low + 1;      
  27.         r = high;      
  28.         do     
  29.         {      
  30.         while (l <= r && list[l] < pivot)      
  31.             l++;      
  32.         while (list[r] >= pivot)      
  33.             r--;      
  34.             if (l < r)      
  35.                 swap(ref list[l], ref list[r]);      
  36.         } while (l < r);      
  37.         list[low] = list[r];      
  38.         list[r] = pivot;      
  39.         if (low + 1 < r)      
  40.             Sort(list, low, r - 1);      
  41.         if (r + 1 < high)      
  42.             Sort(list, r + 1, high);      
  43.     }        
  44. }      

4.插入排序

  1. public class InsertionSorter      
  2. {      
  3.     public void Sort(int[] arr)      
  4.     {      
  5.         for (int i = 1; i < arr.Length; i++)      
  6.         {      
  7.             int t = arr[i];      
  8.             int j = i;      
  9.             while ((j > 0) && (arr[j - 1] > t))      
  10.             {      
  11.                 arr[j] = arr[j - 1];//交换顺序      
  12.                 --j;      
  13.             }      
  14.             arr[j] = t;      
  15.         }      
  16.     }       
  17. }    

5.希尔排序

  1. public class ShellSorter      
  2. {      
  3.     public void Sort(int[] arr)      
  4.     {      
  5.         int inc;      
  6.         for (inc = 1; inc <= arr.Length / 9; inc = 3 * inc + 1) ;      
  7.         for (; inc > 0; inc /= 3)      
  8.         {      
  9.             for (int i = inc + 1; i <= arr.Length; i += inc)      
  10.             {      
  11.                 int t = arr[i - 1];      
  12.                 int j = i;      
  13.                 while ((j > inc) && (arr[j - inc - 1] > t))      
  14.                 {      
  15.                     arr[j - 1] = arr[j - inc - 1];//交换数据      
  16.                     j -= inc;      
  17.                 }      
  18.                 arr[j - 1] = t;      
  19.             }      
  20.         }      
  21.     }     
  22. }     

6.归并排序

  1. <summary>        /// 归并排序之归:归并排序入口  
  2.         /// </summary>        /// <param name="data">无序的数组  
  3.         /// <returns>有序数组</returns>  
  4.         /// <author>Lihua(www.zivsoft.com)</author>  
  5.         int[] Sort(int[] data)  
  6.         {  
  7.             //取数组中间下标  
  8.             int middle = data.Length / 2;  
  9.             //初始化临时数组let,right,并定义result作为最终有序数组  
  10.             int[] left = new int[middle], right = new int[middle], result = new int[data.Length];  
  11.             if (data.Length % 2 != 0)//若数组元素奇数个,重新初始化右临时数组  
  12.             {  
  13.                 right = new int[middle + 1];  
  14.             }  
  15.             if (data.Length <= 1)//只剩下1 or 0个元数,返回,不排序  
  16.             {  
  17.                 return data;  
  18.             }  
  19.             int i = 0, j = 0;  
  20.             foreach (int x in data)//开始排序  
  21.             {  
  22.                 if (i < middle)//填充左数组  
  23.                 {  
  24.                     left[i] = x;  
  25.                     i++;  
  26.                 }  
  27.                 else//填充右数组  
  28.                 {  
  29.                     right[j] = x;  
  30.                     j++;  
  31.                 }  
  32.             }  
  33.             left = Sort(left);//递归左数组  
  34.             right = Sort(right);//递归右数组  
  35.             result = Merge(left, right);//开始排序  
  36.             //this.Write(result);//输出排序,测试用(lihua debug)  
  37.             return result;  
  38.         }  
  39.         /// <summary>  
  40.         /// 归并排序之并:排序在这一步  
  41.         /// </summary>  
  42.         /// <param name="a">左数组  
  43.         /// <param name="b">右数组  
  44.         /// <returns>合并左右数组排序后返回</returns>  
  45.         int[] Merge(int[] a, int[] b)  
  46.         {  
  47.             //定义结果数组,用来存储最终结果  
  48.             int[] result = new int[a.Length + b.Length];  
  49.             int i = 0, j = 0, k = 0;  
  50.             while (i < a.Length && j < b.Length)  
  51.             {  
  52.                 if (a[i] < b[j])//左数组中元素小于右数组中元素  
  53.                 {  
  54.                     result[k++] = a[i++];//将小的那个放到结果数组  
  55.                 }  
  56.                 else//左数组中元素大于右数组中元素  
  57.                 {  
  58.                     result[k++] = b[j++];//将小的那个放到结果数组  
  59.                 }  
  60.             }  
  61.             while (i < a.Length)//这里其实是还有左元素,但没有右元素  
  62.             {  
  63.                 result[k++] = a[i++];  
  64.             }  
  65.             while (j < b.Length)//右右元素,无左元素  
  66.             {  
  67.                 result[k++] = b[j++];  
  68.             }  
  69.             return result;//返回结果数组  
  70.         }  
  71. 注:此算法由周利华提供(http://www.cnblogs.com/architect/archive/2009/05/06/1450489.html   
  72. )  

7.基数排序

  1. //基数排序  
  2.         public int[] RadixSort(int[] ArrayToSort, int digit)  
  3.         {     
  4.             //low to high digit  
  5.             for (int k = 1; k <= digit; k++)  
  6.             {         
  7.                 //temp array to store the sort result inside digit  
  8.                 int[] tmpArray = new int[ArrayToSort.Length];   
  9.                 //temp array for countingsort   
  10.                 int[] tmpCountingSortArray = new int[10]{0,0,0,0,0,0,0,0,0,0};          
  11.                 //CountingSort          
  12.                 for (int i = 0; i < ArrayToSort.Length; i++)          
  13.                 {             
  14.                     //split the specified digit from the element   
  15.                     int tmpSplitDigit = ArrayToSort[i]/(int)Math.Pow(10,k-1) - (ArrayToSort[i]/(int)Math.Pow(10,k))*10;   
  16.                     tmpCountingSortArray[tmpSplitDigit] += 1;   
  17.                 }           
  18.                 for (int m = 1; m < 10; m++)        
  19.                 {              
  20.                     tmpCountingSortArray[m] += tmpCountingSortArray[m - 1];          
  21.                 }          
  22.                 //output the value to result        
  23.                 for (int n = ArrayToSort.Length - 1; n >= 0; n--)         
  24.                 {             
  25.                     int tmpSplitDigit = ArrayToSort[n] / (int)Math.Pow(10,k - 1) - (ArrayToSort[n]/(int)Math.Pow(10,k)) * 10;             
  26.                     tmpArray[tmpCountingSortArray[tmpSplitDigit]-1] = ArrayToSort[n];              
  27.                     tmpCountingSortArray[tmpSplitDigit] -= 1;         
  28.                 }          
  29.                 //copy the digit-inside sort result to source array         
  30.                 for (int p = 0; p < ArrayToSort.Length; p++)         
  31.                 {             
  32.                     ArrayToSort[p] = tmpArray[p];         
  33.                 }     
  34.             }      
  35.             return ArrayToSort;  
  36.         }  

8.计数排序

  1. <summary>        /// counting sort  
  2.         /// </summary>        /// <param name="arrayA">input array  
  3.         /// <param name="arrange">the value arrange in input array  
  4.         /// <returns></returns>  
  5.         public int[] CountingSort(int[] arrayA, int arrange)  
  6.         {      
  7.             //array to store the sorted result,    
  8.             //size is the same with input array.   
  9.             int[] arrayResult = new int[arrayA.Length];      
  10.             //array to store the direct value in sorting process     
  11.             //include index 0;      
  12.             //size is arrange+1;      
  13.             int[] arrayTemp = new int[arrange+1];      
  14.             //clear up the temp array      
  15.             for(int i = 0; i <= arrange; i++)      
  16.             {          
  17.                 arrayTemp[i] = 0;    
  18.             }      
  19.             //now temp array stores the count of value equal    
  20.             for(int j = 0; j < arrayA.Length; j++)     
  21.             {         
  22.                 arrayTemp[arrayA[j]] += 1;     
  23.             }      
  24.             //now temp array stores the count of value lower and equal    
  25.             for(int k = 1; k <= arrange; k++)     
  26.             {         
  27.                 arrayTemp[k] += arrayTemp[k - 1];    
  28.             }       
  29.             //output the value to result      
  30.             for (int m = arrayA.Length-1; m >= 0; m--)     
  31.             {          
  32.                 arrayResult[arrayTemp[arrayA[m]] - 1] = arrayA[m];      
  33.                 arrayTemp[arrayA[m]] -= 1;    
  34.             }      
  35.             return arrayResult;  
  36.         }  

9.小根堆排

  1. <summary>        /// 小根堆排序  
  2.         /// </summary>  
  3.         private void HeapSort(ref double[] dblArray)  
  4.         {  
  5.             for (int i = dblArray.Length - 1; i >= 0; i--)  
  6.             {  
  7.                 if (2 * i + 1 < dblArray.Length)  
  8.                 {  
  9.                     int MinChildrenIndex = 2 * i + 1;  
  10.                     //比较左子树和右子树,记录最小值的Index  
  11.                     if (2 * i + 2 < dblArray.Length)  
  12.                     {  
  13.                         if (dblArray[2 * i + 1] > dblArray[2 * i + 2])  
  14.                             MinChildrenIndex = 2 * i + 2;  
  15.                     }  
  16.                     if (dblArray[i] > dblArray[MinChildrenIndex])  
  17.                     {  
  18.   
  19.   
  20.                         ExchageValue(ref dblArray[i], ref dblArray[MinChildrenIndex]);  
  21.                         NodeSort(ref dblArray, MinChildrenIndex);  
  22.                     }  
  23.                 }  
  24.             }  
  25.         }  
  26.   
  27.         /// <summary>  
  28.         /// 节点排序  
  29.         /// </summary>  
  30.         /// <param name="dblArray">  
  31.         /// <param name="StartIndex">  
  32.   
  33.         private void NodeSort(ref double[] dblArray, int StartIndex)  
  34.         {  
  35.             while (2 * StartIndex + 1 < dblArray.Length)  
  36.             {  
  37.                 int MinChildrenIndex = 2 * StartIndex + 1;  
  38.                 if (2 * StartIndex + 2 < dblArray.Length)  
  39.                 {  
  40.                     if (dblArray[2 * StartIndex + 1] > dblArray[2 * StartIndex + 2])  
  41.                     {  
  42.                         MinChildrenIndex = 2 * StartIndex + 2;  
  43.                     }  
  44.                 }  
  45.                 if (dblArray[StartIndex] > dblArray[MinChildrenIndex])  
  46.                 {  
  47.                     ExchageValue(ref dblArray[StartIndex], ref dblArray[MinChildrenIndex]);  
  48.                     StartIndex = MinChildrenIndex;  
  49.                 }  
  50.             }  
  51.         }  
  52.   
  53.         /// <summary>  
  54.         /// 交换值  
  55.         /// </summary>  
  56.         /// <param name="A">  
  57.         /// <param name="B">  
  58.         private void ExchageValue(ref double A, ref double B)  
  59.         {  
  60.             double Temp = A;  
  61.             A = B;  
  62.             B = Temp;  
  63.         }  

0 0
原创粉丝点击