64°

冒泡排序算法

using System;
using System.Threading.Tasks;

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

        </span><span style="color: #0000ff;">int</span>[] arr = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span>[<span style="color: #800080;">10</span><span style="color: #000000;">];
        </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=<span style="color: #800080;">0</span>;i&lt;=<span style="color: #800080;">9</span>;i++<span style="color: #000000;">){
            arr[i] </span>= <span style="color: #0000ff;">new</span> Random(i+DateTime.Now.Millisecond).Next(<span style="color: #800080;">100</span>,<span style="color: #800080;">999</span><span style="color: #000000;">);
        }
        
        Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">排序之前的数组为 :********************************</span><span style="color: #800000;">"</span><span style="color: #000000;">);
        Show(arr);
        Console.WriteLine();
        Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">排序中的数组为 :********************************</span><span style="color: #800000;">"</span><span style="color: #000000;">);
        
        BubbleSort1(arr);
        Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">排序之后的数组为 :********************************</span><span style="color: #800000;">"</span><span style="color: #000000;">);
        Show(arr);
        Console.Read();
    }
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">5,1,8,4,6</span>
    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> BubbleSort1(<span style="color: #0000ff;">int</span><span style="color: #000000;">[] arr){
        </span><span style="color: #0000ff;">int</span> temp=<span style="color: #800080;">0</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> outer=arr.Length; outer&gt;=<span style="color: #800080;">1</span>;outer--<span style="color: #000000;">){
            </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> inner =<span style="color: #800080;">0</span>;inner &lt;outer-<span style="color: #800080;">1</span>;inner++<span style="color: #000000;">){
                </span><span style="color: #0000ff;">if</span>(arr[inner] &gt; arr[inner+<span style="color: #800080;">1</span><span style="color: #000000;">]){
                    temp </span>= arr[inner+<span style="color: #800080;">1</span><span style="color: #000000;">];
                    arr[inner</span>+<span style="color: #800080;">1</span>] =<span style="color: #000000;"> arr[inner];
                    arr[inner]</span>=<span style="color: #000000;">temp;
                }
            }
            Show(arr);
            Console.WriteLine();
        }
    }
    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Show(<span style="color: #0000ff;">int</span><span style="color: #000000;">[] arr){
        </span><span style="color: #0000ff;">foreach</span>(<span style="color: #0000ff;">var</span> item <span style="color: #0000ff;">in</span><span style="color: #000000;"> arr){
            Console.Write(item</span>+<span style="color: #800000;">"</span>  <span style="color: #800000;">"</span><span style="color: #000000;">);
        }
    }
}

}

排序之前的数组为 :********************************
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

本文转载自博客园,原文链接:https://www.cnblogs.com/morec/p/13191722.html

全部评论: 0

    我有话说: