codescracker


c

C Program Selection Sort



« Previous Program Next Program »


Selection Sort in C

To sort an array in ascending order using selection sort technique in C programming, you have to ask to the user to enter the array elements, now start comparing the array elements, on found, start swapping with the help of temp variable, place first number in the temp and second number in the first, then temp in the second number and continue to next match to sort the whole array in ascending order

C Programming Code for Selection Sort

Following C program ask to the user to enter array size and array elements, then it will sort the array is ascending order and display the sorted array:

/* C Program - Sort Array */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int size, arr[50], i, j, temp;
	printf("Enter Array Size : ");
	scanf("%d",&size);
	printf("Enter Array Elements : ");
	for(i=0; i<size; i++)
	{
		scanf("%d",&arr[i]);
	}
	printf("Sorting array using selection sort...\n");
	for(i=0; i<size; i++)
	{
		for(j=i+1; j<size; j++)
		{
			if(arr[i]>arr[j])
			{
				temp=arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
			}
		}
	}
	printf("Now the Array after sorting is :\n");
	for(i=0; i<size; i++)
	{
		printf("%d  ",arr[i]);
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

selection sort in c programming

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test