冒泡排序 c# 102

202 阅读1分钟
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
    class Program
    {

        static void Main(string[] args)
        {

            // 冒泡排序数据源
            int[] num_list = { 2, 1, 5, 4, 8, 3 };

            // 轮循环,总共要进行的轮数
            for (int i = 0; i < num_list.Length-1; i++) {
                int lun_num = i+1;
            
                // 每一轮,下标可以游走的范围
                int max_idx = num_list.Length - i - 2;

                Console.WriteLine("当前索引{0},是第{1}轮,最大游走下标{2}", i, lun_num,max_idx);

                // 让下标遍历游戏执行功能
                for (int j = 0; j <= max_idx; j++) { 
                // 当前的与后一个比较
                    if (num_list[j] > num_list[j + 1]) {

                        int temp = num_list[j];
                        num_list[j] = num_list[j + 1];
                        num_list[j + 1] = temp;
                    }
                }

            
            
            }

            // 最终结果
            foreach (int t in num_list) {
                Console.WriteLine(t);
            }

            Console.ReadKey();
        }
    
    }
}