c - 调试错误选择排序

标签 c arrays sorting

我不断收到此调试错误,提示我的数据已损坏。我不知道这怎么可能,也不知道如何解决。

Screenshot of error

该程序填充一个数组,然后使用选择排序类型的算法对数字进行排序。您是否可以看到它开始对数字进行排序,然后因数据损坏错误而停止。我该如何解决?

完整代码:

#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 <= last; walk++)

应该是

for (int walk = current + 1; walk < last; walk++)

(walk可以取到最后的值,在数组中超出范围)

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

相关文章:

c - 从 stdin 回显 getchar 和 '\n' char

c - 通过引用 C 传递 Struct *

c - HashTable插入和查找的指针问题

C pragma omp 并行

java - 对通用集合进行排序

java - 对 "sorted"数组进行排序

javascript - 从数组javascript中删除元素

c++ - 为什么 fill() 在一维数组上使用时编译,但在多维数组上不编译

arrays - 删除包含全零的数组层

java - 如何根据学生的分数对学生姓名进行排序?