Shortcut for chapter specific information

Wednesday, April 20, 2011

25th writing of insertion sort (C, with for-while transformation)

instead of

  for(j=1;j<N;j++){
    k = A[j];
    i = j-1;
    while(i>=0&&A[i]>k){
      A[i+1]=A[i];
      i--;
    }
    A[i+1]=k;
  }

one can do while-for transformation.

  for(j=1;j<N;j++){
    for(i=j-1,k=A[j];i>=0 && A[i]>k;A[i+1]=A[i],i--);
    A[i+1]=k;
  }

It is shorter but it's less clear.

No comments:

Post a Comment