Selection Sort

By Developgram - December 12, 2016

/*
Summary: Selection sort divides the input list into two parts: the sublist of items already sorted and the sublist of items remaining to be sorted. It proceeds by finding the smallest (or largest) element in the unsorted sublist, exchanging it with the leftmost unsorted element and moving the sublist boundaries one element to the right.
Complexity - O(n^2)
*/

#include<conio.h>
#include<stdio.h>
#define max 50

int selsort(int, int[]);

int main()
{
 int i, size, data[max];

 printf("\nEnter no of Elements:");
 scanf("%d",&size);

 printf("\nEnter Elements:");
 for(i=1;i<=size;i++)
  scanf("%d",&data[i]);

 printf("\nUnsorted data:\n");
 for(i=1;i<=size;i++)
  printf("%d\t",data[i]);

 selsort(size, data);

 return 0;
}

int selsort(int n, int data[])
{
 int i, j, min, temp;

 printf("\nSorted List is:\n");
 for(i=1;i<=n-1;i++)
 {
  min = i;
  for(j=i+1;j<=n;j++)
  {
   if(data[j]<data[min])
    min = j;
  }

  temp=data[i];
  data[i]=data[min];
  data[min]=temp;
 }

 for(i=1;i<=n;i++)
  printf("%d\t",data[i]);

}

  • Share:

You Might Also Like

0 comments