c#选择排序

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace c4._4  
  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.         public void randIt(int[] num)  
  24.         {  
  25.             Random randNum = new Random();  
  26.             for (int i = 0; i < num.Length; i++)  
  27.             {  
  28.   
  29.                 num[i] = randNum.Next(101);//101??    
  30.             }  
  31.         }  
  32.         //这是选择排序的算法    
  33.         public void sort(int[] arr)  
  34.         {  
  35.             for (int i = 0; i < arr.Length; i++)  
  36.                 for (int j = i; j < arr.Length; j++)//看清楚j的终止条件    
  37.                 {  //选择跟冒泡有点不同,是先排第一个,然后往后排  
  38.                     if (arr[i] < arr[j])  
  39.                     {  
  40.                         int temp = arr[j];  
  41.                         arr[j] = arr[i];  
  42.                         arr[i] = temp;  
  43.                     }  
  44.                 }  
  45.   
  46.         }  
  47.         public void show(int[] arr)  
  48.         {  
  49.             foreach (int i in arr)  
  50.             {  
  51.                 Console.Write("{0} ", i);  
  52.             }  
  53.   
  54.         }  
  55.     }  
  56. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值