備注學院

          LuLu

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            5 隨筆 :: 50 文章 :: 16 評論 :: 0 Trackbacks
          CSDN - 文檔中心 - .NET 閱讀:1338   評論: 0    參與評論
          標題   【算法】C#快速排序類     選擇自 lovewangshu 的 Blog
          關(guān)鍵字   【算法】C#快速排序類
          出處  

          快速排序的基本思想是基于分治策略的。對于輸入的子序列ap..ar,如果規(guī)模足夠小則直接進行排序,否則分三步處理:

          分解(Divide):將輸入的序列ap..ar劃分成兩個非空子序列ap..aq和aq+1..ar,使ap..aq中任一元素的值不大于aq+1..ar中任一元素的值。 
          遞歸求解(Conquer):通過遞歸對p..aq和aq+1..ar進行排序。 
          合并(Merge):由于對分解出的兩個子序列的排序是就地進行的,所以在ap..aq和aq+1..ar都排好序后不需要執(zhí)行任何計算ap..ar就已排好序。 
          這個解決流程是符合分治法的基本步驟的。因此,快速排序法是分治法的經(jīng)典應(yīng)用實例之一。

          using System;

          namespace VcQuickSort
          {
           /// <summary>
           /// ClassQuickSort 快速排序。
           /// 范維肖
           /// </summary>

           public class QuickSort
           {
            public QuickSort()
            {
            }

            private void Swap(ref int i,ref int j)
            //swap two integer
            {
             int t;
             t=i;
             i=j;
             j=t;
            }
            
            public void Sort(int [] list,int low,int high)
            {
             if(high<=low)
             {
              //only one element in array list
              //so it do not need sort
              return;
             }
             else if (high==low+1)
             {
              //means two elements in array list
              //so we just compare them

              if(list[low]>list[high])
              {
               //exchange them
               Swap(ref list[low],ref list[high]);
               return;
              }
             }
             //more than 3 elements in the arrary list
             //begin QuickSort

             myQuickSort(list,low,high);
            }

            public void myQuickSort(int [] list,int low,int high)
            {
             if(low<high)
             {
              int pivot=Partition(list,low,high);
              myQuickSort(list,low,pivot-1);
              myQuickSort(list,pivot+1,high);
             }
            }

            private int Partition(int [] list,int low,int high)
            {
             //get the pivot of the arrary list
             int pivot;
             pivot=list[low];
             while(low<high)
             {
              while(low<high && list[high]>=pivot)
              {
               high--;
              }
              if(low!=high)
              {
               Swap(ref list[low],ref list[high]);
               low++;
              }
              while(low<high && list[low]<=pivot)
              {
               low++;
              }
              if(low!=high)
              {
               Swap(ref list[low],ref list[high]); 
               high--;
              }
             }
             return low;
            }

           }
          }


          posted on 2008-08-07 09:14 smildlzj 閱讀(186) 評論(0)  編輯  收藏 所屬分類: C#
          主站蜘蛛池模板: 会昌县| 固原市| 班玛县| 松溪县| 民丰县| 民县| 泽库县| 开封市| 永平县| 专栏| 涞源县| 北京市| 伊通| 朝阳市| 商城县| 封丘县| 荔浦县| 平邑县| 兴山县| 盐城市| 关岭| 剑河县| 双流县| 双鸭山市| 朝阳县| 德令哈市| 比如县| 双峰县| 乡宁县| 普安县| 澎湖县| 都昌县| 清水河县| 崇仁县| 荣昌县| 绥棱县| 黄陵县| 河南省| 靖西县| 成都市| 翁源县|