Shortcut for chapter specific information

Tuesday, April 19, 2011

Insertion sort: thinking

1 for j = 2 to N
2    key = A[j]
3    i = j-1
4   while i > 0 and A[i] > key
5       A[i+1] = A[i]
6       i = i -1
7   A[i+1] = key

Now ask yourself this.
1, Line 1, why not j =1 to N?
2, Line 4, why not A[i+1]?  What would happen if you do that?
3, Line 7, what would happen if you do A[i] = key?

No comments:

Post a Comment