C#实现所有经典排序算法(转载)

来源:互联网 发布:c语言希尔排序算法 编辑:程序博客网 时间:2024/06/05 18:42
 
  1. //选择排序      
  2.   
  3. class SelectionSorter      
  4.   
  5. {      
  6.   
  7.     private int min;      
  8.   
  9.     public void Sort(int[] arr)      
  10.   
  11.     {      
  12.   
  13.         for (int i = 0; i < arr.Length - 1; ++i)      
  14.   
  15.         {      
  16.   
  17.             min = i;      
  18.   
  19.             for (int j = i + 1; j < arr.Length; ++j)      
  20.   
  21.             {      
  22.   
  23.                 if (arr[j] < arr[min])      
  24.   
  25.                     min = j;      
  26.   
  27.             }      
  28.   
  29.             int t = arr[min];      
  30.   
  31.             arr[min] = arr[i];      
  32.   
  33.             arr[i] = t;      
  34.   
  35.         }      
  36.   
  37.     }      
  38.   
  39.   
  40.   
  41.     static void Main(string[] args)      
  42.   
  43.     {      
  44.   
  45.         int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };      
  46.   
  47.         SelectionSorter s = new SelectionSorter();      
  48.   
  49.         s.Sort(array);      
  50.   
  51.         foreach (int m in array)      
  52.   
  53.             Console.WriteLine("{0}", m);      
  54.   
  55.     }      
  56.   
  57. }      
  58.   
  59.   
  60.   
  61. //冒泡排序      
  62.   
  63. class EbullitionSorter      
  64.   
  65. {      
  66.   
  67.     public void Sort(int[] arr)      
  68.   
  69.     {      
  70.   
  71.         int i, j, temp;     
  72.   
  73.         bool done = false;      
  74.   
  75.         j = 1;      
  76.   
  77.         while ((j < arr.Length) && (!done))//判断长度      
  78.   
  79.         {      
  80.   
  81.             done = true;      
  82.   
  83.             for (i = 0; i < arr.Length - j; i++)      
  84.   
  85.             {      
  86.   
  87.                 if (arr[i] > arr[i + 1])      
  88.   
  89.                 {      
  90.   
  91.                     done = false;      
  92.   
  93.                     temp = arr[i];      
  94.   
  95.                     arr[i] = arr[i + 1];//交换数据      
  96.   
  97.                     arr[i + 1] = temp;      
  98.   
  99.                 }      
  100.   
  101.             }      
  102.   
  103.             j++;      
  104.   
  105.         }      
  106.   
  107.     }      
  108.   
  109.   
  110.   
  111.   
  112.   
  113.     static void Main(string[] args)      
  114.   
  115.     {      
  116.   
  117.         int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };      
  118.   
  119.         EbullitionSorter e = new EbullitionSorter ();      
  120.   
  121.         e.Sort(array);      
  122.   
  123.         foreach (int m in array)      
  124.   
  125.             Console.WriteLine("{0}", m);      
  126.   
  127.     }      
  128.   
  129. }      
  130.   
  131.   
  132.   
  133. //快速排序      
  134.   
  135. class QuickSorter      
  136.   
  137. {      
  138.   
  139.     private void swap(ref int l, ref int r)      
  140.   
  141.     {      
  142.   
  143.         int temp;     
  144.   
  145.         temp = l;      
  146.   
  147.         l = r;      
  148.   
  149.         r = temp;      
  150.   
  151.     }      
  152.   
  153.   
  154.   
  155.     public void Sort(int[] list, int low, int high)      
  156.   
  157.     {      
  158.   
  159.         int pivot;//存储分支点      
  160.   
  161.         int l, r;      
  162.   
  163.         int mid;      
  164.   
  165.         if (high <= low)     
  166.   
  167.             return;      
  168.   
  169.         else if (high == low + 1)      
  170.   
  171.         {      
  172.   
  173.             if (list[low] > list[high])      
  174.   
  175.                 swap(ref list[low], ref list[high]);      
  176.   
  177.             return;      
  178.   
  179.         }      
  180.   
  181.         mid = (low + high) >> 1;      
  182.   
  183.         pivot = list[mid];      
  184.   
  185.         swap(ref list[low], ref list[mid]);      
  186.   
  187.         l = low + 1;      
  188.   
  189.         r = high;      
  190.   
  191.         do     
  192.   
  193.         {      
  194.   
  195.             while (l <= r && list[l] < pivot)      
  196.   
  197.                 l++;      
  198.   
  199.             while (list[r] >= pivot)      
  200.   
  201.                 r--;      
  202.   
  203.             if (l < r)      
  204.   
  205.                 swap(ref list[l], ref list[r]);      
  206.   
  207.         } while (l < r);      
  208.   
  209.         list[low] = list[r];      
  210.   
  211.         list[r] = pivot;      
  212.   
  213.         if (low + 1 < r)      
  214.   
  215.             Sort(list, low, r - 1);      
  216.   
  217.         if (r + 1 < high)      
  218.   
  219.             Sort(list, r + 1, high);      
  220.   
  221.     }   
  222.   
  223.   
  224.   
  225.     static void Main(string[] args)      
  226.   
  227.     {      
  228.   
  229.         int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };      
  230.   
  231.         QuickSorter q = new QuickSorter();      
  232.   
  233.         q.Sort(iArrary, 0, 13);      
  234.   
  235.         for (int m = 0; m <= 13; m++)      
  236.   
  237.             Console.WriteLine("{0}", iArrary[m]);      
  238.   
  239.     }   
  240.   
  241. }      
  242.   
  243.   
  244.   
  245. //插入排序      
  246.   
  247. public class InsertionSorter      
  248.   
  249. {      
  250.   
  251.     public void Sort(int[] arr)      
  252.   
  253.     {      
  254.   
  255.         for (int i = 1; i < arr.Length; i++)      
  256.   
  257.         {      
  258.   
  259.             int t = arr[i];      
  260.   
  261.             int j = i;      
  262.   
  263.             while ((j > 0) && (arr[j - 1] > t))      
  264.   
  265.             {      
  266.   
  267.                 arr[j] = arr[j - 1];//交换顺序      
  268.   
  269.                 --j;      
  270.   
  271.             }      
  272.   
  273.             arr[j] = t;      
  274.   
  275.         }      
  276.   
  277.     }      
  278.   
  279.   
  280.   
  281.     static void Main(string[] args)      
  282.   
  283.     {      
  284.   
  285.         int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };      
  286.   
  287.         InsertionSorter i = new InsertionSorter();      
  288.   
  289.         i.Sort(array);      
  290.   
  291.         foreach (int m in array)      
  292.   
  293.             Console.WriteLine("{0}", m);       
  294.   
  295.     }      
  296.   
  297. }      
  298.   
  299.   
  300.   
  301. //希尔排序      
  302.   
  303.   
  304.   
  305. public class ShellSorter      
  306.   
  307. {      
  308.   
  309.     public void Sort(int[] arr)      
  310.   
  311.     {      
  312.   
  313.         int inc;      
  314.   
  315.         for (inc = 1; inc <= arr.Length / 9; inc = 3 * inc + 1) ;      
  316.   
  317.         for (; inc > 0; inc /= 3)      
  318.   
  319.         {      
  320.   
  321.             for (int i = inc + 1; i <= arr.Length; i += inc)      
  322.   
  323.             {      
  324.   
  325.                 int t = arr[i - 1];      
  326.   
  327.                 int j = i;      
  328.   
  329.                 while ((j > inc) && (arr[j - inc - 1] > t))      
  330.   
  331.                 {      
  332.   
  333.                     arr[j - 1] = arr[j - inc - 1];//交换数据      
  334.   
  335.                     j -= inc;      
  336.   
  337.                 }      
  338.   
  339.                 arr[j - 1] = t;      
  340.   
  341.             }      
  342.   
  343.         }      
  344.   
  345.     }      
  346.   
  347.   
  348.   
  349.     static void Main(string[] args)      
  350.   
  351.     {      
  352.   
  353.         int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };      
  354.   
  355.         ShellSorter s = new ShellSorter();      
  356.   
  357.         s.Sort(array);      
  358.   
  359.         foreach (int m in array)      
  360.   
  361.             Console.WriteLine("{0}", m);    
  362.   
  363.     }      
  364.   
  365. }   
原创粉丝点击