Shell Sort Ordering Algorithm Comparisons and Exchanges Count

Asked

Viewed 305 times

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?

  • The count is not correct. I’m probably putting the counters in the wrong part of the code.

  • Thanks for the tip.

  • Can you identify the error?

  • 1

    has tried debugging the execution to identify what is wrong?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.