Selection Sort

C Programming

C Programming Examples

#include<stdio.h>

int main() {
	int data[10], i, j, k, temp;
	printf("Enter 10 random number to sort in ascending order:\n");
	for(i = 0; i < 10; i++)
		scanf("%d", &data[i]);

	/* Sorting process start */
	for(i = 0; i < 9; i++) {
		k = i;
		for(j = i + 1; j < 10; j++) {
			if(data[k] > data[j])
				k = j;
		}
		if(k != i) {
			temp = data[i];
			data[i] = data[k];
			data[k] = temp;
		}
	}
	printf("After sort\n");
	for(i = 0; i < 10; i++)
		printf("%d\n",data[i]);
	return 0;
}


          /****** Output ******/
          

Enter 10 random number to sort in ascending order:


4


6


1


8


3


7


2


5


9


0



After sort


0


1


2


3


4


5


6


7


8


9

Learn more about Selection Sort