-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?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • well did you step through the code in a debugger? That is the first thing to do – OldProgrammer Mar 06 '23 at 16:54
  • `m` only updates once every iteration of the list, but `i` changes, overwriting everything. Move it to the swap functionality. – B Remmelzwaal Mar 06 '23 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?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Mar 06 '23 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. – Jonathan Leffler Mar 06 '23 at 21:10

1 Answers1

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 );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    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. – B Remmelzwaal Mar 06 '23 at 16:59