C Program Selection Sort Using C Programming Language

How to write a C Program For Selection Sort Using C Programming Language ?

In computer scienceselection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

See More : Selection sort


Solution For C Program:

//C Program For SELECTION SORT

#include<stdio.h>
main()
{
     int i,n,a[50];
     clrscr();
     printf("enter how many elements do u want to enter: ");
     scanf("%d",&n);
     printf("enter the array elements: ");
     for(i=0;i<n;i++)
 scanf("%d",&a[i]);
     sel_sort(a,n);
     getch();
}
sel_sort(int a[],int n)
{
     int i,j,temp,pass,min;
     for(pass=0;pass<n-1;pass++)
     {
  min=pass;         /*assume that pass is the minimum index*/
  for(j=pass+1;j<n;j++)
  {
      if(a[min]>a[j])   /*update min*/
  min=j;
  }
  if(min!=pass)     /*swaping elements*/
  {
      temp=a[pass];
      a[pass]=a[min];
      a[min]=temp;
  }
     }
     printf("final sorted array is: ");
     for(i=0;i<n;i++)
 printf("%d  ",a[i]);
}


Learn More :