使用冒泡倒序排序

        ///


        /// 使用冒泡倒序排序
        ///

        ///
        static void Main(string[] args)
        {
            int[] num = new int[8];
            Console.WriteLine("请输入你购买的商品的价格:");

            //读入商品价格
            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine("第{0}件商品:",i+1);
                num[i] = int.Parse(Console.ReadLine());
            }

            //开始排序——使用冒泡排序
            for (int i = 0; i < num.Length-1; i++)
            {
                for (int j = 0; j < num.Length-1-i; j++)
                {
                    if (num[j] > num[j + 1])
                    {
                        int temp = num[j+1];
                        num[j + 1] = num[j];
                        num[j] = temp;
                    }
                }
            }

            //排列后输出
            Console.WriteLine("价格从高到低排序后为:");
            for (int i = 0; i < num.Length; i++)
            {
                Console.WriteLine("{0}\t",num[i]);
            }


            //冒泡排序速记口诀(升序):
            //N个数字来排队,两两相比小靠前,
            //外层循环N-1,内层循环N-1-i。
            //若要降序排序,只需把程序中的大于号换成小于号就行了。           
            
        }

你可能感兴趣的:(Public,method)