Disclaimer: I know bubble sorting this much data is a waste of time, but I need to do it in some sort of "investigation".
I have a .csv file with 100.000 ints. The program crashes during bubble sorting, and while debugging it throws a "Segmentation fault". This crash ONLY happens when you enter a num higher than approximately 35.000, below that it works perfectly.
This is the recursive function:
void swap(int *arr, int i, int j) {
int temp_num = arr[i];
arr[i] = arr[j];
arr[j] = temp_num;
}
void recur_BubbleSort(int *arr, int len) {
if (len == 1) {
return;
}
for (int i = 0; i < len - 1; i++) {
if (arr[i] > arr[i + 1]) {
swap(arr, i, i + 1);
}
}
recur_BubbleSort(arr, len - 1);
}
And this is the main:
int main() {
srand(time(NULL));
FILE *fpt;
fpt = fopen("numbers_data.csv", "r");
char data[MAX];
int line = 0;
int num = 40000;
int *data_int;
data_int = (int *)malloc(sizeof(int) * (num));
// Get values from .csv and add them to the array
while (!feof(fpt) && (line < num)) {
if (fgets(data, MAX, fpt) != NULL) {
data_int[line] = atoi(data);
line++;
if (num <= 100)
printf("[%d] \n", atoi(data));
}
}
fclose(fpt);
recur_BubbleSort(data_int, num);
printf("Closing...\n");
system("pause");
return 0;
}
I don't know if it's necessary, but this is the function used to create the .csv
void GenerateData(void) {
int temp_num;
FILE *fpt;
fpt = fopen("numbers_data.csv", "w+");
for (int i = 0; i < 100000; i++) {
temp_num = rand();
fprintf(fpt, "%d\n", temp_num);
}
fclose(fpt);
printf("Created.\n");
system("pause");
}
I'm about to throw in the towel, so any help would really be appreciated, thank you.