zoukankan      html  css  js  c++  java
  • 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;

            //}

      

        

      

  • 相关阅读:
    糖尿病周围神经病变有什么表现
    天空之城
    Software Quality Assurance Framework(2)
    组织行为学2
    Software Quality Assurance Framework(1)
    radiculously
    组织行为学1
    software Architecture(1)
    c++运算符重载
    get up~!
  • 原文地址:https://www.cnblogs.com/frank2008syj/p/2664276.html
Copyright © 2011-2022 走看看