The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
Insertion sort is a very playful sorting of algorithm in which the sorted array or list is built one element at a time. Sorting in insertion sort algorithm is very much likely to arranging the deck of cards while playing bridge.
Here is the program to demonstrate Insertion Sort.
#include <stdio.h>
void insertion_sort(int arry[], int n);
int main()
{
int arry[30], i, n;
printf("\n Enter the size of the array : ");
scanf("%d", &n);
printf("\n Enter %d elements in the array : \n",n);
for(i=0; i<n; i++)
scanf("%d",&arry[i]);
insertion_sort(arry,n);
printf("\n Sorted Array in ascending order is : \n");
for(i=0; i<n; i++)
printf("%d ", arry[i]);
return 0;
}
void insertion_sort(int arry[], int n)
{
int i, j, temp;
for(i = 1; i<n; i++)
{
temp = arry[i];
j = i - 1;
while((temp < arry[j]) && (j >= 0))
{
arry[j+1] = arry[j];
j--;
}
arry[j+1] = temp;
}
}
Enter the size of the array : 5 Enter 5 elements in the array : 4 3 1 2 5 Sorted Array in ascending order is : 1 2 3 4 5
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019