algorithm - Loop invariant when first iteration starts -
i'm taking elementary course in data structures & algorithms, book use seminal work clrs. have trouble understanding loop invariant explained in chapter 2.1: insertion sort.
the book says that:
at start of each iteration of loop of lines 1-8, subarry a[1..j -1] consists of elements in a[1..j-1], in sorted order.
now, puzzles me. why hold when first iteration starts? array sorted looks { 5, 2, 4, 6, 1, 3 }. when first iteration of loop starts a[1.. j-1] isn't in sorted order, when iteration ends though.
what missing here?
a[1.. j-1] isn't in sorted order, when iteration ends though.
assuming value of j starts @ 2 initially, a[1.. j-1] contain array of length 1, definition sorted.
Comments
Post a Comment