Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2-D array inseriton sort, possible??

Status
Not open for further replies.

sunhater

Programmer
Oct 10, 2003
7
US
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
 
Where is 2-D array? Outer loop counter (i) is unreferenced var (unused in loop body). You sort 1st row only (but on the other hand - rows times;). There is unbalanced right brace in your snippet.
I think it's wrong attitude for me to write this school sample instead of you. Let's correct mistakes, invent void insert_sort(int /* or double? or template<class T>? */ array[], int rows, int cols) and try again (see Knuth's Bible, v.3 for clarity). It's a bad practice to write sort function with only-one-global-array-sorting capability...
 
>see Knuth's Bible, v.3 for clarity

:)

>It's a bad practice to write sort function with only-one-global-array-sorting capability

Indeed. Specially since we already have a generic sorting mechanism.

[code}
#include <vector>
#include <algorithm>
...
std::vector<int> Array;
...
Array a;
...
std::sort(a.begin(), a.end());
[/CODE]

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
PerFnurt: it seems it's sunhater's task to code insertion sort alg. Bon voyage to him(her)! Look at and help with...
Best regards.
 
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 << &quot;INSERTION SORT PROGRAM&quot; << endl << endl;

cout << &quot;First the unsorted array...&quot; << endl << endl;
for(i=0; i<rows; i++)
{
for(j=0; j<cols;j++)
{
cout<<array[ i ][ j ]<<&quot;\t &quot;;
}
cout<<endl;
}



// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

insert_sort( );

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

cout<<endl;

cout << &quot;Now the sorted array...&quot; << endl << endl;

for(i=0; i<rows; i++)
{
for(j=0; j<cols;j++)
{
cout<<array[ i ][ j ]<<&quot;\t &quot;;
}
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 &quot;It's a bad practice to write sort function with only-one-global-array-sorting capability...&quot; comment. Care to elaborate.
 
>It's a bad practice to write sort function with only-one-global-array-sorting capability

Well, there is one and only one array in the world your function can sort. The global
Code:
int array [][]
thingie.

If it at least were given the array as inparam you could use the function elsewhere too. If you ask yourself &quot;Why would I want to do that?&quot; I would be a tad bit disappionted...

Still it would only be able to sort
Code:
 int[][]
which isn't very generic.

Since this is a school project I will not go into technichal details about your function's internals (those are the rules here)...

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
So you are saying that it isn't good practice to declare the array globally because it makes the program more of a one shot deal.
 
Yes, spot on. A more general approach might be to pass your procedure a pointer to the array, and what size it is. Then you can use it from anywhere in a program and to sort any array. It can still sort a globally-defined array, but also any other array too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top