Code Scrappers


sorting an array C program


              #include<stdio.h>
              #include<stdlib.h>

              void main()
              {
              	   int a[20];

                   int i, j, temp, n;

                   printf("\n enter array size: ");

                   scanf("%d",&n);

                   printf("\n enter the array elements: ");

              	    for(i=0;i<n;++i)
                    {
                        scanf("%d",&a[i]);
              		  }

                    for(i=1;i<n;++i)
                    {
                        for(j=0;j<(n-i);++j)
                        {
                          	if(a[j]>a[j+1])
                      			{
                      			   temp=a[j];

                                  a[j]=a[j+1];

                                  a[j+1]=temp;
                      			}
                        }
                    }

              	    printf("\n Sorted array : ");

                    for(i=0;i<n;++i)
                    {
                        printf("%d ",a[i]);
                    }
              }


          



Output

    enter array size: 5

    enter the array elements: 3
    1
    7
    6
    9

    Sorted array : 1 3 6 7 9