简体   繁体   English

Selection-Sort函数出错

[英]Error in function for Selection-Sort

My code for selection-sort 我的选择代码排序

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

    }

}

on sorting the array in ascending order 按升序排序数组

3    4    1    2

is giving weird output 给出奇怪的输出

2293388    4    3    0

I checked this many time but failed to remove the problem. 我检查了很多次但未能解决问题。 What should I do to work it properly? 我该怎么做才能正常工作?
Algorithm used : 使用的算法
1. search for largest element in the array. 1.搜索数组中最大的元素。
2. Move largest element to the last position of array. 2.将最大元素移动到数组的最后位置。
3. Call itself recursively to sort the first n -1 element of the array. 3.递归调用自身对数组的第一个n-1元素进行排序。

Please don't give any other solution otherwise I will get confused. 请不要给出任何其他解决方案,否则我会感到困惑。

EDIT 编辑

Ah, I see what goes wrong. 啊,我看出出了什么问题。 First of all, while (i++ < n) does not do exactly what you expect it to do. 首先, while (i++ < n)并不完全符合您的预期。 It checks if the condition i < n is true, then it increments i . 它检查条件i < n是否为真,然后它递增i However, it seems that after the conditional check, i is already incremented in the body. 但是,似乎在条件检查之后, i已经在身体中增加了。 So for example, 所以,例如,

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

will print out (with n=4 ): 将打印出来( n=4 ):

1 2 3 4

So you first need to change that. 所以你首先需要改变它。 Secondly, the outer while-loop is not at all necessary. 其次,外部while循环根本不是必需的。 Using one loop will suffice. 使用一个循环就足够了。 Again, change the while loop in here to while (i < n) and increment i in the body. 再次,将while循环更改为while (i < n)并在body中增加i SO the final code will be: 所以最终的代码是:

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

I tested this with your given input ( 3 4 1 2 ) and it prints out a sorted list: 1 2 3 4 . 我用你给定的输入( 3 4 1 2 )对它进行了测试,并打印出一个排序列表: 1 2 3 4

Whenever you see such weird big numbers, its usually an array out of bounds issue. 每当你看到这样奇怪的大数字时,它通常都是一个数组出界的问题。 Please take a small data-set, say 5-6 numbers, and walk through your program. 请使用一个小数据集,比如5-6个数字,然后浏览您的程序。 I am sure you can fix it. 我相信你可以解决它。 Good luck!! 祝好运!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM