Sunday, 15 May 2011

for loop - please explain shellsort code in C -



for loop - please explain shellsort code in C -

this code written in ansi c ritchie..... have used comment in code doubt. have learnt shell sort youtube , have understood how works code confusing specially these loop....why have used gap= n/2....what gap? , these loop doing here.....plz :(

void shellsort(int v[], int n) { int gap, i, j, temp; for(gap= n/2; gap >0; gap /=2) // 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; } }

what edition of book reading from? think there number of programming errors in illustration code way untill 3rd edition.

gap refers int declared above loop. gap, i, j & temp integers. means hold integer numeric value (e.g, 1,2, 12, 586 etc).

a /= b partition assignment literal meaning = / b.

you might wondering v & n are?

int v[] refers int array, value used within [ ] index of array, e.g v[0], v[1], v[2], v[n]...

since v & n declared parameters of shellsort function, means when phone call function, need passed int array value, int value. without these values function cannot operate properly

if have c book, need know c in book :)

c for-loop shellsort

No comments:

Post a Comment