欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

C# 数组冒泡排序复习

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);
            }
        }
    }
}

posted @ 2020-01-10 11:27  萧静默  阅读(476)  评论(0编辑  收藏  举报