欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 数组冒泡排序复习

程序员文章站 2022-06-13 17:33:29
using System; namespace runoob { class MyClass { static void Main(string[] args) { MyClass1 myClass = new MyClass1(); myClass.Dialog(); } } class MyCl ......
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);
            }
        }
    }
}

C# 数组冒泡排序复习