c#冒泡排序

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace c4._3  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             int[] num = new int[10];  
  13.             Program p=new Program();  
  14.             Console.WriteLine("原始数组:");  
  15.             p.randIt(num);  
  16.             p.show(num);  
  17.             p.sort(num);  
  18.             Console.WriteLine();  
  19.             Console.WriteLine("降序数组");  
  20.             p.show(num);  
  21.             Console.ReadKey();  
  22.   
  23.   
  24.         }  
  25.         public void randIt(int[] num)  
  26.         {  
  27.             Random randNum = new Random();  
  28.             for (int i = 0; i < num.Length; i++)  
  29.             {  
  30.                   
  31.                 num[i]= randNum.Next(101);//101??  
  32.             }  
  33.         }  
[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. //这是冒泡的算法  
  2.         public void sort(int[] arr)  
  3.         {   
  4.             for(int i=0;i<arr.Length;i++)  
  5.                 for (int j = 0; j < arr.Length-i-1; j++)//看清楚j的终止条件  
  6.                 {  
  7.                     if (arr[j]< arr[j+1])  
  8.                     {  
  9.                         int temp = arr[j];  
  10.                         arr[j] = arr[j + 1];  
  11.                         arr[j +1] = temp;  
  12.                     }  
  13.                 }  
  14.           
  15.         }  
  16.         public void show(int[] arr)  
  17.         {  
  18.             foreach (int i in arr)  
  19.             {  
  20.                 Console.Write("{0} ", i);  
  21.             }  
  22.           
  23.         }  
  24.     }  
  25. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值