C#数组反转与排序实例分析
本文实例分析了C#数组反转与排序的方法。分享给大家供大家参考。具体实现方法如下:
C#数组反转
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace数据反转
{
classProgram
{
staticvoidMain(string[]args)
{
string[]strAllay={"毛泽东","李世民","秦始皇","成吉思汗","习近平","邓小平"};
strings;
for(inti=0;i<strAllay.Length/2;i++)//strAllay.Length/2是因为经过(将数组的长度值除以2)次就可以将数组成员进行反转了
{
s=strAllay[i];
strAllay[i]=strAllay[strAllay.Length-1-i];//如果i等于数组第一项值(毛泽东)的时候,将它与最后一个值(邓小平)互换。
strAllay[strAllay.Length-1-i]=s;
}
foreach(stringssinstrAllay)
{
Console.Write(ss+"");
}
Console.ReadKey();
}
}
}C#数组排序:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace数组
{
classProgram
{
staticvoidMain(string[]args)
{
//输出一个数组里的最大的数值;
/*
int[]arr=newint[]{10,9,15,6,24,3,0,7,19,1};
intmax=0;
for(inti=0;i<arr.Length-1;i++)
{
if(arr[i]>max)
{
max=arr[i];
}
}
Console.WriteLine(max);
**/
//按大小顺序输出数组的值
int[]list=newint[]{10,9,15,6,24,3,0,7,19,1,100,25,38};
/*
for(inti=0;i<list.Length-1;i++)
{
for(intj=i+1;j<list.Length;j++)
{
if(list[i]>list[j])
{
inttemp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}*/
///<summary>
///插入排序法
///</summary>
///<paramname="list"></param>
for(inti=1;i<list.Length;i++)
{
intt=list[i];
intj=i;
while((j>0)&&(list[j-1]>t))
{
list[j]=list[j-1];
--j;
}
list[j]=t;
}
foreach(intforStrinlist)
{
Console.Write(forStr+"");
}
Console.ReadKey();
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
热门推荐
10 五一公司放假祝福语简短
11 追星女孩的祝福语简短
12 给长辈端午祝福语 简短
13 生日祝福语有诗意简短
14 团圆日祝福语简短
15 中秋商务祝福语简短最新
16 温暖文艺简短祝福语短句
17 男孩上学祝福语简短的
18 入住酒店文案祝福语简短