Write a program to sort elements of an array in ascending order using selection sort.
#include<stdio.h> void print(int [],int); void selection_sort(int a[],int n); int main() { int a[] = {50,20,70,10,90,100,30,60,40,80},size; size = sizeof(a)/sizeof(a[0]); print(a,size); selection_sort(a,size); print(a,size); return 0; } void print(int a[],int n) { int i; for(i = 0;i < n;i++) printf("%d ",a[i]); printf("\n"); } void selection_sort(int a[],int n) { int i,j,k,t,min; for(i = 0;i < n-1;i++) { min = i; for(j = i+1; j < n;j++) { if(a[j] < a[min]) { min = j; } } if(i != min) { k = a[i]; a[i] = a[min]; a[min] = k; } } }
Output
50 20 70 10 90 100 30 60 40 80 10 20 30 40 50 60 70 80 90 100