Program to sort elements of a matrix.

#include<stdio.h>

sorting(int *a, int n,int m)
{ int l,i,j,min,temp;
l=n*m;
for(i=0;i<l;i++)
{ min= *(a+i);
for(j=i;j<l;j++)
{ if(min > *(a+j))
{ min=*(a+j);
*(a+j)=*(a+i);
*(a+i)=min;
}
}
}

}

main()
{ int i,k=0,j,a[2][2],min=0;
printf("Enter the elements of matrix:: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);

printf("\nOriginal Array is:: ");
for(i=0;i<2;i++)
{ printf("\n");
for(j=0;j<2;j++)
printf("\t%d",a[i][j]);
}

sorting(a,2,2);

printf("\nSorted Array is:: ");
for(i=0;i<2;i++)
{ printf("\n");
for(j=0;j<2;j++)
printf("\t%d",a[i][j]);
}
  puts("");

}

Advertisements

2 thoughts on “Program to sort elements of a matrix.

  1. void main()
    {
    int a[3][3];
    int i,j,k,s,temp;
    clrscr();
    printf(“\n Enter the matrix elements :\n”);
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    scanf("%d",&a[i][j]);
    }
    }
    //******loop for elements input*********************************************
    printf("\n Entered unsorted matrix elements is :\n");
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    printf("%d\t",a[i][j]);
    }
    printf("\n");
    }
    //********loops for printig the elements**************************************
    for(k=0;k<3;k++)
    {
    for(s=0;s<3;s++)
    {
    j=s;
    for(i=k;i<3;i++)
    {
    for(;ja[i][j])
    {
    temp=a[i][j];
    a[i][j]=a[k][s];
    a[k][s]=temp;
    }
    }
    j=0;
    }
    }
    }
    //*************loops to sort the elements*************************************************
    printf(“\n Sorted Matrix element is:\n”);
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    printf("%d\t",a[i][j]);
    }
    printf("\n");
    }
    //**********above code will print the elements*****************
    getch();
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s