Write a program to Insertion Sort using function in C program.
CODING:
#include<stdio.h>
#include<conio.h>
void insertion_sort(int *,int);
void display(int *,int);
void main()
{
int i,n,list[20];
clrscr();
printf("How Many Enter The Element:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the number:");
scanf("%d",&list[i]);
}
insertion_sort(list,n);
display(list,n);
getch();
}
void insertion_sort(int list[],int n)
{
int p,i,temp=0,k;
list[0]=-0;
for(i=1;i<=n;i++)
{
p=i-1;
temp=list[i];
while(temp<list[p])
{
list[p+1]=list[p];
p--;
}
list[p+1]=temp;
printf("\n STEP=%d",i);
for(k=0;k<=n;k++)
{
printf("%d \t",list[k]);
}
printf("\n");
}
}
void display(int list[],int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("\n %d",list[i]);
}
}
CODING:
#include<stdio.h>
#include<conio.h>
void insertion_sort(int *,int);
void display(int *,int);
void main()
{
int i,n,list[20];
clrscr();
printf("How Many Enter The Element:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the number:");
scanf("%d",&list[i]);
}
insertion_sort(list,n);
display(list,n);
getch();
}
void insertion_sort(int list[],int n)
{
int p,i,temp=0,k;
list[0]=-0;
for(i=1;i<=n;i++)
{
p=i-1;
temp=list[i];
while(temp<list[p])
{
list[p+1]=list[p];
p--;
}
list[p+1]=temp;
printf("\n STEP=%d",i);
for(k=0;k<=n;k++)
{
printf("%d \t",list[k]);
}
printf("\n");
}
}
void display(int list[],int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("\n %d",list[i]);
}
}
Comments
Post a Comment