Write a C Program of sorting using insertion sort

Admin

 Write a C Program of sorting using insertion sort


#include<stdio.h>
#define MAX 100
int main(void)
{
	int arr[MAX],i,j,k,n;
	printf("Enter the number of elements : ");
	scanf("%d",&n);
	for(i=0; i<n; i++)
	{
		printf("Enter element %d : ",i+1);
		scanf("%d", &arr[i]);
	}
	/*Insertion sort*/
	for(i=1; i<n; i++)
	{
		k=arr[i]; /*k is to be inserted at proper place*/
		for(j=i-1; j>=0 && k<arr[j]; j--)
			arr[j+1]=arr[j];
		arr[j+1]=k;
	}
	printf("Sorted list is :\n");
	for(i=0; i<n; i++)
		printf("%d ",arr[i]);
	printf("\n");
	return 0;
}/*End of main()*/
Output
Enter the number of elements : 5
Enter element 1 : 13
Enter element 2 : 11
Enter element 3 : 90
Enter element 4 : 80
Enter element 5 : 78
Sorted list is :11 13 78 80 90 

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.