Analysis of Algorithms (AOA) / Data Structures (DS)

Insertion Sort in C using Random Numbers

Author: Bhumik Dedhia
/*
	Program to perform Insertion Sort in C using Random Numbers
	Author: Bhumik Dedhia	Author Link: https://www.facebook.com/bhumik2107
	www.pracspedia.com
*/
#include<stdio.h>
#include<conio.h>
#include<time.h>
#define MAX 6000
void insert(int x[], int n)
{
	int i,j,y;
	for(i=1;i < n;i++)
	{		
		y=x[i];
		for(j=i-1;j >= 0 && y < x[j];j--)
		{
			
			x[j+1]=x[j];
		}
		x[j+1]=y;
	}
}

void main()
{
	int n,a[MAX],i;
	time_t start,end;
	clrscr();
	printf("Enter size of list ");
	scanf("%d",&n);
	for(i=0;i < n;i++)
		a[i]=rand();
		
	printf("\nEntered array is \n");
	for(i=0;i < n;i++)
		printf("%d ",a[i]);
	
	start=time(NULL);
	insert(a,n);
	end=time(NULL);
	printf("\nSorted array is \n");
	for(i=0;i < n;i++)
		printf("%d ",a[i]);
	
	printf("\nTIME for %d elements : %f", n, difftime(end,start));
	getch();
}
Download Source Code DS Program List AOA Program List