C# 实现排序--冒泡--选择--插入

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApplication1
  5. {
  6.   class Program
  7.   {
  8.     static void Main(string[] args)
  9.     {
  10.       int[] iarrary = new int[] {1,5,13,6,10,55,99,2,87,12,34,75,33,47};
  11.          //mpsort(iarrary);
  12.          // xzsort(iarrary);
  13.       crsort(iarrary);
  14.       for (int n = 0; n < iarrary.Length; n++)
  15.       {
  16.         Console.WriteLine("{0}",iarrary[n]);
  17.       }
  18.       Console.Read();
  19.     }
  20.     public static void mpsort(int [] list)//冒泡排序
  21.     {
  22.       int i, j, temp;
  23.       bool exchange;
  24.       for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序
  25.       {
  26.         exchange = false;
  27.         for (j = 0; j < list.Length-i;j++ )
  28.         {
  29.           if(list[j]>list[j+1])
  30.           {
  31.             temp = list[j];
  32.             list[j] = list[j + 1];
  33.             list[j + 1] = temp;
  34.             exchange = true;
  35.           }
  36.         }
  37.         if (!exchange)
  38.         {
  39.           break;
  40.         }
  41.       }
  42.     }
  43.     public static void xzsort(int[] list)//选择排序
  44.     {
  45.       int i,j,temp,min;
  46.       for (i = 0; i < list.Length; i++)
  47.       {
  48.         min = i;
  49.         for (j = i + 1; j < list.Length;j++ )
  50.         {
  51.           if (list[min] > list[j])
  52.           {
  53.             min = j;
  54.           }
  55.         }
  56.         temp = list[min];
  57.         list[min] = list[i];
  58.         list[i] = temp;
  59.       }
  60.     }
  61.     public static void crsort(int[] list)//插入排序
  62.     {
  63.       int i,j,temp;
  64.       for (i = 1; i < list.Length; i++)
  65.       {
  66.         temp = list[i];
  67.         j = i;
  68.         while((j > 0)&&(list[j-1]>temp))
  69.         {
  70.           list[j]=list[j-1];
  71.           --j;
  72.         }
  73.         list[j]=temp;
  74.       }
  75.     }
  76.   }
  77. }
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值