[c#演算法和資料結構]快速排序演算法

來源:互聯網
上載者:User

問題描述: 

        快速排序演算法

     //******************************    

     //描述:快速排序演算法 

     //類名:QuickSort

     //作者:洪曉軍

     //時間:2004-11-2 

     //******************************  

     public class QuickSort

    {

        public void quickSort(int[ ] a,int left, int right)     //left為數組a第一個元素位置,right為數組a最後一個元素位置

        {

            int i, j, pivot, temp;     //pivot為支點

            if (left >= right)

                return;

            i = left;

            j = right + 1;

            pivot = a[left];

            while (true)

            {

                do

                {

                    i++;

                } while (a[i] < pivot && i < right);     //從左向右尋找大於支點元素 

                do

                {

                    j--;

                } while (a[j] > pivot && j > 0);     //從右向左尋找小於支點元素

                if (i >= j)

                    break;

                else     //滿足i<j則交換i和j位置元素值

                {

                    temp = a[i];

                    a[i] = a[j];

                    a[j] = temp;

                }

            }

            a[left] = a[j];

            a[j] = pivot;     //以a[j]為新支點,j位置左邊元素值均小於a[j],j位置右邊元素值均大於a[j]

            quickSort(a, left, j - 1);      //遞迴排序

            quickSort(a, j + 1, right);

        }

    }

    以上程式在Microsoft Visual Studio .NET 2003 和Visual C# 2005 Express Edition Beta1中均調試通過.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.