Algorithm 以第一个元素为轴心实现快速排序分区算法

Algorithm 以第一个元素为轴心实现快速排序分区算法,algorithm,Algorithm,我试图实现以第一个元素为轴心的快速排序分区算法,我研究了以最后一个元素为轴心的快速排序。有人能告诉我下面的伪代码哪里错了吗 /* Taking first element of array as pivot and movig all elements smaller than pivot left to pivot and greater tto its right */ // L is leftmost index, R is rightmost index Partition(A[]

我试图实现以第一个元素为轴心的快速排序分区算法,我研究了以最后一个元素为轴心的快速排序。有人能告诉我下面的伪代码哪里错了吗

/* Taking first element of array as pivot and movig all elements 
smaller than pivot left to pivot and greater tto its right */
// L is leftmost index, R is rightmost index 

Partition(A[],L,R) 
{
    pivot = A[L]
    i = L-1
    for (j =L to R )
    {
        // If current element is smaller than or equal to pivot

        if (A[j] <= pivot)
        {
            i++;    // increment index of smaller element
            swap A[i] and A[j]
        }
    }
    swap A[i + 1] and A[L])
    return (i + 1)
}
/*以数组的第一个元素为轴心并移动所有元素
小于左枢轴到枢轴,大于右枢轴*/
//L是最左边的索引,R是最右边的索引
分区(A[],L,R)
{
枢轴=A[L]
i=L-1
对于(j=L到R)
{
//如果当前元素小于或等于枢轴

if(A[j]经过反复尝试和思考,我发现了我的错误。下面是正确的算法

Partition(A[],L,R) 
{
    pivot = A[L]
    i = L
    for (j =L+1 to R )
    {
        if (A[j] <= pivot)
        {
            i++;    // increment index of smaller element
            swap A[i] and A[j]
        }
    }
    swap A[i] and A[L])
    return (i)
}
分区(A[],L,R) { 枢轴=A[L] i=L 对于(j=L+1到R) {
如果(A[j]“分区算法”的工作原理如下:

分区算法的目标是简单地获取一些元素集合(例如,您使用“数组”),然后围绕轴心将该集合分区(或拆分!)为两部分—左侧部分和右侧部分

对于轴左侧和轴右侧的元素,应该有一些“规则”。例如,左侧的所有元素都将小于所选轴,右侧的所有元素都将大于轴

希望这有帮助!

@SiggiSv 根据您的答案,需要将“R”增加1,这样数组的最后一个元素也会与数组一起排序,而不会在代码后面被遗漏

Partition(A[],L,R) 
{
    pivot = A[L]
    i = L
    for (j =L+1 to R+1 )
    {
        if (A[j] <= pivot)
        {
            i++;    // increment index of smaller element
            swap A[i] and A[j]
        }
    }
    swap A[i] and A[L])
    return (i)
}
分区(A[],L,R) { 枢轴=A[L] i=L 对于(j=L+1至R+1) {
if(A[j]这是我在快速排序中使用的分区版本。 也适用于包含重复项的数组。 分区将pivot(始终是第一个元素)放置在排序数组中的位置。假设左、右pivot的子数组已排序,则任务已完成

int partition(int arr[],int p, int q) {
    int i = p;
    int j = q;
    int pivot = arr[p]; //choose pivot to be a first element
    while (i < j) {
        for (; arr[i] < pivot; i++); //iterate from the beginning till an element is larger or equals to pivot
        for (; arr[j] > pivot; j--); //iterate from the end till an element is less or equals to pivot

        if(i < j) {                 //if both for loops are done, arr[i] >= pivot and needs to be
                                    //on pivot's left. arr[j] <= pivot, so we throw it to pivot's right
            if(arr[i] == arr[j])
                i++;    //taking care of duplicates

            swap(arr[i], arr[j]);
        }
    }
    cout << j<<endl;            //now pivot is in its place as if array was sorted
    return j; //all elements on j's left are smaller than all elements on j's right
}

void quicksort(int arr[], int p, int r)
{
    if (p > r ) {
        return;
    }
    int q = partition(arr, p,  r);
    quicksort(arr, p, q-1); 
    quicksort(arr, q+1, r);
}
int分区(intarr[],intp,intq){
int i=p;
int j=q;
int pivot=arr[p];//选择pivot作为第一个元素
而(ipivot;j--);//从末尾迭代,直到元素小于或等于pivot
如果(i=pivot,需要

//在枢轴的左侧。arr[j]QueCube是C++中的。Qsort是C中的Quas排序排序。你确定你错了吗?你怎么想的?你试过解释你的伪代码吗?如果你知道用最后一个元素来正确的枢轴代码,那么你可以做的是交换第一个和最后一个元素,并用最后一个元素运行代码。排序是必需的。如果L是最左边的索引,为什么要将i指定给L-1?此外,j可以从L+1开始,否则可以将轴与自身交换。快速排序的一部分还包括对左右两侧进行快速排序,否则只能将小于轴的项放在左边,而将大于轴的项放在右边。@narusin,他显然是在谈论快速排序算法,而不是一种语言或另一种语言的标准库中的任何特定实现。使用此算法,数组
[3,-1,1,5,4]
将成为
[1,-1,3,5,4]
。您需要继续左侧和右侧的算法。这是在快速排序中发生的,@Neil,但是OP指定(和问题中的文档)他只为分区部分编写伪代码。