C#冒泡排序

来源:互联网 发布:刻章软件 编辑:程序博客网 时间:2024/05/16 19:21

using System;

namespace Sort
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class BubbleSort
 {
  
  [STAThread]

  public void Bubble_Sort(int []array)
  {
   int temp,j,i;
   for(i=0;i<array.Length;i++)
   {
    for(j=0;j<array.Length-i-1;j++)
    {
     if(array[j]<array[j+1])
     {
      temp=array[j];
      array[j]=array[j+1];
      array[j+1]=temp;
      
     }
     
    
    }
       Console.Write(" "+array[j]);

    
   }
  }
  static void Main(string[] args)
  {
   int [] array=new int[11]{2,5,3,4,8,1,78,23,21,89,32};
   Console.WriteLine("输出原来的数组元素");
   for(int count=0;count<array.Length;count++)
   {
    Console.Write(" "+array[count]);
    
    
   }
   Console.WriteLine();
   Console.WriteLine("输出经过排序以后的数组元素");

    BubbleSort obj=new BubbleSort();
   obj.Bubble_Sort(array);
   Console.WriteLine();
   
  }
 }
}

原创粉丝点击