c code for selection sort.

  • csorting
  • write a c code for selection sort.

    WAP to sort an array of n floats in an ascending order using selection sort.

    Code

    #include <stdio.h> void printArray(float *A, int n) { for (int i = 0; i < n; i++) { printf("%.1f ", A[i]); } printf("\n"); } void selectionSort(float *A, int n) { int indexOfMin; float temp; for (int i = 0; i < n - 1; i++) { indexOfMin = i; for (int j = i + 1; j < n; j++) { if (A[j] < A[indexOfMin]) { indexOfMin = j; } } temp = A[i]; A[i] = A[indexOfMin]; A[indexOfMin] = temp; } } int main() { int n = 5; float A[n]; for (int i = 0; i < n; i++) { scanf("%f", &A[i]); } printArray(A, n); selectionSort(A, n); printArray(A, n); return 0; }