选择排序(C#)

本文介绍了一个简单的选择排序算法实现过程,通过定义一个 SelectionSorter 类并实现 Sort 方法来对整型数组进行排序。示例代码展示了如何使用该类对一个包含多个元素的数组进行排序,并输出排序后的结果。
None.gifsing System;
None.gif
public class SelectionSorter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
// public enum comp {COMP_LESS,COMP_EQUAL,COMP_GRTR};
InBlock.gif
  private int min;
InBlock.gif  
// private int m=0;
InBlock.gif
   public void Sort(int [] list)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif       
for(int i=0;i<list.Length-1;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif          min
=i;
InBlock.gif          
for(int j=i+1;j<list.Length;++j)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            
if(list[j]<list[min])
InBlock.gif            min
=j;
ExpandedSubBlockEnd.gif          }

InBlock.gif        
int t=list[min];
InBlock.gif        list[min]
=list[i];
InBlock.gif        list[i]
=t;
InBlock.gif     
//   Console.WriteLine("{0}",list[i]);
ExpandedSubBlockEnd.gif
     }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
public class MainClass
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif     
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
int[] iArrary=new int[]dot.gif{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
InBlock.gif     SelectionSorter ss
=new SelectionSorter();
InBlock.gif     ss.Sort(iArrary);
InBlock.gif     
for(int m=0;m<=13;m++)
InBlock.gif     Console.WriteLine(
"{0}",iArrary[m]);  
InBlock.gif 
ExpandedSubBlockEnd.gif     }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/weipengyang/archive/2006/03/01/340284.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值