创建控制台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Consolepaixu
{
class Program
{
private const int Num = 30;
private const int MaxValue = 30;
private const int MinValue = 1;
static void Main(string[] args)
{
int[] a = GenerateNumber3();
int[] b = BubbleSort(a);
for (int i = 0; i < b.Length; i++)
{
Console.Write(b[i].ToString() + " ");
}
Console.ReadLine();
}
public static int[] BubbleSort(int[] list)
{
int i, temp;
for (int j = 0; j < list.Length; j++)
{
for (i = list.Length - 1; i > j; i--)
{
if (list[j] < list[i])
{
temp = list[j];
list[j] = list[i];
list[i] = temp;
}
}
}
return list;
}
public static int[] GenerateNumber3()
{
//用于存放1到30这30个数
int[] container = new int[30];
//用于保存返回结果
int[] result = new int[30];
Random random = new Random();
for (int i =1; i <= 30; i++)
{
container[i-1] = i;
}
int index;
int value;
for (int i = 0; i <30; i++)
{
//从[1,container.Count + 1)中取一个随机值,保证这个值不会超过container的元素个数
index = random.Next(0, container.Length - i-1);
//以随机生成的值作为索引取container中的值
value = container[index];
//将随机取得的值放到结果集合中
result[i] = value;
//将刚刚使用到的数从容器集合中移到末尾去
container[index] = container[container.Length - i-1];
//将队列对应的值移到队列中
container[container.Length - i-1] = value;
}
return result;
}
}
}