/* Selection Sorting */
#include <stdio.h>
int main()
{
int n, i, j, min, pos, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
for(i = 0; i < n; i++){
printf("Enter element %d: ", i+1);
scanf("%d", &a[i]);
}
printf("Array you entered is .....\n");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nArray in ascending order is .....\n");
for(i = 0; i < n; i++){
min = a[i];
pos = i;
for(j = i+1; j < n; j++){
if(a[j] < min){
min = a[j];
pos = j;
}
}
temp = a[i];
a[i] = a[pos];
a[pos] = temp;
printf("%d ", a[i]);
}
return 0;
}
Comments
Post a Comment