Thread: sorting - merge -

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Question sorting - merge -

    Program doesnt return to main after calling merge sort. Could someone point out why is that?
    Code:
    private:
    	int nNumItems;
    	string *pasWords;
    ....more code
    ....
    int MergeSort();
    int gMergeSort(string*pasWords, int nStart, int nEnd);
    int Merge(string*pasWords, int nStart, int nMidpoint, int nEnd);

    Code:
    int Wordlist::MergeSort()
    {
    	int nStart=0, nEnd=nNumItems-1;
    
    	if (pasWords == NULL)
    	{
    		cout << "Array is Empty..." << endl;
    		return 0;
    	}
    	gMergeSort(nStart, nEnd );
    	return 0;
    }
    
    
    int Wordlist::gMergeSort(int nStart, int nEnd)
    {
    	int nMidpoint=0;
    	
    	if (nEnd >= nStart+1)
    	{
    		nMidpoint = (nEnd-nStart)/2;					//Getting Midpoint
    		gMergeSort(nStart, nMidpoint);
    		gMergeSort(nMidpoint+1, nEnd);
    		Merge(nStart, nMidpoint, nEnd);
    	}
    	
    	return 0;
    }
    
    int Wordlist::Merge(int first, int mid, int last)
    {
    	int i,j,k;
    	string *temp;
    	temp = new string [(nNumItems-1)/2];  
    
    	i=0, k=first;
    	while(k<=mid)
    		temp[i++]=pasWords[k++];
    
    	i=0,j=mid+1,k=first;
    	for(;;)
    	{
    		if(temp[i]<= pasWords[j])
    		{
    			pasWords[k++]=temp[i++];
    			if(i>mid-first) return 0;
    		}
    		else
    		{
    			pasWords[k++]=pasWords[j++];
    			if(j>last) break;
    		}
    	}
    	while(i<=mid-first)
    		pasWords[k++]=temp[i++];
    
    	return 0;
    }
    after the red marked call to function, i tried putting a cout, but it just skips it and teriminates the program without going back to main.
    Last edited by dayknight; 02-01-2006 at 08:55 PM.
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663

    Talking

    after the red marked call to function, i tried putting a cout, but it just skips it and teriminates the program without going back to main.
    How does your program terminate? Does it get stuck in an infinite loop?

    I also think you could organize your program differently. All the sort functions can be called by your object in main() and then return to main() when they are done. main() is supposed to be the air traffic controller. Instead, you are calling a function from main(), then jumping from inside that function to another function, and then doing it again, which is a mess to troubleshoot.

    I envision something like this:

    Code:
    int main()
    {
    
        WordList myList;
        ....
        myList.MergeSort();
    	
        myList.gMergeSort(0); //the 2nd param nNumItems can be accessed from inside gMergeSort()
    	
        myList.Merge(1, 2, 3);
    Last edited by 7stud; 02-01-2006 at 09:24 PM.

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    normally... but it is not suppose to terminate.
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge Sort the best sorting algorithm?
    By sehr alt in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 06-20-2007, 02:19 PM
  2. Merge Module Problems
    By mercury529 in forum Windows Programming
    Replies: 0
    Last Post: 11-29-2006, 03:30 PM
  3. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM
  4. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  5. help with merge sort
    By maloy in forum C Programming
    Replies: 4
    Last Post: 03-04-2002, 06:22 PM