I am trying to do an insertion sort program where I sort a 2-D array. I think I am close, but I keep getting so screwy data after array[1][0]. Everything in row one sorts out great and the first element in row 2 is set right, but then it just goes wild.
Here's the source code that I have.
void insert_sort( )
{
int i, j, a, temp, b;
for(i=0; i<rows; i++)
{
for (j=1; j<cols; j++)
{
a=j-1;
b=i;
temp=array[j];
while ((a>=0) && (temp<array[a]))
{
array[a+1]=array[a];
a--;
if(a<=0 && b>0)
{
a=cols-1;
b--;
}
}
array[a+1]=temp;
}
}
}
}
any help would be welcomed
Here's the source code that I have.
void insert_sort( )
{
int i, j, a, temp, b;
for(i=0; i<rows; i++)
{
for (j=1; j<cols; j++)
{
a=j-1;
b=i;
temp=array[j];
while ((a>=0) && (temp<array[a]))
{
array[a+1]=array[a];
a--;
if(a<=0 && b>0)
{
a=cols-1;
b--;
}
}
array[a+1]=temp;
}
}
}
}
any help would be welcomed