【问题标题】:Selection Sort function working sorting incorrectly选择排序功能工作排序不正确
【发布时间】:2021-09-01 17:32:04
【问题描述】:

我正在尝试在 C 中实现我的第一个排序算法,其中一个数字数组作为命令行参数给出,然后调用函数对数组进行排序并打印输出。该程序在排序之前重新打印数组,据我所知,错误在于排序算法本身。这是函数:

    void ascending(int n, int arr[])
    {
        for (int i = 0; i < (n - 1); i++)
        {
           //min is equal to i (use as index)
           int min = i;

            //Compare arr[i] to all other elements in the array
            for (int j = i + 1; j < n; j++)
            {
                //If a smaller number is found, its index (j) is now min
                if (arr[j] < arr[min])
                {
                    min = j;

                    //Swapping values to be in correct place
                    int temp = arr[min];
                    arr[min] = arr[i];
                    arr[i] = temp;
                }
            }
        }

        printf("sorted: ");
        for (int i = 0; i < n; i++)
        {
            printf("%i, ", arr[i]);
        }
        printf("\n");
    }

如果我要求它对 [5, 4, 60, 2, 1] 进行排序,它会正确地将输出排序为 sorted: [1, 2, 4, 5, 60]

但如果我要求它对 [60, 5, 3, 3, 1, 4, 2] 进行排序,它将打印:[2, 1, 4, 3, 3, 5, 60],对一些数字进行排序,但不是其他人。

提前致谢!

【问题讨论】:

  • 发现程序不起作用后你做了什么?然后你继续调试它吗?在调试器中运行您的程序,并在运行时跟踪其流程和变量值。如果您已经这样做了,请提供您找到的调试信息。 How to debug small programs

标签: c selection-sort


【解决方案1】:

Selection Sort 中,内部for 循环的目标是找到未排序子数组中最小元素的索引(即从索引i 开始到索引n-1 结束的数组)。找到最小值后应该进行交换,以便正确地将其放置在数组中的索引 i 处:

void ascending(int n, int arr[])
{
    for (int i = 0; i < (n - 1); i++)
    {
       //min is equal to i (use as index)
       int min = i;

       //Compare arr[i] to all other elements in the array
       for (int j = i + 1; j < n; j++)
       {
           //If a smaller number is found, its index (j) is now min
           if (arr[j] < arr[min])
           {
               min = j;
           }
       }

       //Swapping values to be in correct place
       int temp = arr[min];
       arr[min] = arr[i];
       arr[i] = temp;
    }
    printf("sorted: ");
    for (int i = 0; i < n; i++)
    {
        printf("%i, ", arr[i]);
    }
    printf("\n");
}

【讨论】:

  • 谢谢伙计,现在很明显,但昨晚我脑子里一片空白。祝你有美好的一天!
【解决方案2】:

在这个if语句中

           if (arr[j] < arr[min])
            {
                min = j;

                //Swapping values to be in correct place
                int temp = arr[min];
                arr[min] = arr[i];
                arr[i] = temp;
            }

分配后

                min = j;

这个交换

                //Swapping values to be in correct place
                int temp = arr[min];
                arr[min] = arr[i];
                arr[i] = temp;

没有意义。

内部for循环可以这样写

        for (int j = i + 1; j < n; j++)
        {
            //If a smaller number is found, its index (j) is now min
            if (arr[j] < arr[min])
            {
                min = j;
            }
        }

        if ( min != i )
        {   
                //Swapping values to be in correct place
                int temp = arr[min];
                arr[min] = arr[i];
                arr[i] = temp;
        }

注意,函数的声明方式最好是第一个参数指定数组,第二个参数指定数组中的元素个数,

例如

void ascending( int arr[], size_t n )
{
    for ( size_t i = 0; i < n; i++ )
    {
        size_t min = i;

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2016-01-24
    相关资源
    最近更新 更多