QuickSortSplit

   static int Split(int[] arr, int first, int last)        

  {

            int splitPos = first;

            int pivot = arr[first];

            int lastExchangePos = first;

            bool getLastExchange = false;

            for (int i = last; i >= first + 1; --i )

            {                

       if (arr[i] < pivot && arr[i - 1] > pivot)

                 {                    

        getLastExchange = true;

                       lastExchangePos = i;

                       break;

                 }            

    }

            for (int i = first + 1; i <= last; i++)

            {                               

      if (arr[i] < pivot)                

      {                    

        Swap<int>(ref arr[i], ref arr[splitPos]);

                      splitPos++;

                 }            

    }

            if (getLastExchange)

            {              

      Swap<int>(ref arr[lastExchangePos], ref arr[splitPos]);            

    }

            return splitPos;

        }

    

   关键:找到最后需在splitPos处交换Pivot值的地方,即找到最后该值的特征。

  简单处理:用C#函数即可。

  

        //static int Split(int[] arr, int first, int last)

        //{

        //    int splitPos = first;

        //    int pivot = arr[first];

        //    for (int i = first + 1; i <= last; i++)

        //    {

        //        if (arr[i] < pivot)

        //        {

        //            Swap<int>(ref arr[i], ref arr[splitPos]);

        //            splitPos++;

        //        }

        //    }

        //    Swap<int>(ref arr[Array.IndexOf(arr, pivot)], ref arr[splitPos]);    //indexof(pivot);  ,即存初值的用处

        //    return splitPos;

        //}

  

    

  

原文地址:https://www.cnblogs.com/frank2008syj/p/2664276.html