Next, I'm developing a college assignment where I store and display the sorting results of various algorithms, such as comparisons, exchanges, and time. I have everything ready and written, that is, the scope of the work is Ok, we need to implement the rest of the algorithms.
I got almost everything with the help of colleagues from another forum, but I have an error that I can not solve, if anyone can give me an idea of what is happening, I am grateful.
1- Here I have my global list of records that will store sort results:
typedef struct { int numcomp; int numtrocas = 0; } STATISTICS;
2- As an example, the bubble sort algorithm receives the values by reference:
void bubbleSort (int vet [], int num, STATISTICS * statistics) {
int i, continua, aux, fim = num;
do{
continua = 0;
for(i = 0; i < fim -1; i++ ){
estatisticas->numcomp++;
if(vet > vet[i+1]){
aux = vet;
vet = vet[i+1];
vet[i+1] = aux;
estatisticas->numtrocas++;
continua = 1;
}
}
fim--;
}while(continua != 0);
}
3- Here is the list declaration and the call of the sort function with the display of stored values:
int * ptr; ptr = geraVetor (vector1); // printVector (ptr, vector1);
ESTATISTICAS* estatisticas = (ESTATISTICAS*) malloc(sizeof(ESTATISTICAS));
clock_t start, end;
start = clock();
bubbleSort(ptr,vetor1, estatisticas);
end = clock();
printf("\n\nTROCAS: %d", estatisticas->numcomp);
printf("\n\nTROCAS: %d", estatisticas->numtrocas);
4- The error consists of: when compiling my program everything is ok, however the value returned by the numcomp variable is well beyond the number of comparisons, if it looks more like a memory address. something like: 348219. But the numtrocas variable returns the value normally after increments ++;
I look forward to your help, thank you.