Thursday, January 19, 2012

Array [XIV] 2-D Array Sorting




Sorting a 2-D array Row-wise


#include <stdio.h>
#include <conio.h>

//Sorting Columnwise

  void main()
      {
           int Arr[10][10],i,j,m,n,k,temp;
           printf("\n\tEnter Row Size [Below 10] : ");
           scanf("%d",&n);
           printf("\n\tEnter Column Size [Below 10] : ");
           scanf("%d",&m);
           printf("\n");
           for(i=0;i<n;i++)
              {
                   for(j=0;j<m;j++)
                        {
                            printf("\tElement at [%d,%d] : ",i,j);
                            scanf("%d",&Arr[i][j]);
                        }
              }
           printf("\n");
           printf("\n\tOriginal Array\n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           for(k=0;k<n;k++)
              {
                   for(i=0;i<m;i++)
                        {
                            for(j=0;j<m-i-1;j++)
                                {
                                      if(Arr[k][j]>Arr[k][j+1])
                                                {
                                                          temp=Arr[k][j];
                                                          Arr[k][j]=Arr[k][j+1];
                                                          Arr[k][j+1]=temp;
                                                }
                                }
                        }
              }

           printf("\n\tSorted Array\n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           getch();
      }


Sorting a 2-D array Column-wise


#include <stdio.h>
#include <conio.h>

//Sorting Rowwise

  void main()
      {
           int Arr[10][10],i,j,m,n,k,temp;
           clrscr();
           printf("\n\tEnter Row Size [Below 10] : ");
           scanf("%d",&n);
           printf("\n\tEnter Column Size [Below 10] : ");
           scanf("%d",&m);
           printf("\n");
           for(i=0;i<n;i++)
              {
                   for(j=0;j<m;j++)
                        {
                            printf("\tElement at [%d,%d] : ",i,j);
                            scanf("%d",&Arr[i][j]);
                        }
              }
           printf("\n");
           printf("\n\tOriginal Array\n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           for(k=0;k<m;k++)
              {
                   for(i=0;i<n;i++)
                        {
                            for(j=0;j<n-i-1;j++)
                                {
                                      if(Arr[j][k]>Arr[j+1][k])
                                                {
                                                          temp=Arr[j][k];
                                                          Arr[j][k]=Arr[j+1][k];
                                                          Arr[j+1][k]=temp;
                                                }
                                }
                        }
              }

           printf("\n\tSorted Array\n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           getch();
      }



Sorting a 2-D array


#include <stdio.h>
#include <conio.h>

//Overall Matrix Sorting
  void main()
      {
           int Arr[10][10],i,j,n=0,m=0,temp;
           clrscr();
           printf("\n\tEnter Nos. of Rows [Below 10] : ");
           scanf("%d",&n);
          printf("\n\tEnter Nos. of Cols [Below 10] : ");
           scanf("%d",&m);
           for(i=0;i<n;i++)
              {
                   for(j=0;j<m;j++)
                        {
                            printf("\n\tEnter Element at [%d,%d] : ",i,j);
                            scanf("%d",&Arr[i][j]);
                        }
              }
           printf("\n\tOriginal Array \n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           for(i=0;i<(n*m)-1;i++)
              {
                   for(j=0;j<(n*m)-1;j++)
                        {
                            if(Arr[j/m][j%m]>Arr[(j+1)/m][(j+1)%m])
                                {
                                      temp=Arr[j/m][j%m];
                                      Arr[j/m][j%m]=Arr[(j+1)/m][(j+1)%m];
                                      Arr[(j+1)/m][(j+1)%m]=temp;
                                }
                        }
              }
           printf("\n\tAfter Sorting\n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           getch();
      }


No comments: