您的位置:首页 > 编程语言 > C#

C#实现的3种排序算法--冒泡排序、选择排序、插入排序

2008-03-12 12:36 846 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      int[] iarrary = new int[] {1,5,13,6,10,55,99,2,87,12,34,75,33,47};
      //mpsort(iarrary);
      // xzsort(iarrary);
      crsort(iarrary);
      for (int n = 0; n < iarrary.Length; n++)
      {
        Console.WriteLine("{0}",iarrary
);
      }
      Console.Read();
    }

    public static void mpsort(int [] list)//冒泡排序
    {
      int i, j, temp;
      bool exchange;
      for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序
      {
        exchange = false;
        for (j = 0; j < list.Length-i;j++ )
        {
          if(list[j]>list[j+1])
          {
            temp = list[j];
            list[j] = list[j + 1];
            list[j + 1] = temp;
            exchange = true;
          }
        }
        if (!exchange)
        {
          break;
        }
      }
    }

    public static void xzsort(int[] list)//选择排序
    {
      int i,j,temp,min;
      for (i = 0; i < list.Length; i++)
      {
        min = i;
        for (j = i + 1; j < list.Length;j++ )
        {
          if (list[min] > list[j])
          {
            min = j;
          }
        }
        temp = list[min];
        list[min] = list[i];
        list[i] = temp;
      }
    }

    public static void crsort(int[] list)//插入排序
    {
      int i,j,temp;
      for (i = 1; i < list.Length; i++)
      {
        temp = list[i];
        j = i;
        while((j > 0)&&(list[j-1]>temp))
        {
          list[j]=list[j-1];
          --j;
        }
        list[j]=temp;
      }
    }
  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐