Friday, November 10, 2017

Insertion Sort in C

A program to show insertion sort using C language.

I am currently accepting programming work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.


Program Listing

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

void main( )
{
int arr[5] = { 251, 17, 313, 135, 23 } ;
int i, j, k, temp ;

clrscr( ) ;

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

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

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

for ( k = i ; k > j ; k-- )
arr[k] = arr[k - 1] ;

arr[k + 1] = temp ;
}
}
}

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

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

getch( ) ;
}

No comments:

Post a Comment