Shortcut for chapter specific information

Thursday, April 28, 2011

53rd and 54th writing of insertion sort (CLRS pseudocode, C pseudocode) : insertion sort with sentinel

To deepen my understanding of insertion sort. I start to think through different variants of insertion sort.  The most trivial one is to use sentinel in the sort. 

In CLRS version, it looks like
1,  A[0] = -infinity
2,  for j = 1  to N
3,      key = A[j]
4,      i = j -1
5,      while A[i] > k
6,          A[i+1] = A[i]
7,          i = i - 1
8,      A[i+1] = k

I also wrote a C program on paper. So far looks good to me.  Of course, the compiler is still the final judge. 

No comments:

Post a Comment