0
How can I count the number of Shell Sort comparisons and exchanges? Where to use counters correctly?
void ShellSort(int vetor[], int n)
{
int i , j , val, comp=0, swap=0;
int gap = 1;
while(gap < n) {
gap = 3*gap+1;
}
while ( gap > 1) {
gap /= 3;
for(i = gap; i < n; i++) {
val = vetor[i];
j = i;
comp++;
while (j >= gap && val < vetor[j - gap]) {
vetor[j] = vetor [j - gap];
j = j - gap;
swap++;
}
vetor [j] = val;
}
}
//printf("Comparações: %d\nTrocas: %d\n\n", comp, swap);
}
Any problem?
– Maniero
The count is not correct. I’m probably putting the counters in the wrong part of the code.
– Lucas Rodrigues Costa
Thanks for the tip.
– Lucas Rodrigues Costa
Can you identify the error?
– Lucas Rodrigues Costa
has tried debugging the execution to identify what is wrong?
– Ricardo Pontual