【问题标题】:Error in function for Selection-Sort选择排序功能错误
【发布时间】:2013-07-01 17:01:19
【问题描述】:

我的选择排序代码

#include <stdio.h>

void selection_sort(int a[], int n);

int main()
{
    int size;

    printf("Enter the size of array: ");
    scanf("%d",&size);

    int b[size],i = 0;
    printf("Enter %d integers to be sorted: ",size);
    while(i++ < size)
        scanf("%d",&b[i]);
    selection_sort(b, size);

    printf("Sorted integers(by selection sort) are: ");
    for(int i = 0; i < size; i++)
          printf("%d",b[i]);

    return 0;       
}

void selection_sort(int a[], int n)
{   
    while(n >= 0 )
    {
        if(n == 0)
            break;
        else
        {
            int i = 0, c = 0;
            int largest = a[0];
            while(i++ < n)
                if(largest < a[i])
                {
                    c = i ;
                    largest = a[i];
                }
            int temp = a[--n];
            a[n] = largest;
            a[c] = temp;
            selection_sort(a, n);
       } 

    }

}

关于数组升序排序

3    4    1    2

给出奇怪的输出

2293388    4    3    0

我检查了很多次,但未能解决问题。 我应该怎么做才能正常工作?
使用的算法 :
1. 搜索数组中最大的元素。
2.将最大元素移动到数组的最后一个位置。
3. 递归调用自身对数组的前n -1 个元素进行排序。

请不要给出任何其他解决方案,否则我会感到困惑。

【问题讨论】:

  • 您期望的输出是什么?您收到的输出有什么奇怪的地方?还有足够的代码来查看您如何调用该函数会有所帮助。如果n 是数组的大小,那么访问a[n] 是未定义的行为。
  • @ShafikYaghmour 好的,让我编辑
  • 你是如何“检查这个”的?使用调试器单步执行会告诉您正在使用的索引存在一些问题(例如,代码似乎从未检查过a[1]largest)。即使您由于某种原因不能使用调试器,也会有一对printf() 转储感兴趣的值(例如largesticn 和数组值)在每个step 会很快告诉你出了什么问题。
  • @MichaelBurr;现在它给4 3 0 1

标签: c arrays function sorting data-structures


【解决方案1】:

编辑

啊,我知道出了什么问题。首先,while (i++ &lt; n) 并没有完全按照您的预期去做。它检查条件i &lt; n 是否为真,然后递增i。但是,似乎在条件检查之后,i 已经在正文中增加了。例如,

while (i++ < n)
   printf ("%d ", i);

将打印出来(n=4):

1 2 3 4

所以你首先需要改变它。其次,外部的while循环完全没有必要。使用一个循环就足够了。同样,将此处的 while 循环更改为 while (i &lt; n) 并在正文中增加 i。所以最终的代码是:

#include <stdio.h>

void selection_sort(int a[], int n);

int main()
{
    int size;

    printf("Enter the size of array: ");
    scanf("%d", &size);

    int b[size], i = 0;
    printf("Enter %d integers to be sorted: ", size);
    while(i < size) {
        scanf("%d", &b[i]);
        i++;
    }

    selection_sort(b, size);

    printf("Sorted integers(by selection sort) are: ");
    i = 0;
    for(i = 0; i < size; i++)
          printf("%d ", b[i]);

    printf ("\n");
    return 0;       
}

void selection_sort(int a[], int n)
{   
    if(n == 0)
        return;
    else
    {
        int i = 0, c = 0;
        int largest = a[0];
        while(i < n) {
            if(largest < a[i])
            {
                c = i;
                largest = a[i];
            }
            i++;
        }

        int temp = a[--n];
        a[n] = a[c];
        a[c] = temp;
        selection_sort(a, n);
    } 
}

我使用您给定的输入 (3 4 1 2) 对此进行了测试,它会打印出一个排序列表:1 2 3 4

【讨论】:

  • 输出0 1 2 3
  • 那我很好奇你的函数参数是什么以及你如何打印出数组元素。
【解决方案2】:

每当您看到如此奇怪的大数字时,通常是数组越界问题。请取一个小的数据集,比如 5-6 个数字,然后遍历你的程序。我相信你可以修复它。祝你好运!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2017-04-24
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    相关资源
    最近更新 更多