Hey, I am trying to make a mergeSort to sort an array of
This is my function(s):Code:struct student {
char first[30];
char last[30];
int month;
int day;
int year; //this is not looked at while sorting
}
Some helpful advice please :) :) The error produced is that it crashes randomly in the middle of printing. What's worse, based on previous output before it prints the whole and finished. However, if I take out the prints, then it finishes and the program does NOT crash. However, it prints them out of order. Anybody can see what's wrong with my mergeSort?Code://sorts left and right halves and then merges them together; recursively called
//to get smaller halves
void mergeSort(struct student* list[], int low, int high) {
if(low<high) {
int mid=(low+high)/2;
printf("\nMerge LOW,MID,HIGH: %d-%d-%d\n",low,mid,high);
mergeSort(list,low,mid);
mergeSort(list,mid+1,high);
merge(list,low,mid+1,high);
}
}
//takes a second array and organizes each half of the array alphabetically, and then
//replaces the values in the array
void merge(struct student* list[], int low, int mid, int high) {
int length=high-low+1;
printf("\nLength: %d\n",length);
struct student* temp[1002];
int count1=low, count2=mid;
int index=0;
while((count1<mid) && (count2<high)) {
if(compareAge(list[count1],list[count2])==-1) {
temp[index]=list[count1];
index++;
count1++;
}
else {
temp[index]=list[count2];
index++;
count2++;
}
}
int i;
if(count1<mid) {
for(i=index;i<length;i++) {
temp[i]=list[count1];
count1++;
}
}
else if(count2<high) {
for(i=index;i<length;i++) {
temp[i]=list[count2];
count2++;
}
}
for(i=0;i<high;i++) {
if(temp[i]->first=="NULL" || temp[i]->last=="NULL") {
printf("Temp[%d]: NULL",i);
}
else
printf("Temp[%d]: %s %s\n",i,temp[i]->first,temp[i]->last);
}
for(i=low;i<=high;i++) {
list[i]=temp[i-low];
}
for(i=0;i<high;i++) {
if(list[i]->first=="NULL" || list[i]->last=="NULL") {
printf("List[%d]: NULL",i);
}
else
printf("List[%d]: %s %s\n",i,list[i]->first,list[i]->last);
}
}