我在C中实现了这个排序程序。为什么它不起作用?

huangapple go评论65阅读模式
英文:

I implement this sorting program in c . Why this is not working?

问题

The sorting program you provided appears to have an issue in the inner loop where you are attempting to sort the array using the Bubble Sort algorithm. The issue is in these lines of code:

arr[i] = arr[j];
arr[j] = m;

You are swapping the elements incorrectly. It should be:

arr[j] = arr[i];
arr[i] = m;

Here's the corrected version of the code:

#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]) {
                // Swap the elements correctly
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

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

    return 0;
}

With this correction, the program should sort the array correctly using the Bubble Sort algorithm.

英文:
#include &lt;stdio.h&gt;
int main() {
    int arr[5];
   
    for(int i=0; i&lt;5; i++)
        scanf(&quot;%d&quot;,&amp;arr[i]); 

    for(int i=0; i&lt;5; i++)
    {
        int m=arr[i];
        for(int j=i+1; j &lt;5; j++)
        {
            if(m&gt;arr[j])
            {
                arr[i]=arr[j];
                arr[j]=m;
            }
       }
   }

   for(int i=0; i&lt;5; i++)
       printf(&quot;%d &quot;,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?

答案1

得分: 2

问题在于这个if语句

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

它没有更新变量m的值。

你需要写成这样:

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

事实上,在内部for循环之前声明的变量m是多余的。你可以在不使用变量m的情况下写成:

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

如果只交换数组中的元素一次,循环可能会更有效。例如:

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

你可以写一个单独的函数。例如:

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

然后像这样调用它:

selection_sort(arr, 5);
英文:

The problem is this if statement

if(m&gt;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&gt;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] &lt; 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 &lt; 5; i++ )
{
    int m = i;

    for ( int j = i + 1; j &lt; 5; j++ )
    {
        if ( arr[j] &lt; 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 &lt; n; i++ )
    {
        size_t m = i;

        for ( size_t j = i + 1; j &lt; n; j++ )
        {
            if ( a[j] &lt; 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 );

huangapple
  • 本文由 发表于 2023年3月7日 00:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定