Get Update on recent Technology & Programming

Friday, 10 November 2017

Selection sort In C

Posted by   on Pinterest



Selection sort In C

#include<stdio.h>

#define MAX 100

int main(void)

{

	int arr[MAX],i,j,n,temp,min;

	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]);

	}

	/*Selection sort*/

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

	{

		/*Find the index of smallest element*/

		min=i;

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

		{

			if(arr[min]>arr[j])

				min=j ;

		}

		if(i!=min)

		{

			temp=arr[i];

			arr[i]=arr[min];

			arr[min]=temp ;

		}

	}

	printf("Sorted list is : \n");

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

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

	printf("\n");

	return 0;

}





No comments:
Write comments

Hey, we've just launched a new custom color Blogger template. You'll like it -
Join Our Newsletter