Thursday 4 August 2016

Arrays Selection Sort

Sorting an Array using Selection Sort

#include<stdio.h>
void main()
{
          int i, j, n, a[10], temp;
          printf("\nEnter the value of n:");
          scanf("%d", &n);

          printf("\nEnter array elements:");
          for(i=0;i<n;i++)
                  scanf("%d", &a[i]);

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

          printf("\nSelection sort: array elements:");
          for(i=0;i<n;i++)
                  printf("%d ", a[i]);
}

Output:
Enter the value of n:7
Enter array elements:3 2 1 5 6 9 1
Selection sort: array elements:1 1 2 3 5 6 9

No comments:

Post a Comment