快排中 partition 的两种写法!

我怕爱的太早我们不能终老 提交于 2019-11-28 07:36:59

1

 void swap(int &A,int &B)
 {
 int temp;
 temp=A;
 A=B;
 B=temp;
  }

 int  partition(QVector<int> &numbers,int low,int high)
    {
        int pivotkey=numbers[low];//当参考值

        while(low<high)
        {
            while(low<high&&numbers[high]>pivotkey)
            {high--;}
            swap(numbers[low],numbers[high]);

           while(low<high&&numbers[low]<=pivotkey)
               { low++;}
            swap(numbers[low],numbers[high]);

        }

        return low;

    }

2

  int partition(QVector<int> &input, int s, int e){
         int j = s-1;
        int cmp = input[e];
         for (int i = s; i < e; ++i) {
            if (input[i] < cmp)
                swap(input[i], input[++j]);
       }
        swap(input[e], input[++j]);
     }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!