C#排序算法 之 冒泡排序

本文介绍了一种使用C#实现的冒泡排序算法。通过一个具体的示例展示了如何定义一个冒泡排序类,并在其内部实现排序逻辑。该算法能够对整数数组进行升序排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. using System;   
  2.   
  3. namespace BubbleSorter    
  4. {    
  5.     public class BubbleSorter    
  6.     {    
  7.         public void Sort(int [] list)    
  8.         {    
  9.             int i,j,temp;    
  10.             bool done=false;    
  11.             j=1;    
  12.             while((j<list.Length)&&(!done))    
  13.             {    
  14.                 done=true;    
  15.                 for(i=0;i<list.Length-j;i++)    
  16.                 {    
  17.                     if(list[i]>list[i+1])    
  18.                     {    
  19.                         done=false;    
  20.                         temp=list[i];    
  21.                         list[i]=list[i+1];    
  22.                         list[i+1]=temp;    
  23.                     }    
  24.                 }    
  25.                 j++;    
  26.             }    
  27.         }    
  28.     }    
  29.   
  30.     public class MainClass    
  31.     {    
  32.         public static void Main()    
  33.         {    
  34.             int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};    
  35.             BubbleSorter sh=new BubbleSorter();    
  36.             sh.Sort(iArrary);    
  37.             forint m=0;m<iArrary.Length;m++)    
  38.             {   
  39.                 Console.Write("{0} ",iArrary[m]);    
  40.                 Console.WriteLine();    
  41.             }   
  42.         }    
  43.     }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值