c# 使用“冒泡”法对数组元素进行排序结果如图所示

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApp3__冒泡_法排序_数组_
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] a = new double[10];
            Console.WriteLine("请输入10个数:");
            for(int i = 0; i < 10; i++)
            {
                Console.Write("第{0}个数:",i+1);
                a[i] = double.Parse(Console.ReadLine());
            }
            for(int i = 0; i < 10; i++)
            {
                Console.Write("{0}\t", a[i]);
            }
            Console.WriteLine();
            double temp;
            for(int i = 0; i < 9; i++)
            {
                for(int j = 0; j < 9-i ; j++)
                {
                    if (a[j] > a[j+1])
                    {
                        temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
            Console.WriteLine("排好序为:");
            for(int j = 0; j < 10; j++)
            {
                Console.Write("{0}\t",a[j]);
            }
        }
    }
}

相关文章: