Wednesday, 4 September 2013

SELECTION SORT C PROGRAM


/*CSEMATTER.BLOGSPOT.IN

Program: C program to implement selection sort*/

#include<stdio.h>
#include<conio.h>
void main( )
{
int arr[10];
int i, j, temp,n ;
printf("enter the no. of inputs");
scanf("%d",&n);
printf("enter the numbers");
for(i=0;i<n;i++)
{scanf("%d",&arr[i]);}
for ( i = 0 ; i < n-1 ; i++ )
{for ( j = i + 1 ; j < n ; j++ )
{
if ( arr[i] > arr[j] )
{
temp = arr[i] ;
       arr[i] = arr[j] ;
arr[j] = temp ;
}
} }
 printf ( "Array after sorting:\n") ;
for ( i = 0 ; i < n ; i++ )
printf ( "%d\t", arr[i] ) ;
}
/*Output
enter the no. of inputs 5
enter the numbers
12  4   67  1   90
Array after sorting:
1   4   12  67  90*/

2 comments:

  1. Selection Sort in C

    Selection sort is simplest way to sort array elements. Selection sort is based of maximum and minimum value. First check minimum value in array list and place it at first position (position 0) of array, next find second smallest element in array list and place this value at second position (position 1) and so on. Same process is repeated until sort all element of an array.

    ReplyDelete