调试错误选择排序

时间:2014-04-05 21:55:46

标签: 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;
}

1 个答案:

答案 0 :(得分:2)

看起来像

for(int walk = current + 1; walk&lt; = last; walk ++)

应该是

for(int walk = current + 1; walk&lt; last; walk ++)

(Walk可以取值最后一个,超出数组范围)