Selection Sort C program taking n input from user and generating ramdom array.

#include<stdio.h>

#define max 50



void main()

{

	int i, n, a[max];



	printf("\nEnter no of Elements:");

	scanf("%d",&n);

generate(a,n);

	printf("\nRandom Elements:");

	for(i=1;i<=n;i++)

		printf("%d ",a[i]);



 printf("\nSorted List Is:\n\n");

	selsort(n,a);



}



void selsort(int n, int a[])

{

	int i,j,min,temp;





	for(i=1;i<=n-1;i++)

	{

		min = i;

		for(j=i+1;j<=n;j++)

		{

			if(a[j]<a[min])

				min = j;

		}

		temp=a[i];

		a[i]=a[min];

		a[min]=temp;

	}

	for(i=1;i<=n;i++)

		printf("%d ",a[i]);

}

void generate(int a[],int n)

{

int i;

for(i=0;i<n;i++)

   a[i]=rand()%100;

}

Post a Comment

2 Comments

Thanks,To visit this blog.