我一直得到这个调试错误,我的数据已损坏。我不知道这怎么可能,也不知道怎么解决。
此程序填充数组,然后使用选择排序类型算法对数字排序。你能看到它开始排序数字,然后停止与这个损坏的数据错误。我怎样才能修好它?
完整代码:

#define _CRT_SECURE_NO_WARNINGS
#define ARY_SIZE 10
#include <stdio.h>
#include <stdlib.h>

void selectionSort(int[], int last);
void ranNumPerm_10(int bubble_1[]);

int main(void)
{
    int list[ARY_SIZE] = { 0 };


    int last;
    last = 10;

    ranNumPerm_10(list);
    for (int i = 0; i < ARY_SIZE; i++)
    {
        printf("%d\n", list[i]);
    }
    printf("\nUnsorted on top \n");

    selectionSort(list, last);

    for (int i = 0; i < ARY_SIZE; i++)
    {
        printf("%d\n", list[i]);
    }

    return 0;
}

void selectionSort(int list[], int last)
{
    int smallest;
    int tempData;

    for (int current = 0; current < last; current++)
    {
        smallest = current;
        for (int walk = current + 1; walk <= last; walk++)
        if (list[walk] < list[smallest])
        {
            smallest = walk;

            tempData = list[current];
            list[current] = list[smallest];
            list[smallest] = tempData;

        }

    }
    return;
}

void ranNumPerm_10(int list[])
{
    int oneRandno;
    int haveRand[ARY_SIZE] = { 0 };

    for (int i = 0; i < ARY_SIZE; i++)
    {
        do
        {
            oneRandno = rand() % ARY_SIZE;
        } while (haveRand[oneRandno] == 1);
        haveRand[oneRandno] = 1;
        list[i] = oneRandno;
    }
    return;
}

最佳答案

看起来像
for(int walk=current+1;walk应该是
for(int walk=current+1;walk(Walk可以取最后一个值,该值超出数组的范围)

关于c - 调试错误选择排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22887355/

10-11 19:29