Problem Merge Sort For Large n

My programs gives a segmentation fault for large n (n=9999999). It works fine for small n. Where n = total numbers to sort
 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 List* Merge(List *ParentList) { if(ParentList->length()==1) { return ParentList; } List *LeftList=new List; List *RightList=new List; List *TempList=new List; unsigned int Counter=ParentList->length(); unsigned int MidPoint=Counter/2; ListItem *TempPointer=ParentList->getTail(); while(Counter!=MidPoint) { RightList->insertAtHead(TempPointer->value); TempPointer=TempPointer->prev; Counter--; } while(TempPointer!=NULL) { LeftList->insertAtHead(TempPointer->value); TempPointer=TempPointer->prev; } //delete ParentList; LeftList=Merge(LeftList); RightList=Merge(RightList); ListItem *LeftPointer=LeftList->getHead(); ListItem *RightPointer=RightList->getHead(); if(LeftPointer->value<=RightPointer->value) { TempList->insertAtHead(LeftPointer->value); LeftPointer=LeftPointer->next; } else { TempList->insertAtHead(RightPointer->value); RightPointer=RightPointer->next; } TempPointer=TempList->getHead(); while((LeftPointer!=NULL) && (RightPointer!=NULL)) { if(LeftPointer->value<=RightPointer->value) { TempPointer->next=LeftPointer; LeftPointer->prev=TempPointer; LeftPointer=LeftPointer->next; TempPointer=TempPointer->next; } else { TempPointer->next=RightPointer; RightPointer->prev=TempPointer; RightPointer=RightPointer->next; TempPointer=TempPointer->next; } } if(RightPointer==NULL) { TempPointer->next=LeftPointer; LeftPointer->prev=TempPointer; } else { TempPointer->next=RightPointer; RightPointer->prev=TempPointer; } return TempList; }
Increase the stack space that is reserved for your program, or remove recursion.
Could there be any other reason? Is there a fault in program.
Nope. Like cire said, you ran out of stack space, which is space the processor uses to preform operations.
I have changed my code and declared the Lists on heap but still it gives an error. The following is the divide part:

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 List* Merge(List *ParentList) { if((ParentList)->Length==1) { return ParentList; } List **LeftList=new List*; *LeftList=new List; List **RightList=new List*; *RightList=new List; unsigned long long Counter=0; unsigned long long MidPoint=ParentList->Length/2; ListItem *TempPointer=(ParentList)->head; //cout<insertAtTail(TempPointer->value); TempPointer=TempPointer->next; Counter++; // RightList->insertAtHead(TempPointer->value); // TempPointer=TempPointer->prev; // Counter--; } //cout<<(*LeftList)->Length<insertAtTail(TempPointer->value); TempPointer=TempPointer->next; } //cout<<(*RightList)->Length<
Topic archived. No new replies allowed.