RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1047347
Accepted
NEStenerus nester
NEStenerus nester
Asked:2020-11-18 03:15:45 +0000 UTC2020-11-18 03:15:45 +0000 UTC 2020-11-18 03:15:45 +0000 UTC

对大量数据进行排序c ++

  • 772

所以,我需要对csv文件进行排序,它有114318条记录,我需要使用快速排序,我写了一个程序,但是这里有问题,当排序超过2000条记录时,会出现错误

Необработанное исключение по адресу 0x00D21F49 в proj.exe: 0xC00000FD: Stack 
overflow (параметры: 0x00000001, 0x004A2FF8).

互联网说问题是堆栈溢出正在发生,我将所有数组移动到全局区域,但它没有帮助,告诉我,我怎样才能对整个文件进行快速排序?这是代码:

int temperature(const Star* x)
{
    int psefdo_temperature= 0;
    if (x->spectral_type[0] == 'O')
        psefdo_temperature = 70;
    if (x->spectral_type[0] == 'B')
        psefdo_temperature = 60;
    if (x->spectral_type[0] == 'A')
        psefdo_temperature = 50;
    if (x->spectral_type[0] == 'F')
        psefdo_temperature = 40;
    if (x->spectral_type[0] == 'G')
        psefdo_temperature = 30;
    if (x->spectral_type[0] == 'K')
        psefdo_temperature = 20;
    if (x->spectral_type[0] == 'M')
        psefdo_temperature = 10;
    psefdo_temperature += -1 * (x->spectral_type[1] - '0');
    return psefdo_temperature;
}

void quicksort_temperature(Star array[], int lo, int hi)
{
    if (hi - lo > 1)
    {
        Star pivot = array[lo];
        int j = lo + 1;
        for (int i = lo + 1; i < hi; i++)
            if (temperature(&array[i]) <= temperature(&pivot))
            {
                Star temp = array[i];
                array[i] = array[j];
            array[j] = temp;
            j++;
        }
        quicksort_temperature(array, lo, j - 1);
        quicksort_temperature(array, j, hi);
    }
}


int main()
{
    Star* p = (Star*)malloc(120000 * sizeof(Star));
    int number_of_stars = 114318;
    FILE* f = fopen("1.csv", "r");
    char header[200];
    fscanf(f, "%s\n", header);
    for (int i = 1; i < number_of_stars; i++)
    {
        fscanf(f, "%ld,%s,%lf,%lf,%lf,%lf,%s,%lf,%lf,%lf,%[^\n]\n",
        &p[i].hip, p[i].proper_name, &p[i].right_ascension, &p[i].declination,
        &p[i].magnitude, &p[i].absolute_magnitude, p[i].spectral_type, &p[i].x,
        &p[i].y, &p[i].z, p[i].constellation);
    }
    fclose(f);

    printf("sorting start");

    quicksort_temperature(p, 0, number_of_stars);

    char s[] = "sorted_by_temperature.txt";
    save_stars(s, p, number_of_stars);
    free(p);
    return 0;
}

这是进行排序的代码部分,文件中有几个字段,我按字段排序,spectral_type格式中有数据латинская заглавная буква, цифра,示例:G1 B0 A3如何使程序工作number_of_stars >2000?

PS只有在不比给定算法慢的情况下才能更改排序算法

PPS之星公告:

struct star
{
    long int hip;
    char proper_name[20];
    double right_ascension;
    double declination;
    double magnitude;
    double absolute_magnitude;
    char spectral_type[15];
    double x, y, z;
    char constellation[5];
};
typedef struct star Star;`
c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Harry
    2020-11-18T04:00:37Z2020-11-18T04:00:37Z

    如果您在标签中指定了 C++,则如下所示:

    bool lessClass(const Star& a, const Star& b)
    {
        const char * cls = "OBAFGKM";
    
        const char * ast = strchr(cls,a.spectral_type[0]);
        if (ast == 0) ast = cls + 7;
    
        const char * bst = strchr(cls,b.spectral_type[0]);
        if (bst == 0) bst = cls + 7;
    
        if (ast < bst) return true;
        if (ast > bst) return false;
        return a.spectral_type[1] < b.spectral_type[1];
    }
    
    int main(int argc, const char * argv[])
    {
        Star* p = (Star*)malloc(120000 * sizeof(Star));
        int number_of_stars = 114318;
    
        ...
    
        sort(p,p+number_of_stars,lessClass);
    }
    

    如果是 C,那么是这样的:

    int compClass(const void * astar, const void * bstar)
    {
        const char * cls = "OBAFGKM";
    
        const Star * a = (const Star *)astar;
        const Star * b = (const Star *)bstar;
    
        const char * ast = strchr(cls,a->spectral_type[0]);
        if (ast == 0) ast = cls + 7;
    
        const char * bst = strchr(cls,b->spectral_type[0]);
        if (bst == 0) bst = cls + 7;
    
        if (ast < bst) return -1;
        if (ast > bst) return 1;
        return a->spectral_type[1] - b->spectral_type[1];
    }
    
    int main(int argc, const char * argv[])
    {
        Star* p = (Star*)malloc(120000 * sizeof(Star));
        int number_of_stars = 114318;
    
        //...
    
        qsort(p,number_of_stars,sizeof(Star),compClass);
    }
    
    • 3

相关问题

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5