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;

            //}

      

        

      

  • 相关阅读:
    C#应用NPOI实现导出EXcel表格中插入饼状图(可实现动态数据生成)
    Asp.Net开发学习知识点整理
    Javascript,闭包
    sublime 自定义快捷生成代码块
    $.extend()使用
    ztree 数据格式及其配置
    ztree 数据格式 及 基本用法
    表单中两个submit如何判断点击的是哪个submit
    myChart.on('finished')
    jQuery数组排序
  • 原文地址:https://www.cnblogs.com/frank2008syj/p/2664276.html
Copyright © 2011-2022 走看看