C#实现所有经典排序算法

大家都很熟悉用javac实现经典排序算法,下面就用C#实现各种排序,具体代码见下面示例。 
  选择排序 




class SelectionSorter
  {
  private int min;
  public void Sort(int[] arr)
  {
  for (int i = 0; i %26lt; arr.Length - 1; ++i)
  {
  min = i;
  for (int j = i + 1; j %26lt; arr.Length; ++j)
  {
  if (arr[j] %26lt; arr[min])
  min = j;
  }
  int t = arr[min];
  arr[min] = arr;
  arr = t;
  }
  }
  static void Main(string[] args)
  {
  int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
  SelectionSorter s = new SelectionSorter();
  s.Sort(array);
  foreach (int m in array)
  Console.WriteLine("{0}", m);
  }
  } 
  冒泡排序 




class EbullitionSorter
  {
  public void Sort(int[] arr)
  {
  int i, j, temp;
  bool done = false;
  j = 1;
  while ((j %26lt; arr.Length) %26amp;%26amp; (!done))//判断长度
  {
  done = true;
  for (i = 0; i %26lt; arr.Length - j; i++)
  {
  if (arr %26gt; arr[i + 1])
  {
  done = false;
  temp = arr;
  arr = arr[i + 1];//交换数据
  arr[i + 1] = temp;
  }
  }
  j++;
  }
  }
  static void Main(string[] args)
  {
  int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
  EbullitionSorter e = new EbullitionSorter ();
  e.Sort(array);
  foreach (int m in array)
  Console.WriteLine("{0}", m);
  }
  } 
  快速排序  




class QuickSorter
  {
  private void swap(ref int l, ref int r)
  {
  int temp;
  temp = l;
  l = r;
  r = temp;
  }
  public void Sort(int[] list, int low, int high)
  {
  int pivot;//存储分支点
  int l, r;
  int mid;
  if (high %26lt;= low)
  return;
  else if (high == low + 1)
  {
  if (list[low] %26gt; list[high])
  swap(ref list[low], ref list[high]);
  return;
  }
  mid = (low + high) %26gt;%26gt; 1;
  pivot = list[mid];
  swap(ref list[low], ref list[mid]);
  l = low + 1;
  r = high;
  do
  {
  while (l %26lt;= r %26amp;%26amp; list[l] %26lt; pivot)
  l++;
  while (list[r] %26gt;= pivot)
  r--;
  if (l %26lt; r)
  swap(ref list[l], ref list[r]);
  } while (l %26lt; r);
  list[low] = list[r];
  list[r] = pivot;
  if (low + 1 %26lt; r)
  Sort(list, low, r - 1);
  if (r + 1 %26lt; high)
  Sort(list, r + 1, high);
  }
  static void Main(string[] args)
  {
  int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
  QuickSorter q = new QuickSorter();
  q.Sort(iArrary, 0, 13);
  for (int m = 0; m %26lt;= 13; m++)
  Console.WriteLine("{0}", iArrary[m]);
  }
  } 
  插入排序 
   




public class InsertionSorter
  {
  public void Sort(int[] arr)
  {
  for (int i = 1; i %26lt; arr.Length; i++)
  {
  int t = arr;
  int j = i;
  while ((j %26gt; 0) %26amp;%26amp; (arr[j - 1] %26gt; t))
  {
  arr[j] = arr[j - 1];//交换顺序
  --j;
  }
  arr[j] = t;
  }
  }
  static void Main(string[] args)
  {
  int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
  InsertionSorter i = new InsertionSorter();
  i.Sort(array);
  foreach (int m in array)
  Console.WriteLine("{0}", m);
  }
  } 
  希尔排序 




public class ShellSorter
  {
  public void Sort(int[] arr)
  {
  int inc;
  for (inc = 1; inc %26lt;= arr.Length / 9; inc = 3 * inc + 1) ;
  for (; inc %26gt; 0; inc /= 3)
  {
  for (int i = inc + 1; i %26lt;= arr.Length; i += inc)
  {
  int t = arr[i - 1];
  int j = i;
  while ((j %26gt; inc) %26amp;%26amp; (arr[j - inc - 1] %26gt; t))
  {
  arr[j - 1] = arr[j - inc - 1];//交换数据
  j -= inc;
  }
  arr[j - 1] = t;
  }
  }
  }
  static void Main(string[] args)
  {
  int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
  ShellSorter s = new ShellSorter();
  s.Sort(array);
  foreach (int m in array)
  Console.WriteLine("{0}", m);
  }
  } 

 
         
       
                1

友情提示:此文并不表示本站肯定持有相同观点,转载请注明出处。
 您可能对 [C#] 的这些文章也感兴趣:

C#重点知识详解(一)  C#处理鼠标和键盘事件
得到系统进程和结束某个指定的进程  点对点通讯发送和接收
简易C#入门教程  VisualStudio.Net内幕(7)
C#,程序员的新工具  C#3.0 为我们带来什么(3) —— 初始化器
C#程序设计语言2.0简介  BeginnerwithC#6
[C#学习]在多线程中如何调用Winform  用C#创建COM对象
全面剖析VB.NET(4)  C#基础学习——异步编程篇
C# 3.0 Cookbook出版  在用c#开发的ActiveX中调用JavaScript方法
浅析C#中的套接字编程(5)  如何使用C#调用非托管DLL函数
用于统计函数执行时间的类  C#代码关闭Windows XP