1. 程式人生 > >C#實現氣泡排序

C#實現氣泡排序

* 問題描述:

設計一個程式,輸入10個數存入陣列中,然後實現氣泡排序。

程式碼如下:

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

namespace bubbling
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("將10個數存入陣列中,然後實現氣泡排序");

            double
[] c = new double[10] { 2, 3, 5, 7, 12, 27, 77, 23, 17, 99 }; int i, j; double m; for (i = 0; i < 10; ++i) for (j = 0; j < 10 - i - 1; ++j) { if (c[j] > c[j + 1]) { m = c[j]; c[j] = c[j + 1
]; c[j + 1] = m; } } Console.Write("10個數氣泡排序後(按從小到大的順序)為:"); foreach (double var in c) { Console.Write(var); //依次讀取字串中的元素 Console.Write(" "); } Console.ReadKey(); } } }

執行結果如下:
這裡寫圖片描述