所以,我需要对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++,则如下所示:
如果是 C,那么是这样的: