# sorting codes

Discussion in 'C' started by weirdo_xto, Jun 25, 2007.

1. ### weirdo_xtoNew Member

Joined:
Mar 9, 2007
Messages:
5
0
Trophy Points:
0
can you pls help me find a way to sort two dimensional arrays with a size of  integer...i want to sort it from highest to lowest... can you pls help me with the codes...i only know how to sort 1 dimensional array using bubble sort...

Code:
```printf("\nThe numbers from highest to lowest are: ");
for(x=0;x<=8;x++)
for(y=x+1;y<=9;y++)
if(num[x]>num[y]){even = num[x];
num[x]=num[y];
num[y]=even;
}
for(x=0;x<=9;X++)
printf("\n%d",num[x]);```
how about with two dimensional arrays????? Last edited by a moderator: Jun 25, 2007

Joined:
Jul 12, 2004
Messages:
15,341
378
Trophy Points:
83
It looks like you have not told us your exact requirements and in the context of your problem the best solution could be to convert the 2 dimensional array into a one dimension array of 5X2 elements and then sort it.

3. ### DaWeiNew Member

Joined:
Dec 6, 2006
Messages:
835
5
Trophy Points:
0
Occupation:
Semi-retired EE
Location:
Texan now in Central NY
http://www.daweidesigns.com
You can actually just run through the 2D array and make your comparisons and swap when necessary. For instance, you may compare Array  with Array  and decide whether to swap them or not. This approach merely requires a loop for each dimension. For example,
Code:
```for (i = 0; i < rowMax; i++)
{
for (j = 0; j < colMax; j++)
{
// Put your sort stuff here, which will require two more loops
...
```
This approach will allow you to sort a 2D array which is not an actual 2D array, such as one where each row is gotten independently from malloc or new. All you have to do is get the indexing or pointing right in the loops.

4. ### weirdo_xtoNew Member

Joined:
Mar 9, 2007
Messages:
5
0
Trophy Points:
0
Code:
```#include<stdio.h>
int num;
int x,y,a,b,low;

main()
{
clrscr();
for(x=0;x<=4;x++)
for(y=0;y<=1;y++){
printf("Enter number: ");
scanf("%d",&num[x][y]);
}

printf("\nThe number from highest to lowest are: ");
{
for(x=0;x<=4;x++)
for(y=0;y<=1;y++)
{
for(a=x+1;a<=4;a++)
for(b=y+1;b<=1;b++)
{
if(num[x][y]>=num[a][b]){low=num[x][y];
num[x][y]=num[a][b];
num[a][b]=low;
}
}
}
for(x=0;x<=4;x++)
for(y=0;y<=1;y++){
printf("%d",num[x][y]);
}
getch();
}```

Last edited by a moderator: Jun 27, 2007
5. ### weirdo_xtoNew Member

Joined:
Mar 9, 2007
Messages:
5
0
Trophy Points:
0
Code:
```#include<stdio.h>
int num;
int x,y,a,b,low;

main()
{
clrscr();
for(x=0;x<=4;x++)
for(y=0;y<=1;y++){
printf("Enter number: ");
scanf("%d",&num[x][y]);
}

printf("\nThe number from highest to lowest are: ");
{
for(x=0;x<=4;x++)
for(y=0;y<=1;y++)
{
for(a=0;a<=4;a++)
for(b=0;b<=1;b++)
{
if(num[x][y]>=num[a][b]){low=num[x][y];
num[x][y]=num[a][b];
num[a][b]=low;
}
}
}
for(x=0;x<=4;x++)
for(y=0;y<=1;y++){
printf("%d",num[x][y]);
}
getch();
}```
the first number inputed and the last number inputed is not sorting.... or could you give me other ways to sort this 10 numbers......pls help me

Last edited by a moderator: Jun 27, 2007