Esempio n. 1
0
        private static void quickSortWithCocktailAndPartition(int low, int high, int[] data)
        {
            if (high - low < 9)
            {
                InsertSort.Sort(data, low, high);
                return;
            }

            int midValue = selectPivotMidOfNine(low, high, data);
            int a = low, b = a, c = high, d = c;

            while (b < c)
            {
                while (b < c && midValue <= data[c])
                {
                    if (midValue == data[c])
                    {
                        Util.swap(c, d--, data);
                    }
                    c--;
                }

                while (b < c && data[b] <= midValue)
                {
                    if (midValue == data[b])
                    {
                        Util.swap(a++, b, data);
                    }
                    b++;
                }

                Util.swap(b++, c--, data);
            }
            int step;

            // 合并
            step = Math.Min(a - low, b - a);
            vecswap(low, b - step, step, data);
            step = Math.Min(high - d, d - c);
            vecswap(b + 1, high - step + 1, step, data);

            if (b - a > 1)
            {
                quickSortWithCocktailAndPartition(low, low + b - a - 1, data);
            }
            if (d - c > 1)
            {
                quickSortWithCocktailAndPartition(high - (d - c) + 1, high, data);
            }
        }
Esempio n. 2
0
        private static List <int> MergeSortOnlyList(List <int> data, int low, int high)
        {
            if (high - low < 15)
            {
                //	长度小于15则使用直接插入排序,效率可提高35%
                List <int> insertList = data.GetRange(low, high - low + 1);
                InsertSort.Sort(insertList, 0, insertList.Count - 1);
                return(insertList);
            }

            List <int> mergeList  = new List <int>();
            int        middle     = (low + high) / 2;
            List <int> leftMerge  = MergeSortOnlyList(data, low, middle);
            List <int> rightMerge = MergeSortOnlyList(data, middle + 1, high);

            //	当两个子数组已经有序,直接合并
            if (leftMerge[leftMerge.Count - 1] <= rightMerge[0])
            {
                mergeList = leftMerge;
                mergeList.AddRange(rightMerge.GetRange(0, rightMerge.Count));
                return(mergeList);
            }

            int i = 0, j = 0;

            while (true)
            {
                if (leftMerge[i] < rightMerge[j])
                {
                    mergeList.Add(leftMerge[i]);
                    if (++i == leftMerge.Count)
                    {
                        mergeList.AddRange(rightMerge.GetRange(j, rightMerge.Count - j));
                        break;
                    }
                }
                else
                {
                    mergeList.Add(rightMerge[j]);
                    if (++j == rightMerge.Count)
                    {
                        mergeList.AddRange(leftMerge.GetRange(i, leftMerge.Count - i));
                        break;
                    }
                }
            }
            return(mergeList);
        }
Esempio n. 3
0
 private static void quickSort(int low, int high, int[] data)
 {
     if (low < high)
     {
         if (high - low < 9)
         {
             InsertSort.Sort(data, low, high);
             return;
         }
         else
         {
             int middle = getPivotCocktail(low, high, data);
             quickSort(low, middle - 1, data);
             quickSort(middle + 1, high, data);
         }
     }
 }