FirstChance异常StackOverFlow合并排序Shell排序冒泡排序

时间:2012-12-11 04:25:45

标签: c++ shell mergesort bubble-sort stack-overflow

嘿伙计们,我正在研究某些类型,并尝试实现冒泡排序,合并排序和shell排序。我使用过时的技术,但我想知道你们是否可以让我知道为什么我一直收到以下错误:

sortApplication2.exe中0x01135EF7的第一次机会异常:0xC00000FD:堆栈溢出(参数:0x00000000,0x00542000)。 sortApplication2.exe中0x01135EF7处的未处理异常:0xC00000FD:堆栈溢出(参数:0x00000000,0x00542000)。

我正在使用Visual Studio 2012,如果它起任何作用。我的代码有三个不同的文件,所以我将分别发布。

我的标题文件:

#pragma once
class sort
{
public:
    sort(); 
    void random1(int array[]); 
    void random2(int array[]); 
    void random3(int array[]); 
    void bubbleSort(int array[], int length); 
    /*void merge(int *input, int p, int r);                                           
    void merge_sort(int *input, int p, int r);*/ 
    void shellSort(int array[], int length); 
};

我的班级实施档案:

#include "sort.h"
#include <time.h>
#include <iostream>

using namespace std;

sort::sort()
{}

void sort::random1(int array[])
{
        // Seed the random-number generator with current time so that
        // the numbers will be different every time the program runs.
    for(int i = 0; i < 25; i++)
    {

        srand ((unsigned) time(NULL));
        int n = rand();     //generates a random number
        array[i] = n;       //places it into the array
    }

}
void sort::random2(int array[])
{
    // Seed the random-number generator with current time so that
    // the numbers will be different every time the program runs.
    for(int i = 0; i < 10000; i++)
    {
        srand ((unsigned) time(NULL));
        int n = rand();     //generates a random number
        array[i] = n;       //places it into the array
    }

}
void sort::random3(int array[])
{
    // Seed the random-number generator with current time so that
    // the numbers will be different every time the program runs.
    for(int i = 0; i < 100000; i++)
    {
        srand ((unsigned) time(NULL));
        int n = rand();     //generates a random number
        array[i] = n;       //places it into the array
    }
}
void sort::bubbleSort(int array[], int length)
{
    //Bubble sort function 

    int i,j;

    for(i = 0; i < 10; i++)
    {
        for(j = 0; j < i; j++)
        {
            if(array[i] > array[j])
            {
                int temp = array[i]; //swap 
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}

    /*void sort::merge(int* input, int p, int r) //the merge algorithm of the merge sort
    {
        int mid = (p + r) / 2;
        int i1 = 0;
        int i2 = p;
        int i3 = mid + 1;

    // Temp array
    int x = r -p + 1;
    int *temp;
    temp = new int [x];

    // Merge in sorted form the 2 arrays
    while ( i2 <= mid && i3 <= r )
        if ( input[i2] < input[i3] )
            temp[i1++] = input[i2++];
        else
            temp[i1++] = input[i3++];
         // Merge the remaining elements in left array
    while ( i2 <= mid )
        temp[i1++] = input[i2++];

    // Merge the remaining elements in right array
    while ( i3 <= r )
        temp[i1++] = input[i3++];

    // Move from temp array to master array
    for ( int i = p; i <= r; i++ )
        input[i] = temp[i-p];
}
void sort::merge_sort(int *input, int p, int r) //the merge sort algorithm
{
    if ( p < r ) //When p and r are equal the recursion stops and the arrays are then     passed to the merge function.
    {
        int mid = (p + r) / 2;
        merge_sort(input, p, mid); //recursively calling the sort function in order to     break the arrays down as far as possible
        merge_sort(input, mid + 1, r);//recursively calling the sort function in order to     break the arrays down as far as possible
        merge(input, p, r); //merge function realigns the smaller arrays into bigger arrays     until they are all one array again
    }
}*/


void sort::shellSort(int array[], int  length) //Shell sort algorithm
{
    int gap, i, j, temp;
    for( gap = length / 2; gap > 0; gap /= 2) //gap is the number of variables to skip when doing the comparisons
    {  
        for( i = gap; i < length; i++) //This for loop sets the variable to use as the gap for the comparisons
        {   
            for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j -= gap)
            {
                temp = array[j]; //the array variables are swapped
                array[j] = array[j + gap];
                array[j + gap] = temp;
             }
         }
     }
}

我的驱动文件:

#include "sort.h"
#include <iostream>

using namespace std;

int main()
{
    int bubbleArray1[25]; //these are the arrays to be sorted. three for each sort.     each has a length of 25, 10000, or 100000.
    int bubbleArray2[10000];
    int bubbleArray3[100000];
    int mergeArray1[25];
    int mergeArray2[10000];
    int mergeArray3[100000];
    int shellArray1[25];
    int shellArray2[10000];
    int shellArray3[100000];
    sort Sorts;

    Sorts.random1(bubbleArray1);
    Sorts.random1(mergeArray1);
    Sorts.random1(shellArray1);
    Sorts.random2(bubbleArray2);
    Sorts.random2(mergeArray2);
    Sorts.random2(shellArray2);
    Sorts.random3(bubbleArray3);
    Sorts.random3(mergeArray3);
    Sorts.random3(shellArray3);

    cout << "BubbleSort1 is now being sorted.\n";
    Sorts.bubbleSort(bubbleArray1, 25);
    cout << "BubbleSort2 is now being sorted.\n";
    Sorts.bubbleSort(bubbleArray2, 10000);
    cout << "BubbleSort3 is now being sorted.\n";
    Sorts.bubbleSort(bubbleArray3, 100000);
    cout << "End bubble sorts.\n";

    /*cout << "MergeSort1 is now being sorted.\n";
    Sorts.merge_sort(mergeArray1, 0, 25);
    cout << "MergeSort2 is now being sorted.\n";
    Sorts.merge_sort(mergeArray2, 0, 10000);
    cout << "MergeSort3 is now being sorted.\n";
    Sorts.merge_sort(mergeArray3, 0, 100000);
    cout << "End merge sorts.\n";*/

    cout << "ShellSort1 is now being sorted.\n";
    Sorts.shellSort(shellArray1, 25);
    cout << "ShellSort1 is now being sorted.\n";
    Sorts.shellSort(shellArray2, 10000);
    cout << "ShellSort1 is now being sorted.\n";
    Sorts.shellSort(shellArray3, 100000);
    cout << "End shell sorts.\n";

    cout << "Array\tElements\n";
    cout << "BubbleSort1\t";
    for(int i = 0; i < 25; i++)
    {
        cout << bubbleArray1[i] << " ";
    }
    cout << "\nMergeArray1\t";
    for(int i = 0; i < 25; i++)
    {
        cout << mergeArray1[i] << " ";
    }
    cout << "\nShellArray1\t";
    for(int i = 0; i < 25; i++)
    {
        cout << shellArray1[i] << " ";
    }


    return 0;
}

我知道这是很多代码。并且可能有很多方法可以使代码更好。 我只是想知道是什么导致了错误,因为我无法使用我的编译器找到它。

1 个答案:

答案 0 :(得分:0)

您在堆栈上分配了太多内存。具有“自动”存储类的变量进入堆栈。改为分配堆。

所以,而不是:

int shellArray3[100000];

执行:

int* shellArray3 = new int[100000];

或者更好的是,使用std :: vector。

如果您不想使用堆内存,也可以使用静态存储类来实现这样的操作。要做到这一点:

static int shellArray3[100000];

这将为整个程序分配一个变量实例,而不是为堆栈上的每个函数条目分配一个副本。