-3

The full error is as follows:- "|error: cannot convert 'int*' to 'int**' for argument '1' to 'void quickSort(int**, int, int)'|"

MY whole code is below:

#include <iostream>

using namespace std;

int Partition (int *A[], int p, int r) {
    int x = *A[r];
    int i = p-1;
    for (int j=0; j<=r; j++){
        if(*A[j]<=x){
            i++;
            int save=*A[j];
            *A[j] = *A[i];
            *A[i] = save;
        }
    }
    int save2=*A[i+1];
    *A[i+1]=*A[r];
    *A[r]=save2;
    return (i+1);
}

void quickSort(int *A[], int p, int r) {
    if (p<r){
    int q = Partition(A, p, r);
    quickSort(A, p, (q-1));
    quickSort(A, (q+1), r);
    }
}



int main() {
    int RR[] = {2,8,7,1,3,5,6,4};
    int y=sizeof(RR)/sizeof(int)-1;
    cout << y << endl;
    int *QQ = RR;
    cout << *QQ << endl;
    quickSort(QQ, 0, y);

    return 0;
}

This is an implementation that I tried myself from a pseudo code. I'm new to programming so it would be a great help if you could illustrate a little of why this error occurred.

Thanks in advance

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
vibster
  • 3
  • 2
  • 1
    `int *A[]` vs `int *QQ`: missing one dimension. just read the compiler error message. – Jean-François Fabre Oct 18 '16 at 20:38
  • Alternative: Rewrite your functions so that they don't require a pointer to the array. You don't need to use a pointer here. – user4581301 Oct 18 '16 at 20:44
  • The declaration `int *A[]` is for an *array of pointers* to integers. The declaration `int A[]` is an array of integers. Are you passing an array of integer or pointers? – Thomas Matthews Oct 18 '16 at 20:48
  • @user4581301 I tried re-writing the functions as so it compute with directly passing the array in it instead of pointers. Though the errors went away, the program crashed when i ran it. Also, won't declaring new arrays in each function increase the use of memory as supposed to using pointers. – vibster Oct 18 '16 at 21:14
  • @Jean-François Fabre I think I understand it, I tried using int *A instead of int*A[]. But same as before though the errors went away but it still crashed. I'm pretty bad this ain't I. – vibster Oct 18 '16 at 21:15
  • @VibhuDubey the program crashes because of a logic error in the `Partition` function. Step through `Partition` a few times with your development environment's debugger and you'll see the problem. – user4581301 Oct 18 '16 at 21:20
  • @user4581301 you were right, i found that error in the comparison operator of the Partition. Thank you man !~vibster – vibster Oct 19 '16 at 16:23
  • To pass an array by reference, you can use `template void quicksort(int (&A)[N], int p, int r)`. This may prove more useful than pointers, since I don't see any pointer arithmetic in there. Additionally, since `r` is the last (`(sizeof(A) / sizeof(A[0])) - 1`th) element, and `N` is the size of the array, you can then make `r` default to `N - 1`. – Justin Time - Reinstate Monica Oct 24 '16 at 20:57

1 Answers1

0

The first thing I notice about the code is a whole lot of unneccessary pointer dereferencing. The contents of A will be changed without the need for additional pointers because Arrays decay to pointers (What is array decaying?) so A is treated as a pointer to the first array element and you are effectively passing the array by reference already.

Worse, int * A[] isn't a pointer to an array of int, it is an array of pointers to int. A very different thing. *A[0] does not return 2, it tries to use 2 as an address and return whatever happens to be in memory at address 2. This will almost certainly not be anything you want, or are allowed, to see so the program will do something unfortunate. Crash if you are lucky.

Instead, try

int Partition (int A[], int p, int r) {
    int x = A[r];
    int i = p-1;
    for (int j=0; j<=r; j++){
        if(A[j]<=x){
            i++;
            int save=A[j];
            A[j] = A[i];
            A[i] = save;
        }
    }
    int save2=A[i+1];
    A[i+1]=A[r];
    A[r]=save2;
    return (i+1);
}

void quickSort(int A[], int p, int r) {
    cout << p << ',' << r << endl; // Bonus: This will make the next bug really easy to see
    if (p<r){
    int q = Partition(A, p, r);
    quickSort(A, p, (q-1));
    quickSort(A, (q+1), r);
    }
}

Note the extra cout statement at the top of quickSort This will help you see the logic error in Partition. The program will crash due to a... wait for it! A Stack Overflow, but the cout will show you why.

Community
  • 1
  • 1
user4581301
  • 29,019
  • 5
  • 26
  • 45
  • Thanks for the answer and especially for the tip, Now back to fixing that Recursion. Thanks Again! – vibster Oct 18 '16 at 22:09
  • @VibhuDubey Another hint: the [Wikipedia page for Quicksort](https://en.wikipedia.org/wiki/Quicksort) about 1/4 of the way down has a pseudocode for Lomuto Quicksort, which I'm pretty sure is what you're implementing. If you compare what you have against what they have you should have the bugs picked off in a few minutes. I count 5 differences/bugs. That said, I've always found the Hoare version harder to get wrong. – user4581301 Oct 18 '16 at 22:15