I am trying to sort a 2D array using bubble sort and then print the newly sorted array. This is what I have so far:
void sort_2Darray(int x[][MAX],int size)
{
int i;
int j;
int hold;
int pass;
for(pass=1;pass<size;pass++)
{
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(x[i][j]>x[i][j+1])
{
hold=x[i][j];
x[i][j]=x[i][j+1];
x[i]j+1]=hold;
}
}
}
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
printf("%2d", x[i][j]);
}
printf("\n");
}
}
}
And this is what it prints when size==4
before sorting:
0 3 6 6
2 9 7 0
4 1 1 1
7 0 2 6
after it printed this 4 times:
0 3 6 6
2 7 0 0
1 1 1 4
0 2 6 -352439155
for(pass=0;pass<size;pass++)
{
for(i=0;i<(size-1);i++)
{
for(j=0;j<(size-i-1);j++)
{
if(x[pass][j]>x[pass][j+1])
{
hold=x[pass][j];
x[pass][j]=x[pass][j+1];
x[pass][j+1]=hold;
}
}
}
I haven't tested this code, but this should work properly and do the job, I think.
Please report if it doesn't work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With