I have a question about the following code for Insertion Sort:
void insertion(Item a[], int ell, int r)
{
int i;
for (i=ell+1; i<=r; i++)
compexch(a[ell], a[i]);
{
for (i=ell+2; i<=r; i++)
{
int j=i;
Item v=a[i];
while(less (v, a[j-1]))
{
a[j]=a[j-1];
j--;
}
a[j]=v;
}
}
}
Ok, so my question is specifically about the while loop portion-I see that j is decremented and want to know what happens when j=0 and the a[-1] occurs. I do not understand how we can allow a negative index-what if the information we compare happens to work out and the while loops continues to run? Thanks.
less()function? What does it do and why is it there? Also, the second{is bothering me. Why is that there?for (int i=ell+1 ; i<=r ; i++) {…} for (int i=ell+2 ; i<=r ; i++) {…}. Having the upper/right bound inclusive is less common than exclusive.) The value placed to be able to have just one loop condition is usually called sentinel. As Nate Eldredge points out, placing this value ina[ell]is what the first loop does.