递归选择算法退出,为什么?

时间:2015-07-21 06:44:28

标签: c recursion selection-sort

我完成了编写递归选择算法的任务,我做了。 该算法工作正常,直到我将数组大小增加到150多个元素。

在增加元素后,算法停止工作并以“奇怪”(-1073741571(0xC00000FD))退出代码退出。

我想我超出了数据范围。但即使在我的代码上盯着一个小时后,我找不到哪里。使用调试功能并没有给我任何提示。

有人可以帮助我为什么这段代码不能使用超过150个元素吗?

#include <stdio.h>
#include <string.h>
#include <limits.h>

#define MAX 500


// print the array and count the number of reoccurring characters and print it.
void ausgabe(int *array, size_t size)
{
    int i;
    int prev_char = INT_MAX, cur_char, count = 0;
    for (i = 0; i < size; i++)
    {
        cur_char = array[i];
        if (cur_char == prev_char)
        {
            printf("%c", array[i]);
            prev_char = cur_char;
            count++;
        }
        else
        {
            printf(" Count = %i\n%c", count, array[i]);
            prev_char = cur_char;
            count = 0;
        }
    }
    printf(" Count = %i", count);
}

//Fill the array with characters from A to Z and start over
void fill_field(int *array, size_t size)
{
    int i, j = 0;
    for (i = 0; i < size; i++)
    {
        if ( j > 25)
            j = 0;
        array[i] =  65 + j++;
    }
}
//Recursive selectionsort algorithm.
void selection_sort(int *source, size_t size)
{
    static int min = INT_MAX, position = 0, start_pos = 0, temp, found;
    if ( position < size)
    {
        if (min > source[position])
        {
            min = source[position];
            found = position;
        }
        position++;
        selection_sort(source, size);
    }
    else
    {
        temp = source[start_pos];
        source[start_pos] = min;
        source[found] = temp;

        position = start_pos + 1;
        start_pos++;
        min = INT_MAX;
        if ( position < size )
        selection_sort(source, size);
    }
}

int main(void)
{
    int array[MAX];
    fill_field(array, MAX);
    ausgabe(array, MAX);
    selection_sort(array, MAX);
    ausgabe(array, MAX);
    return 0;
}

0 个答案:

没有答案