Home » » simple insertion sort program in c

simple insertion sort program in c

This is simple program to perform insertion sort. First see the procedure then go for code. Insertion sort average complexity is about O(n^2).




// Insertion Sort Program

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

void insertionSort(int a[],int );
void display(int a[],int );

void insertionSort(int a[],int max)
{
int j;
for(int i=1;i<max;i++)
{
j=i-1;
int temp=a[i];
while((j>=0)&&(temp<a[j]))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}

void display(int a[],int mx)
{
for(int i=0;i<mx;i++)
printf("%d ",a[i]);
}


void main()
{
int a[12],n;
printf("Enter Number of Elements\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter %d Array Elements\n",i);
scanf("%d",&a[i]);
}
insertionSort(a,n);
display(a,n);
getch();
}

// Output :







Share this article :

0 comments:

Post a Comment

 
My Other Websites : Ad Networks | Games Copyright © 2014. Study Depth - All Rights Reserved