递归快速排序引起分段错误(不溢出) [英] Recursive quick-sort causing segmentation fault (not overflow)

查看:181
本文介绍了递归快速排序引起分段错误(不溢出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我(这将是一个学校项目由于明天一早非常方便)

我的天堂一直在努力执行C ++中的递归快速排序算法,然而,当我运行它,我得到一个运行时分段故障(视窗):

 的#include<&iostream的GT;
#包括LT&;&sstream GT;
#包括LT&;&stdlib.h中GT;使用命名空间std;无效getRandomList(INT,INT *);
无效outputList(INT,INT *);
无效快速排序(INT,INT *);诠释的main()
{
    COUT<<快速排序例如,杰克·威尔基\\ N的;
    而(真)
    {
        COUT<<请输入列表长度\\ n;
        串pnput;
        CIN>> pnput;
        cin.ignore(1);
        stringstream的温度;
        温度<< pnput;
        INT长;
        温度>>长度;
        如果(长度。1 ||长度100000)
        {
            COUT<<无效的输入!(0℃;输入< 100,000)\\ n;
        }
        其他
        {
            COUT<<创建随机列表<<长度LT;<项目的\\ n;
            为int *名单=新的INT [长度]
            getRandomList(长度,清单);
            outputList(长度,清单);
            双启动=时钟();
            快速排序(长度,清单);
            双停=时钟();
            双倍时间=停止启动;
            COUT<<时间和LT;<MS;
            cin.get();
            删除[]列表;
            打破;
        }
    }
}无效快速排序(INT LEN,为int *名单)
{
    如果(LEN&。1)
    {
        返回;
    }
    其他
    {
        INT低[10];
        INT中期[10];
        LMID [0] =列表[0];
        INT高[10];
        INT透镜[3] = {0,1,0};
        的for(int i = 1; I< LEN,我++)
        {
            如果(名单[1] - ,列表[0])
            {
                低[镜头[0] =列表[我]
                透镜[0] ++;
            }
            否则,如果(名单[I]>清单[0])
            {                高[镜头[2] =列表[我]
                透镜[2] ++;
            }
            其他
            {
                中旬[镜头[1] =列表[我]
                透镜[1] ++;
            }
        }
        快速排序(镜头[0],低);
        快速排序(透镜[2],高);
        的for(int i = 0; I< LEN,我++)
        {
                如果(ⅰ&下;透镜[0])
                {
                      名单[I] =低[I]
                }
                否则如果(ⅰ&下;透镜[0] +透镜[1])
                {
                       列表[I] =月中[I-透镜[0]];
                }
                其他
                {
                    名单[I] =高[I镜头[0] -lens [1];
                }
        }
    }
    返回;}

我的调试程序(DEV C ++,几乎退出互联网和不能得到任何大)说,错误是行

  LMID [0] =列表[0];

但我找不到它的任何问题,请尽快有错误,因为它调用快速排序功能,我beleive问题已是与传递一个数组,我相当肯定,该功能没有递归和腹胀堆栈

如果你需要它debuging,下面是我使用的其他功能

 无效getRandomList(INT长度,为int *输​​出)
{
    的for(int i = 0; I<长度;我+ +)
    {
        输出[I] =兰特()%100;
    }
    返回;
}无效outputList(INT长度,为int *输​​入)
{
    的for(int i = 0; I<长度;我+ +)
    {
        COUT&所述;&下;输入[1] - ;&下;;
    }
    COUT<<\\ n;
    返回;
}


解决方案

我没有看到任何地方声明或LMID变量的初始化。这将是问题可能。

Please help me (this would be very handy for a school project due tommorow)

I haven been trying to implement a recursive quick-sort algorithm in C++, however, when i run it i get a runtime segmentation fault(windows):

#include <iostream>
#include <sstream>
#include <stdlib.h>

using namespace std;

void getRandomList(int, int*);
void outputList(int, int*);
void quicksort(int, int*);

int main()
{
    cout<<"Quick Sort example, By Jack Wilkie\n";
    while (true)
    {
        cout<<"Please enter list length\n";
        string pnput;
        cin>>pnput;
        cin.ignore(1);
        stringstream temp;
        temp << pnput;
        int length;
        temp >> length;
        if (length < 1 || length > 100000)
        {
            cout<<"INVALID INPUT! (0 < input < 100,000)\n";
        }
        else
        {
            cout<<"Creating random list of "<<length<<" items\n";
            int *list = new int[length];
            getRandomList(length, list);
            outputList(length, list);
            double start = clock();
            quicksort(length, list);
            double stop = clock();
            double time = stop-start;
            cout<<time<<"ms";
            cin.get();
            delete[] list;
            break;
        }
    }
}

void quicksort(int len, int* list)
{
    if (len < 1)
    {
        return;
    }
    else
    {
        int low[10];
        int mid[10];
        lmid[0] = list[0];
        int high[10];
        int lens[3] = {0,1,0};
        for(int i = 1; i < len; i++)
        {
            if(list[i] < list[0])
            {
                low[lens[0]] = list[i];
                lens[0]++;
            }
            else if (list[i] > list[0])
            {

                high[lens[2]] = list[i];
                lens[2]++;
            }
            else
            {
                mid[lens[1]] = list[i];  
                lens[1]++;
            }
        }
        quicksort(lens[0], low);
        quicksort(lens[2], high);
        for(int i = 0; i < len; i++)
        {
                if (i < lens[0])
                {
                      list[i] = low[i];
                }
                else if (i < lens[0]+lens[1])
                {
                       list[i] = mid[i-lens[0]];
                }
                else
                {
                    list[i] = high[i-lens[0]-lens[1]];
                }
        }
    }
    return;

}

My debug program (dev c++, almost out of internet and cant get anything big) say that the error is on line

    lmid[0] = list[0];

however i cannot find any issues with it, it has the error as soon as it calls the quicksort function, i beleive the issue has something to do with passing an array, and i am fairly sure that the function isn't recursing and bloating the stack

in case you need it for debuging, here are the other functions i used

void getRandomList(int length, int* output)
{
    for(int i = 0; i < length; i++)
    {
        output[i] = rand() % 100;
    }
    return;
}

void outputList(int length, int* input)
{
    for(int i = 0; i < length; i++)
    {
        cout<<input[i]<<" ";
    }
    cout<<"\n";
    return;
}

解决方案

I dont see anywhere declaration or initialization of lmid variable. That would be problem probably.

这篇关于递归快速排序引起分段错误(不溢出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆