算法排序之冒泡排序

首先看冒泡排序原理:

A B C D(A和B比较完 B和C比较 C和D比较 条件满足就替换)

看完原理就上代码:

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

namespace suanfatest
{
    class Program
    {
        static void Main(string[] args)
        {

            int temp = 0;
            int[] arr = { 23, 44, 56, 86, 108, 11, 3, 6, 7 };

            Console.WriteLine("排序前的數組是:");
            
            foreach(int item in arr)
            {
                Console.Write(item + ", ");
            }

            for (int i = 0; i < arr.Length-1; i++)
            {
                for (int j = 0; j < arr.Length -1; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        temp = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
            Console.WriteLine("");

            Console.WriteLine("排序後的數組是:");

            foreach (int item in arr)
            {
                Console.Write(item + ", ");
            }

            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

代码分析:

下一个学习插入排序..........

原文地址:https://www.cnblogs.com/yzenet/p/5016467.html