C# 演算法之 冒泡排序

來源:互聯網
上載者:User
static void Main(string[] args)
        {

            int[] arr ={2,5,8,9,1,57,8,5877,5,58,56,3,557,7};

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

                           
            }

            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i].ToString());
            
            }

            Console.ReadKey();

        }

 

 

 

現在來講一下程式運行過程:

2,5,8,1,9,8,57,5,58,56,3,557,7,5877,|  i的第一次迴圈
2,5,1,8,8,9,5,57,56,3,58,7,557,5877,|  依次
2,1,5,8,8,5,9,56,3,57,7,58,557,5877,|
1,2,5,8,5,8,9,3,56,7,57,58,557,5877,|
1,2,5,5,8,8,3,9,7,56,57,58,557,5877,|
1,2,5,5,8,3,8,7,9,56,57,58,557,5877,|
1,2,5,5,3,8,7,8,9,56,57,58,557,5877,|
1,2,5,3,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|

第一次迴圈的時候:首先arr[0]跟arr[1]比較 ,如果arr[0]大於arr[1]那麼原來 arr[0]的值和arr[1]的值互換,再次內迴圈

arr[1]此時值不變=5 ,arr[2]=8 這兩個比較 arr[1]<arr[8]那麼不變動, 然後再比較arr[2]=8和arr[3]=1 ,arr[2]>arr[3],值互換,在迴圈 arr[3]此時值為=8,arr[4]=9, 一次比較

因此第一次迴圈之後 最後一值肯定是最大的那個值

1,2,5,8,5,8,9,3,56,7,57,58,557,5877,|
此時最大值已經得到,因此下次迴圈只用比較最大值之前那幾位元字!

所以

 for (int j = 0; j < arr.Length - i-1; j++)

需要這樣比較!i=0的時候 需要全部比較

i=1的時候只要比較13個數字就可以了(因為最後一個已經是最大的那個值了)

這樣就是把最大的那個依次往後排(就像泡泡一樣一個一個浮出來)

完整代碼:

 

 

sing System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

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

            int[] arr ={2,5,8,9,1,57,8,5877,5,58,56,3,557,7};

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

                //注意這裡只是用來 將每次迴圈的結果顯示出來
                  //正式裡面不用寫這個
                for (int l = 0; l < arr.Length; l++)
                {
                    Console.Write(arr[l].ToString() + ",");
                    

                  
                }
                Console.WriteLine("|");

                Console.ReadKey();
                //到這裡
            }

            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i].ToString());
            
            }

            Console.ReadKey();

        }

    }
}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.