Unclear statement in Shell sort algorithm in C

73 Views Asked by At

The 3rd statement of the innermost loop is unclear to me:

void shellsort(int v[], int n) 
{
    int gap, i, j, temp;

    for(gap=n/2;gap>0;gap/=2){
        for(i=gap;i<n;i++){
            for(j=i-gap;j>=0 && v[j]>v[j+gap];j-=gap) {
                temp=v[j];
                v[j]=v[j+gap];
                v[j+gap]=temp;
            }
        }
    }
}

Why is j subtracted by gap? Doesn't it make j a negative value? For example if j was 0 and subtracted by, say 5...?

0

There are 0 best solutions below