Function not working on c program selection sort
Asked 07 September, 2021
Viewed 2.7K times
  • 48
Votes

Why this function is not working here? It's not sorting on output. Suppose if I enter 1 4 2 the output is always 1 4 2 not 1 2 4.

How can properly implement this selection sort?

Thanks in advance!!

#include <stdio.h>

int selection_sort (int a[],int n, int i, int j,int temp,int min){

    for(i=0;i<n;i++)
      scanf("%d",&a[i]);

   for(i=0;i<n;i++){
        min=i;
      for(j=i+1;j<n;j++){
         if(a[j]<a[min]){
            min=j;
         }
      }
      temp=a[i];
      a[i]=a[min];
      a[min]=temp;
   }
}
int main(){

   int i, j, n,a[20], temp,min;

   printf("How many elements:
 ");
   scanf("%d",&n);

   printf("Enter array elements:
");
    for(i=0;i<n;i++)
      scanf("%d",&a[i]);

printf("Sorted array: ");
   for(i=0;i<n;i++)
      printf(" %d",a[i]);
    return 0;
    selection_sort(i,j,n,a,temp,min);

}

1 Answer