C#排序算法大全

C#排序算法大全

  1. 一、冒泡排序(Bubble)   
  2.   
  3. using System;    
  4.   
  5. namespace BubbleSorter   
  6. {   
  7.  public class BubbleSorter   
  8.  {   
  9.   public void Sort(int[] list)   
  10.   {   
  11.    int i,j,temp;   
  12.    bool done=false;   
  13.    j=1;   
  14.    while((j<list.Length)&&(!done))   
  15.    {   
  16.     done=true;   
  17.     for(i=0;i<list.Length-j;i++)   
  18.     {   
  19.      if(list[i]>list[i+1])   
  20.      {   
  21.      done=false;   
  22.      temp=list[i];   
  23.      list[i]=list[i+1];   
  24.      list[i+1]=temp;   
  25.      }   
  26.     }   
  27.    j++;   
  28.    }   
  29.   }   
  30.  }   
  31.   
  32.  public class MainClass   
  33.  {    
  34.   public static void Main()   
  35.   {   
  36.    int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};   
  37.    BubbleSorter sh=new BubbleSorter();   
  38.    sh.Sort(iArrary);   
  39.    for(int m=0;m<iArrary.Length;m++)   
  40.    Console.Write("{0} ",iArrary[m]);    
  41.    Console.WriteLine();   
  42.   }   
  43.  }   
  44. }   
  45.   
  46. 二、选择排序(Selection)   
  47.   
  48. using System;   
  49.   
  50. namespace SelectionSorter   
  51. {   
  52.  public class SelectionSorter   
  53.  {    
  54.   private int min;   
  55.   public void Sort(int [] list)   
  56.   {   
  57.    for(int i=0;i<list.Length-1;i++)   
  58.    {   
  59.    min=i;   
  60.     for(int j=i+1;j<list.Length;j++)   
  61.     {   
  62.     if(list[j]<list[min])   
  63.     min=j;   
  64.     }   
  65.    int t=list[min];   
  66.    list[min]=list[i];   
  67.    list[i]=t;   
  68.    }   
  69.   }   
  70.  }   
  71.   
  72.  public class MainClass   
  73.  {    
  74.   public static void Main()   
  75.   {   
  76.    int[] iArrary = new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};   
  77.    SelectionSorter ss=new SelectionSorter();   
  78.    ss.Sort(iArrary);   
  79.    for (int m=0;m<iArrary.Length;m++)   
  80.    Console.Write("{0} ",iArrary[m]);    
  81.    Console.WriteLine();   
  82.   }   
  83.  }   
  84. }   
  85.   
  86. 三、插入排序(InsertionSorter)   
  87.   
  88. using System;   
  89.   
  90. namespace InsertionSorter   
  91. {   
  92.  public class InsertionSorter   
  93.  {   
  94.   public void Sort(int [] list)   
  95.   {   
  96.    for(int i=1;i<list.Length;i++)   
  97.    {   
  98.    int t=list[i];   
  99.    int j=i;   
  100.     while((j>0)&&(list[j-1]>t))   
  101.     {   
  102.     list[j]=list[j-1];   
  103.     --j;   
  104.     }   
  105.    list[j]=t;   
  106.    }   
  107.   }   
  108.  }   
  109.   
  110.  public class MainClass   
  111.  {    
  112.   public static void Main()   
  113.   {   
  114.    int[] iArrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};   
  115.    InsertionSorter ii=new InsertionSorter();   
  116.    ii.Sort(iArrary);   
  117.    for(int m=0;m<iArrary.Length;m++)   
  118.    Console.Write("{0}",iArrary[m]);    
  119.    Console.WriteLine();   
  120.   }   
  121.  }   
  122. }   
  123.   
  124. 四、希尔排序(ShellSorter)   
  125.   
  126. using System;   
  127.   
  128. namespace ShellSorter   
  129. {   
  130.  public class ShellSorter   
  131.  {   
  132.   public void Sort(int [] list)   
  133.   {   
  134.   int inc;   
  135.   for(inc=1;inc<=list.Length/9;inc=3*inc+1);   
  136.    for(;inc>0;inc/=3)   
  137.    {   
  138.     for(int i=inc+1;i<=list.Length;i+=inc)   
  139.     {   
  140.     int t=list[i-1];   
  141.     int j=i;   
  142.      while((j>inc)&&(list[j-inc-1]>t))   
  143.      {   
  144.      list[j-1]=list[j-inc-1];   
  145.      j-=inc;   
  146.      }   
  147.     list[j-1]=t;   
  148.     }   
  149.    }   
  150.   }   
  151.  }   
  152.   
  153.  public class MainClass   
  154.  {    
  155.   public static void Main()   
  156.   {   
  157.    int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};   
  158.    ShellSorter sh=new ShellSorter();   
  159.    sh.Sort(iArrary);   
  160.    for(int m=0;m<iArrary.Length;m++)   
  161.    Console.Write("{0} ",iArrary[m]);   
  162.    Console.WriteLine();   
  163.   }   
  164.  }   
  165. }     
  166.    
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值