Wednesday, 20 August 2014

Filled Under:

C program to implement Selection Sort

Share


Write a C program to sort given N elements using SELECTION sort method using functions :
a) To find maximum of elements
b) To swap two elements
Selection sort is comparison based sorting technique, It finds the minimum value in list and swaps that value to the first position and so on. Selection sort is inefficient on larger data. 

Example of C program to implement Selection Sort

 
#include <stdio.h>
#include <conio.h>
 
void main()
{
 int array[10];
 int i, j, N, temp;
 
 int findmax(int b[10], int k);      /* function declaration */
 void exchang(int b[10], int k);
 
 clrscr();
 
 printf("Enter the value of Nn");
 scanf("%d",&N);
 
 printf("Enter the elements one by onen");
 for(i=0; i<N ; i++)
 {
  scanf("%d",&array[i]);
 
 }
 
 printf("Input array elementsn");
 for(i=0; i<N ; i++)
 {
  printf("%dn",array[i]);
 }
 
 /* Selection sorting begins */
 exchang(array,N);
 
 printf("Sorted array is...n");
 for(i=0; i< N ; i++)
 {
  printf("%dn",array[i]);
 }
 
}   /* End of main*/
 
/* function to find the maximum value */
int findmax(int b[10], int k)
{
 int max=0,j;
 for(j = 1; j <= k; j++)
 {
  if ( b[j] > b[max])
  {
   max = j;
  }
 }
 return(max);
}
 
 
void exchang(int b[10],int k)
{
 int  temp, big, j;
 for ( j=k-1; j>=1; j--)
 {
 
  big = findmax(b,j);
  temp = b[big];
  b[big] = b[j];
  b[j] = temp;
 }
 return;
}