Monday, 20 June 2011

Selection Sort Program in c



#include <stdio.h>

void selection_sort();
int a[50], n;

main()
{
  int i;
  
  printf("\nEnter size of an array: ");
  scanf("%d", &n);
  
     printf("\nEnter elements of an array:\n");
  for(i=0; i<n; i++)
    scanf("%d", &a[i]);
  
     selection_sort();
  
     printf("\n\nAfter sorting:\n");
  for(i=0; i<n; i++)
    printf("\n%d", a[i]);


}

void selection_sort()
{
     int i, j, min, temp;

     for (i=0; i<n; i++)
     {
       min = i;
          for (j=i+1; j<n; j++)
          {
      if (a[j] < a[min])
        min = j;
          }
          temp = a[i];
          a[i] = a[min];
          a[min] = temp;
     }
}
Editing & Program by 'Shojib'

No comments:

Post a Comment