Sep 30, 2011

C program to sort given N elements using SELECTION sort method

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. Read more about C Programming Language .


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/


#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 N\n");
 scanf("%d",&N);

 printf("Enter the elements one by one\n");
 for(i=0; i<N ; i++)
 {
  scanf("%d",&array[i]);

 }

 printf("Input array elements\n");
 for(i=0; i<N ; i++)
 {
  printf("%d\n",array[i]);
 }

 /* Selection sorting begins */
 exchang(array,N);

 printf("Sorted array is...\n");
 for(i=0; i< N ; i++)
 {
  printf("%d\n",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;
}
Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!