Selection Sort in C

Selection Sort:

Selection sort is easy and well known sorting technique.In selection sort unordered list is divided into two parts sorted and unsorted. minimum number from unsorted list is selected and placed in at sorted array at proper location.this process is done until list get sorted.

Worst case performance:     О(n2)
Best case performance:         Ðž(n2)
Average case performance: О(n2)

#include <stdio.h>
#include <conio.h>

void main( )
{
int arr[5] = { 25, 17, 31, 13, 2 } ;
int i, j, temp ;

clrscr( ) ;

printf ( "Selection sort.\n" ) ;
printf ( "\nArray before sorting:\n") ;

for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", arr[i] ) ;

for ( i = 0 ; i <= 3 ; i++ )
{
for ( j = i + 1 ; j <= 4 ; j++ )
{
if ( arr[i] > arr[j] )
{
temp = arr[i] ;
arr[i] = arr[j] ;
arr[j] = temp ;
}
}
}

printf ( "\n\nArray after sorting:\n") ;

for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", arr[i] ) ;

getch( ) ;
}

Selection Sort in Java

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *