2

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);

}
1
  • 1
    I think the return here is before the actual sorting happening. Commented Sep 7, 2021 at 16:05

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0
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

1 Comment

Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.