Ticker

6/recent/ticker-posts

Selection Sort- DSA C Program

 


This is the Selection Sort program in C for sorting the array in ascending order.

	#include<stdio.h>
	int main()
	{
	    int k, a[10], n, i, j, min, temp;
	    printf("enter the no of elements you want to add : ");
	    scanf("%d",&n);
	    printf("enter the array elements : ");
	    for(i=0; i<n; i++)
	    {
	        scanf("%d",&a[i]);
	    } 
	printf("sorted array : ");
	
	for(i=0;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=0;i<n;i++){
		printf("%d  ",a[i]);
	}
	}

Post a Comment

0 Comments