Sorting a 2D-array
#include<stdio.h> #include<string.h> main() { int a[3][3],i,j,k,n,temp; n=sizeof(a)/sizeof(a[0]); printf("enter 2D array....\n"); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf(" %d",&a[i][j]); //printf("\n"); printf("array before sorting\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf(" %d ",a[i][j]); printf("\n"); } for(i=0;i<n;i++) for(j=0;j<n;j++) for(k=j+1;k<n;k++) { if(a[i][j]>a[i][k]) { temp=a[i][j]; a[i][j]=a[i][k]; a[i][k]=temp; } } printf("array after sorting\n"); for(i=0;i<n;i++) { for(k=0;k<n;k++) printf("%d ",a[i][k]); printf("\n"); } }OutPut:
madan@madan-Lenovo-G570:~/madan$ vi sort_2D_Array.c madan@madan-Lenovo-G570:~/madan$ cc sort_2D_Array.c madan@madan-Lenovo-G570:~/madan$ ./a.out enter 2D array(3x3).... 9 8 7 6 5 4 3 2 1 array before sorting 9 8 7 6 5 4 3 2 1 array after sorting 7 8 9 4 5 6 1 2 3