VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • 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);
            }
        }
    }
}
复制代码


相关教程