Quick sort partition algorithm

前端 未结 2 1571
轻奢々
轻奢々 2021-01-27 07:11
void partition(int *a, int size) {
   int pivot = a[0];
   int left = 0, right = 0;
   for(left = 1, right = size-1; left <= right; left++, right--) {
       if(a[lef         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-27 07:54

    here is another option, little bit more similar to the original one

    int partition(int arr[], int left, int right)
    {
        int pivot = arr[left];
        while (left != right)
        {
            if (arr[left] > arr[right])
            {
                swap(arr[left], arr[right]);
            }
            if (pivot == arr[left])
                right--;
            else // Pivot == arr[right]
                left++;
        }
        return left;
    }
    

提交回复
热议问题