简体   繁体   中英

i am trying to implement the double pivot partition function in reference to quicksort. Can someone help me?

hi I'm trying to implement this double pivot function and then pass the found indices to the recursive quicksort function with three calls (arr, left, pivot_sx) / (arr, pivot_sx + 1, pivot_dx-1) / (arr, pivot_dx + 1, right). I know that the function is still incomplete and that I do not return the indexes most likely I will declare a pointer to which I will assign the value of the left index and the right index will simply return it with a return.

the code I wrote for now is this:

#include <iostream>
#include<time.h>
#include <utility>
using namespace std;
int double_partition(int a[],int left,int right){
    if(a[left]>a[right]){std::swap(a[left],a[right]);}
    int piv_sx=a[left];
    int piv_dx=a[right];
    int i_piv_sx=left;
    int i_piv_dx=right;
    int i=left+1;
    int j=right-1;
    while(i<=j){
        if(a[i]<=piv_sx){
            std::swap(a[i],a[i_piv_sx]);
            i_piv_sx+=1;
        }else if(a[i]>=piv_dx){
            std::swap(a[i],a[i_piv_dx]);
            i_piv_dx-=1;
        }
        i++;
    }
        return 0;
}
void printArray(int arr[], int size){
    int i;
    for (i=0; i < size; i++)
        cout<<" "<<arr[i];
        cout<<std::endl;
}
int main()
{
    srand(time(NULL));
    int n;
    cout << "Enter the number of items:" << "\n";
    cin >>n;
    int *arr = new int(n);
    for (int x = 0; x < n; x++) {
        int num = rand() % 10000;
        arr[x]=num;
    }
    printArray(arr,n);
    double_partition(arr,0,n-1);
    printArray(arr,n);
}

as we know the pivot on the left must be smaller than the one on the right so at the beginning I compare the two pivots and if the one on the left is greater then I make the exchange. After doing this I save the values of the pivots in two variables which I will then need to make the various comparisons. and then I do this while loop where I start i from left + 1 because I don't need to compare the first element because I already know the value since it corresponds to the left pivot so it would be a useless comparison same reasoning for j = right-1. I do all the comparisons and in the end when I compile and run the code sometimes I find a correct result and sometimes I find an incorrect result.

Is there anything I'm not doing right? Could anyone help me?

I should find the array in the situation where the elements on the left are smaller than the left pivot, the elements in the center are greater than the left pivot but smaller than the right pivot and the elements on the right will be greater than the right pivot.

int *arr = new int(n); gives you 1 int , initialized with the value n .

You want int *arr = new int[n]; , which gives you an array of n int s.

Also don't forget to delete the array with delete[] arr; when you do not need it anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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