本文实例分析了c#数组反转与排序的方法。分享给大家供大家参考。具体实现方法如下:
c#数组反转
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace 数据反转
{
class program
{
static void main(string[] args)
{
string[] strallay = { "毛泽东", "李世民", "秦始皇", "成吉思汗", "习近平","邓小平"};
string s;
for (int i = 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 (string ss in strallay)
{
console.write(ss+" " );
}
console.readkey();
}
}
}
c#数组排序:
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace 数组
{
class program
{
static void main(string[] args)
{
//输出一个数组里的最大的数值;
/*
int[] arr = new int[] { 10, 9, 15, 6, 24, 3, 0, 7, 19, 1 };
int max = 0;
for (int i = 0; i < arr.length - 1; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
console.writeline(max);
**/
//按大小顺序输出数组的值
int[] list = new int[] { 10, 9, 15, 6, 24, 3, 0, 7, 19, 1 ,100,25,38};
/*
for (int i = 0; i < list.length-1; i++)
{
for (int j = i+1; j < list.length; j++)
{
if (list[i] > list[j])
{
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}*/
/// <summary>
/// 插入排序法
/// </summary>
/// <param name="list"></param>
for (int i = 1; i < list.length; i++)
{
int t = list[i];
int j = i;
while ((j > 0) && (list[j - 1] > t))
{
list[j] = list[j - 1];
--j;
}
list[j] = t;
}
foreach (int forstr in list)
{
console.write(forstr + " ");
}
console.readkey();
}
}
}
希望本文所述对大家的c#程序设计有所帮助。
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
C#实现猜数字游戏
本文实例为大家分享了c#实现猜数字游戏具体代码,供大家参考,具体内容如下给定一个0-100的随机数字猜其大小题目样式:电脑产生一个0到100之间的随机数字,并且...
[阅读全文]
-
-
C# 可空类型的具体使用
在项目中我们经常会遇到可为空类型,那么到底什么是可为空类型呢?下面我们将从4个方面为大家剖析。1、可空类型基础知识顾名思义,可空类型指的就是某个对象类型可以为空...
[阅读全文]
-
-
-
C#实现猜数字小游戏
本文实例为大家分享了c#实现猜数字小游戏的具体代码,供大家参考,具体内容如下效果如图:代码:using system;using system.collecti...
[阅读全文]
-
-
C#实现简单俄罗斯方块
最近在看《.net游戏编程入门经典 c#篇》 第一章介绍了如何制作俄罗斯方块,自己试了试按照书上的步骤,可算是完成了。于是写下这篇文章留作纪念。1.类的设计在充...
[阅读全文]
-
-
网友评论