Q. write a c program to implies the insertion sorting method.
Ans.
/* c program for insertion sorting method */
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[30];
int i,j,size,tmp;
printf("\n\t------ Insertion sorting method ---------\n\n");
printf("Enter total no. of elements : ");
scanf("%d", &size);
for(i=0; i<size; i++)
{
printf("Enter %d element : ",i+1);
scanf("%d", &arr[i]);
}
for(i=0; i<size; i++)
{
for(j=i-1; j>=0; j--)
{
if(arr[j]>arr[j+1])
{
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
else
break;
}
}
printf("\n\t------- Insertion sorted elements -------\n\n");
for(i=0; i<size; i++)
printf(" %d",arr[i]);
getch();
return 0;
}
/************** OUTPUT ***************/
Related programs:
Ans.
/* c program for insertion sorting method */
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[30];
int i,j,size,tmp;
printf("\n\t------ Insertion sorting method ---------\n\n");
printf("Enter total no. of elements : ");
scanf("%d", &size);
for(i=0; i<size; i++)
{
printf("Enter %d element : ",i+1);
scanf("%d", &arr[i]);
}
for(i=0; i<size; i++)
{
for(j=i-1; j>=0; j--)
{
if(arr[j]>arr[j+1])
{
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
else
break;
}
}
printf("\n\t------- Insertion sorted elements -------\n\n");
for(i=0; i<size; i++)
printf(" %d",arr[i]);
getch();
return 0;
}
/************** OUTPUT ***************/
Insertion sorting method output |
Related programs:
- Heap sorting method and algorithm
- Heap sorting
- Bubble sorting
- Selection Sorting
- Insertion sorting using function
- Shell sorting
- Quick sorting
- Merge sorting
- Radix sorting
- Liner sorting
Comments
Post a Comment