Order Algorithm Comparison and Exchanges Count Shell Sort

0

How can I count the number of comparisons and changes in Shell Sort? Where to properly use the counters?

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);
}
    
asked by anonymous 11.09.2018 / 15:22

0 answers