using System;
using System.Collections.Generic;
using System.Text;
namespace ArrayMax
{
class Program
{
//产生一个包含不重复数字的随机数字的数组
private int[] GetArray(int a)
{
//定义一个数组,接收随机产生的数字
int [] array = new int [a];
//声明Random类,随机产生1,101之间的数字
Random rnd = new Random();
//X用来接收随机产生的数字
int x = 0;
//Bool 用来表示是否有相同的数字
for (int i = 0; i < a;i ++ )
{
//FOR循环产生数组里的元素
label:
x = rnd.Next(1,a+1);
for (int j = 0; j < i;j ++ )
{
//for循环验证X是否合法
if (x ==array [j])
{
//如果数组中有该数字,则跳转到label处,重新产生数字,并验证
goto label;
}
}
//如果能走到这里,说明数组中还没有这个数字,
array[i] = x;
}
return array;
}
//对数组进行排序
private void Sort(int [] array)
{
int len = array.Length;
if (len < 1)
return;
for (int i = 0; i < len;i ++ )
{
for (int j = i; j < len ;j ++ )
{
if (array [i] < array [j])
{
array [i] = array[i] + array[j];
array [j] = array[i] - array[j];
array [i] = array[i] - array[j];
}
}
}
}
static void Main(string[] args)
{
int x = 0;
if (args.Length > 0)
{
x = Convert.ToInt32(args[1]);
}
else
{
x = 100;
}
Program p = new Program();
int[] array = p.GetArray(x);
int[] Array = new int[x];
array.CopyTo(Array,0 );
p.Sort(Array);
Console .WriteLine("排序前");
for (int i = 0;i < array .Length ;i ++)
{
Console .WriteLine ("array[" +i +"]=" +array [i]);
}
Console .WriteLine ("排序后");
for (int i = 0; i < Array.Length;i ++ )
{
Console.WriteLine("Array[" + i +"]=" +Array [i] );
}
Console.ReadLine();
}
}
}
浙公网安备 33010602011771号