0
I would like to know where I need to insert my variables that count exchanges and comparisons.
In this algorithm I am dealing with an array of two positions that by reference modify details[0] that would be the amount of comparisons and details[1] that would be the amount of exchanges.
I don’t know where to insert and I’ve done research and more research and no one can answer that question. Thank you if you can answer me. Hugs.
void merge(int *array, int inicio, int meio, int fim, unsigned long int *detalhes){
int *vetAux, p1, p2, tamanho, i, j, k;
int fim1 = 0, fim2 = 0;
tamanho = fim - inicio + 1;
p1 = inicio;
p2 = meio + 1;
vetAux = (int *) malloc(tamanho * sizeof(int));
if(vetAux != NULL){
for(i = 0; i < tamanho; i++){
if(!fim1 && !fim2){
detalhes[0]++;
if(array[p1] < array[p2]){
vetAux[i] = array[p1++];
detalhes[1]++;
}
else{
vetAux[i] = array[p2++];
detalhes[1]++;
}
if(p1 > meio)
fim = 1;
if(p2 > fim)
fim2 = 1;
} else {
detalhes[0]++;
if(!fim1){
vetAux[i] = array[p1++];
detalhes[1]++;
}
else{
vetAux[i] = array[p2++];
detalhes[1]++;
}
}
}
for(j = 0, k = inicio; j < tamanho; j++, k++){
array[k] = vetAux[j];
}
}
free(vetAux);
}
void mergeSort(int *array, int inicio, int fim, unsigned long int *detalhes){
int meio;
if(inicio < fim){
meio = floor((fim + inicio)/2);
mergeSort(array, inicio, meio, detalhes);
mergeSort(array, meio+1, fim, detalhes);
merge(array, inicio, meio, fim, detalhes);
}
}
Vector statement of details in main():
unsigned long int detalhes[2] = {0};
Calling the sort function in main():
detalhes[0] = 0; detalhes[1] = 0;
tempo[0] = clock();
mergeSort(array, inicio, fim, detalhes);
tempo[1] = clock();
showDetalhes(detalhes, array, size, tempo);
Note: These variables are in the wrong places, because the results are not coming out right.
Place the declaration of
detalhes
and call (use) of functions please.– sbrubes
Done! I sent the codes.
– Haryel Ramalho