zoukankan      html  css  js  c++  java
  • 选择排序和冒泡排序

    static void Main(string[] args)
            {
                int[] arr = new int[] { 11,1, 9, 4, 6, 8, 6, 11, 30 };
     
                printMM(arr);
                SelectSort(arr);
                printMM(arr);
     
                int[] arr1 = new int[] { 11, 1, 9, 4, 6, 8, 6, 11, 30 };
     
                printMM(arr1);
                BoubleSort(arr1);
                printMM(arr1);
            }
     
            static void printMM(int[] inputarr)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < inputarr.Length; i++)
                {
                    sb.Append(inputarr[i]).Append(",");
                }
                Console.WriteLine(sb.ToString());
            }
     
            /// <summary>
            /// 选择排序
            /// </summary>
            /// <param name="inputArr"></param>
            static void SelectSort(int[] inputArr)
            {
                for (int i = 0; i < inputArr.Length-2; i++)
                {
                    for (int j = i+1; j <= inputArr.Length-1; j++)
                    {
                        if(inputArr[i]>inputArr[j])
                        {
                            int temp = inputArr[i];
                            inputArr[i] = inputArr[j];
                            inputArr[j] = temp;
                        }
                    }
                }
            }
     
            /// <summary>
            /// 冒泡排序
            /// </summary>
            /// <param name="inputArr"></param>
            static void BoubleSort(int[] inputArr)
            {
                for (int i = 0; i < inputArr.Length - 1; i++)
                {
                    bool isExchange = false;
                    for (int j = inputArr.Length-2; j >=i; j--)
                    {
                        if (inputArr[j] > inputArr[j+1])
                        {
                            int temp = inputArr[j+1];
                            inputArr[j+1] = inputArr[j];
                            inputArr[j] = temp;
     
                            isExchange = true;
                        }
                    }
                    if (!isExchange)
                        break;
                }
            }
  • 相关阅读:
    捷微商城小程序上线啦~
    JEECG 新版在线文档WIKI正式发布
    https 详解
    css 3 新特性
    js 基础(一)
    BFC
    .Net、C# 汉字转拼音,简体繁体转换方法
    丰富“WinForms” 的一个别样"项目"(学生管理)
    学生管理系统1
    学生管理系统
  • 原文地址:https://www.cnblogs.com/zhxm/p/3844772.html
Copyright © 2011-2022 走看看