Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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 ();
  }


This post first appeared on Easy Learn Computer, please read the originial post: here

Share the post

How to sort an array’s elements

×

Subscribe to Easy Learn Computer

Get updates delivered right to your inbox!

Thank you for your subscription

×