Tanveer Rifu
Tanveer Rifu

Reputation: 205

Function not working on c program selection sort

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:\n ");
   scanf("%d",&n);

   printf("Enter array elements:\n");
    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);

}

Upvotes: 2

Views: 75

Answers (2)

Jardel Lucca
Jardel Lucca

Reputation: 1203

There are 3 main issues in your code:

  1. You are getting user input twice. One in main function:

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

    Another in selection_sort function:

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

    Fix this by removing the one in selection_sort function.

  2. As noted in the comments section, you are returning from main function before calling selection_sort function:

    return 0;
    

    Fix this by moving it to the end of main function.

  3. You are printing the expected results from selection_sort function before calling it:

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

    Fix this by calling selection_sort before printing its results.

Here is the fixed code:

#include <stdio.h>

int selection_sort (int a[], int n, int i, int j, int temp, int min)
{
   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]);
    }
    
    selection_sort(a, n, i, j, temp, min);

    printf("Sorted array:");
    for(i=0;i<n;i++) {

        printf(" %d", a[i]);
    }
    printf("\n");
    
    return 0;
}

Here is a test:

$ gcc main.c && ./a.out
How many elements: 3
Enter array elements: 1 4 2
Sorted array: 1 2 4

Upvotes: 2

JAnonymity
JAnonymity

Reputation: 1

int main(){

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

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

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

    selection_sort(a,n,i,temp,min);//Not need to use j,cause j isn't used in your function
    
    for(i=0;i<n;i++)
    printf("%d\t",a[i]);//You should call your function before print your data,and also before "return 0"
    
    return 0; 
}

AND,"int i,int temp,int min" these don't need to be the parameters.You can just create them in your function

Upvotes: 0

Related Questions