zoukankan      html  css  js  c++  java
  • 冒泡排序算法

    经典冒泡排序算法
     
    using System;
    using System.Threading.Tasks;
    
    namespace SingletonDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
    
    
                int[] arr = new int[10];
                for(int i=0;i<=9;i++){
                    arr[i] = new Random(i+DateTime.Now.Millisecond).Next(100,999);
                }
                
                Console.WriteLine("排序之前的数组为 :********************************");
                Show(arr);
                Console.WriteLine();
                Console.WriteLine("排序中的数组为 :********************************");
                
                BubbleSort1(arr);
                Console.WriteLine("排序之后的数组为 :********************************");
                Show(arr);
                Console.Read();
            }
            
            
            static void BubbleSort1(int[] arr){
                int temp=0;
                for(int outer=arr.Length; outer>=1;outer--){
                    for(int inner =0;inner <outer-1;inner++){
                        if(arr[inner] > arr[inner+1]){
                            temp = arr[inner+1];
                            arr[inner+1] = arr[inner];
                            arr[inner]=temp;
                        }
                    }
                    Show(arr);
                    Console.WriteLine();
                }
            }
            static void Show(int[] arr){
                foreach(var item in arr){
                    Console.Write(item+"  ");
                }
            }
        }
    }
    排序之前的数组为 :********************************
    977 588 159 628 199 669 239 709 280 749
    排序中的数组为 :********************************
    588 159 628 199 669 239 709 280 749 977
    159 588 199 628 239 669 280 709 749 977
    159 199 588 239 628 280 669 709 749 977
    159 199 239 588 280 628 669 709 749 977
    159 199 239 280 588 628 669 709 749 977
    159 199 239 280 588 628 669 709 749 977
    159 199 239 280 588 628 669 709 749 977
    159 199 239 280 588 628 669 709 749 977
    159 199 239 280 588 628 669 709 749 977
    159 199 239 280 588 628 669 709 749 977
    排序之后的数组为 :********************************
    159 199 239 280 588 628 669 709 749 977
  • 相关阅读:
    leetcode231
    leetcode326
    leetcode202
    leetcode121
    leetcode405
    leetcode415
    2019-9-2-win10-uwp-应用转后台清理内存
    2019-9-2-win10-uwp-应用转后台清理内存
    ACM学习心得
    ACM学习心得
  • 原文地址:https://www.cnblogs.com/morec/p/13191722.html
Copyright © 2011-2022 走看看