简体   繁体   English

如何更改我的快速排序代码以使用数组的最后一个元素作为 pivot?

[英]How do I change my Quicksort code to use the last element of my array as the pivot?

Can someone explain me how i can change the following code to use the last element of my array as the pivot?有人可以解释一下我如何更改以下代码以将数组的最后一个元素用作 pivot? In this code example i am using the first element as my pivot.在这个代码示例中,我使用第一个元素作为我的 pivot。

I found a couple of other codes but they are sorting the array in a different way which is not what i want.我发现了一些其他代码,但它们以不同的方式对数组进行排序,这不是我想要的。

In my attempts to change the pivot to use the last element i didn't get an errorcode but the sorting failed in the middle of my array.在我尝试将 pivot 更改为使用最后一个元素时,我没有收到错误代码,但在我的数组中间排序失败。 Any help is very much appreciated.很感谢任何形式的帮助。

int partition(vector<int>& v,int low, int high){
    int pivot = v[low];
    int ui{low};
    int oi{high + 1};
    
    while(true){

        while(v[++ui] < pivot){
            if(ui == high){
                break;
            }
        }
        
        while(pivot<v[--oi]){
            if(oi == low){
                break;
            }
        }
        
        if(ui >= oi){
            break;
        }
        swap(v[ui],v[oi]);
    }
    
    swap(v[low],v[oi]);
    
    return oi;
}

void quicksort(vector<int>& v, int low, int high){
    
    if(high>low){
        
        int pivot = partition(v,low,high);
        
        quicksort(v,low, pivot - 1);
        quicksort(v,pivot + 1,high);
    }
}

It does not matter where you choose your pivot.您选择 pivot 的位置并不重要。 All that matters is that you chose one.重要的是你选择了一个。 You could even choose a random one each time.您甚至可以每次随机选择一个。

Once your pivot is chosen, then you can start moving elements around, using the place where the pivot was as the initial unused, move-to spot when shuffling elements.选择 pivot 后,您可以开始移动元素,使用 pivot 所在的位置作为最初未使用的位置,在洗牌时移动到位置。

When you are done with the partitioning, your unused, move-to spot should be between the left and right sides.完成分区后,未使用的移动位置应位于左右两侧之间。 Stick the pivot value there, and return the index of that spot to the caller.将 pivot 值粘贴在那里,并将该点的索引返回给调用者。

To switch from using low as the pivot index to using high , you need to following changes:要从使用low作为 pivot 索引切换到使用high ,您需要进行以下更改:

int partition(vector<int>& v,int low, int high){
    // before:
    //int pivot = v[low];
    //int ui{low};
    //int oi{high + 1};

    // after:
    int pivot = v[high];
    int ui{low - 1};
    int oi{high};

    while(true){

        while(v[++ui] < pivot){
            if(ui == high){
                break;
            }
        }
        
        while(pivot<v[--oi]){
            if(oi == low){
                break;
            }
        }
        
        if(ui >= oi){
            break;
        }
        swap(v[ui],v[oi]);
    }
    
    //before:
    //swap(v[low],v[oi]);
    
    //after:
    swap(v[high],v[oi]);
    
    return oi;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM