Sorry about only including the insertion sort function and I certainly don't want anyone to write out a program for me. I was just looking for a little direction, a little leading.
Here is the whole code that I have so far.
//using namespace std;
const int rows = 3;
const int cols = 10;
void insert_sort( );
int array[rows][cols]={
{39, 312, 13, 64, 46, 246, 242, 35, 73, 89},
{391, 547, 23, 345, 86, 57, 58, 89, 56, 23},
{34, 462, 35, 28, 1, 75, 45, 296, 3456, 312}
};
// ------------------------------------------------------------------
// ------------------------------------------------------------------
int main()
{
int i, j;
cout << "INSERTION SORT PROGRAM" << endl << endl;
cout << "First the unsorted array..." << endl << endl;
for(i=0; i<rows; i++)
{
for(j=0; j<cols;j++)
{
cout<<array[ i ][ j ]<<"\t ";
}
cout<<endl;
}
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
insert_sort( );
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
cout<<endl;
cout << "Now the sorted array..." << endl << endl;
for(i=0; i<rows; i++)
{
for(j=0; j<cols;j++)
{
cout<<array[ i ][ j ]<<"\t ";
}
cout<<endl;
}
cout<<endl;
return 0;
}
// ------------------------------------------------------------------
// ------------------------------------------------------------------
void insert_sort( )
{
int temp, i, j, b;
for(int h=0; h<rows; h++)
{
for(j=1; j<cols; j++)
{
i=j-1;
b=h;
temp=array[j];
while((i>=0) && (temp < array))
{
array[i+1]=array;
i--;
if(i<=0 && b>0)
{
i=cols-1;
b--;
}
}
array[i+1]=temp;
}
}
}
As I said before, the code sorts the first line great, one example, brings the 1 from third row to [0][0] of the array. but once it goes to the second row, it just repeats a large number from the array. I appreciate all the suggestions and I know there are probably better ways to sort, but the insertion sort is the one I have to use.
I was curious about the unreferenced var comment. I set the i(now h) = to b because I didn't want to change the value of i(h) after going through the if statement. I guess that is wrong.
Also I don't quite understand the "It's a bad practice to write sort function with only-one-global-array-sorting capability..." comment. Care to elaborate.