C# 陣列氣泡排序複習
阿新 • • 發佈:2020-12-21
using System;
namespace runoob
{
class MyClass
{
static void Main(string[] args)
{
MyClass1 myClass = new MyClass1();
myClass.Dialog();
}
}
class MyClass1
{
public void Dialog()
{
Console. WriteLine("請輸入要產生的隨機數的個數");
int num = Convert.ToInt32(Console.ReadLine());
int[] array = new int[num];
Random random = new Random();
for (int i = 0; i <= num - 1; i++)
{
array[i] = random.Next(0, 100);
}
Console.WriteLine("您輸入的數是:");
foreach (var arr in array)
{
Console.Write(arr + " ");
}
for (int i = 0; i < array.Length; i++)
{
for (int j = i + 1; j < array.Length; j++ )
{
if (array[i] < array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Console.WriteLine();
Console.WriteLine("您輸入的數從大到小排序是:");
foreach (var item in array)
{
Console.Write("{0}\t", item);
}
for (int i = 0; i < array.Length; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Console.WriteLine();
Console.WriteLine("您輸入的數從小到大排序是:");
foreach (var item1 in array)
{
Console.Write("{0}\t", item1);
}
}
}
}