sorting codes

weirdo_xto's Avatar, Join Date: Mar 2007
Light Poster
can you pls help me find a way to sort two dimensional arrays with a size of [5][2] 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 shabbir; 25Jun2007 at 09:03.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
You can actually just run through the 2D array and make your comparisons and swap when necessary. For instance, you may compare Array [3][1] with Array [4][6] 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.
0
weirdo_xto's Avatar, Join Date: Mar 2007
Light Poster
Code:
#include<stdio.h>
int num[5][2];
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 shabbir; 27Jun2007 at 08:17.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code
0
weirdo_xto's Avatar, Join Date: Mar 2007
Light Poster
Code:
#include<stdio.h>
int num[5][2];
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 shabbir; 27Jun2007 at 08:18.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code