Q. write a C program for insertion sorting method using function.
Ans.
/* c program for insertion sorting method using function */
#include<stdio.h>
#include<conio.h>
void insertion(int [], int );
int main()
{
int arr[30];
int i,size;
printf("\n\t------- Insertion sorting using function -------\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]);
}
insertion(arr,size);
printf("\n\t------- Insertion sorted elements using function -------\n\n");
for(i=0; i<size; i++)
printf(" %d",arr[i]);
getch();
return 0;
}
void insertion(int arr[], int size)
{
int i,j,tmp;
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;
}
}
}
/*********** OUTPUT ****************/
Related programs:
Ans.
/* c program for insertion sorting method using function */
#include<stdio.h>
#include<conio.h>
void insertion(int [], int );
int main()
{
int arr[30];
int i,size;
printf("\n\t------- Insertion sorting using function -------\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]);
}
insertion(arr,size);
printf("\n\t------- Insertion sorted elements using function -------\n\n");
for(i=0; i<size; i++)
printf(" %d",arr[i]);
getch();
return 0;
}
void insertion(int arr[], int size)
{
int i,j,tmp;
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;
}
}
}
/*********** OUTPUT ****************/
Insertion sorting with screen shot |
Related programs:
- Heap sorting method and algorithm
- Heap sorting
- Bubble sorting
- Selection Sorting
- Insertion sorting
- Shell sorting
- Quick sorting
- Merge sorting
- Radix sorting
- Liner sorting
No comments:
Post a Comment