-2
#include <stdio.h>
int main() {
    int arr[5];
   
    for(int i=0; i<5; i++)
        scanf("%d",&arr[i]); 

    for(int i=0; i<5; i++)
    {
        int m=arr[i];
        for(int j=i+1; j <5; j++)
        {
            if(m>arr[j])
            {
                arr[i]=arr[j];
                arr[j]=m;
            }
       }
   }

   for(int i=0; i<5; i++)
       printf("%d ",arr[i]);
   return 0;
}

Why is this sorting program not working? Where is this program wrong? This program not sorting the array. Where is it wrong?

4
  • well did you step through the code in a debugger? That is the first thing to do Commented Mar 6, 2023 at 16:54
  • m only updates once every iteration of the list, but i changes, overwriting everything. Move it to the swap functionality. Commented Mar 6, 2023 at 16:56
  • Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: What is a debugger and how can it help me diagnose problems? You may also want to read this: How to debug small programs? Commented Mar 6, 2023 at 17:00
  • Which sort algorithm do you think you are implementing? It looks a bit like it might be a selection sort rather than a bubble sort, but if you look for selection sort algorithms (on SO or via your preferred search engine), you'll find there are key differences between them and your code. A selection sort finds the index of the minimum element in the remaining section of the array and at the end of a pass, swaps the minimum with the first (zeroth) element in the remaining section of the array. A bubble sort swaps two elements whenever it finds a pair out of sequence. Commented Mar 6, 2023 at 21:10

1 Answer 1

2

The problem is this if statement

if(m>arr[j])
{
    arr[i]=arr[j];
    arr[j]=m;
}

that does not update the value of the variable m.

You need to write

if(m>arr[j])
{
    arr[i]=arr[j];
    arr[j]=m;
    m = arr[i];
}

In fact the variable m declared before the inner for loop is redundant. You could just write without using the variable m

if ( arr[j] < arr[i] )
{
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
}

The loops could be more efficient if to swap elements in the array only one time. For example

for ( int i = 0; i < 5; i++ )
{
    int m = i;

    for ( int j = i + 1; j < 5; j++ )
    {
        if ( arr[j] < arr[m] )
        {
            m = j;
        }
    }

    if ( m != i )
    {
        int tmp = arr[i];
        arr[i] = arr[m];
        arr[m] = tmp;
    } 
}

You could write a separate function. For example

void selection_sort( int a[], size_t n )
{
    for ( size_t i = 0; i < n; i++ )
    {
        size_t m = i;

        for ( size_t j = i + 1; j < n; j++ )
        {
            if ( a[j] < a[m] )
            {
                m = j;
            }
        }

        if ( m != i )
        {
            int tmp = a[i];
            a[i] = a[m];
            a[m] = tmp;
        } 
    }
}

and call it like

selection_sort( arr, 5 );
Sign up to request clarification or add additional context in comments.

1 Comment

At that point, just move the declaration of m inside the if statement. Moreover, call it temp ‐ what it actually is. More readable and more logical.

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.