Skip to content Skip to sidebar Skip to footer

How to sort an array’s elements

Here we have used Bubble Sort algorithm. First we will take the array’s elements. For this we have used scanf() function. Then we will sort the array’s elements using Bubble Sort Algorithm.


#include<stdio.h>
#include<conio.h>
  void main()
  {
            int a[20],N;
            clrscr();

            printf("How many item:");
            scanf("%d",&N);
            printf("Enter items :\n");
            for(int i=0;i<N;i++)   // Taking array’s elements
            {
                  scanf("%d",&a[i]);
            }
            printf("Array is :");   // Showing  array’s elements
            for(i=0;i<N;i++)
            {
                  printf("%d  ",a[i]);
            }
// sorting array’s elements
            for(i=0;i<N;i++)  
            {
              int ptr=0;
              while(ptr<N-1-i)
              {
                if(a[ptr]>a[ptr+1])
                 {
                         int temp=a[ptr];
                         a[ptr]=a[ptr+1];
                         a[ptr+1]=temp;
                 }
                        ptr=ptr+1;
              }
            }

            printf("\nSorted array :");  // Showing sorted array’s elements
            for(i=0;i<N;i++)
            {
                   printf("%d  ",a[i]);
            }
   getch ();
  } 

Post a Comment for "How to sort an array’s elements"