-1
I need to do a college work involving quicksort, but I’m finding difficulties in the code:
#include <stdio.h>
#include <stdlib.h>
#define RA 8
int quickSort(int *vec[], int left, int right, int qtd );
int partition(int *vec[], int left, int right);
void troca(int *x,int *y);
main(){
int vet[RA] = {1,8,3,6,2,5,2,5};
int left = 0;
int right = left + 1;
int qtd;
printf("Antes do QuickSort: ");
printf("\n[ ");
for(int i = 0; i <= RA; i++){
printf("%d ",vet[i]);
}
printf(" ]\n");
quickSort(&vet[RA], left, right, qtd);
}
int quickSort(int vec[], int left, int right, int qtd){
int r;
if (right > left){
r = partition(&vec,left,right);
qtd = quickSort(&vec,left, r - 1, qtd);
qtd = quickSort(&vec, r+1, right, qtd);
}
return (qtd + 1);
}
int partition(int *vec[], int left, int right){
int i, j;
i = left;
for(j = left + 1; j <= right; j++){
if(vec[j] < vec[left]){
i++;
troca(&vec[i], &vec[j]);
}
}
troca(&vec[left],&vec[i]);
return i;
}
void troca(int *x,int *y, int aux){
aux = *x;
*x = *y;
*y = aux;
}
Return me the following errors:
Estrutura 2.cpp In function 'int main()':
[Error] cannot convert 'int*' to 'int**' for argument '1' to 'int quickSort(int**, int, int, int)'
In function 'int partition(int**, int, int)':
[Error] cannot convert 'int**' to 'int*' for argument '1' to 'void troca(int*, int*)'
[Error] cannot convert 'int**' to 'int*' for argument '1' to 'void troca(int*, int*)'
I have the slight impression that I did something wrong in passing parameters.
What don’t you understand? What you tried to solve the situation?
– MauroAlmeida
@Mauroalmeida errors seem to draw attention to the passing of parameters, but when modifico returns more errors.
– rebel_vitor
Do you know the difference between memory addresses and pointers? Use of '&' and '*'?
– MauroAlmeida
@Mauroalmeida * vc will be accessing the contents of the variable in memory, and & vc will be indicating the address of the variable in memory.
– rebel_vitor
exact, that’s it. Your problem is passing parameters as address in the case of the array. The array is already a pointer to memory.
– MauroAlmeida
@Mauroalmeida yes ,but where exactly the passage would be wrong?
– rebel_vitor
Vc passes a memory address to the quicksort Function and puts in the method signature that the parameter is an int *vec[], but in the implementation already puts as int vec[].
– MauroAlmeida
to get the value of the pointer you have to put * before the variables that are pointers and not the &
– MauroAlmeida
understood the mistake or need more tips? I did not want to give answer, it is better when we get there.
– MauroAlmeida
@Mauroalmeida No, I still don’t understand but I’m analyzing the code and it’s really better when the solution snaps in my head.
– rebel_vitor
Another thing I don’t understand is the number of parameters your swap function has. The signature has two and the implementation has 3. I think the implementation with 3 parameters does not make sense because the auxiliary variable should be started within the method and not as a parameter.
– MauroAlmeida
And in the uses of this function you never put the third parameter.
– MauroAlmeida
@Mauroalmeida as so third parameter?
– rebel_vitor
On one side you have
int quickSort(int *vec[]
but the other hasint quickSort(int vec[]
so the compiler doesn’t know which one you want. And the same for herevoid troca(int *x,int *y)
Down below is like thisvoid troca(int *x,int *y, int aux)
.– Isac