Thread: Help With Merging Sorted Lists of Strings

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    Unhappy Help With Merging Sorted Lists of Strings

    I am presently working on taking two lists of strings, sorting them, and then merging them into one list of strings that is already sorted without sorting the third list. So far this is the code that I have:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void qsort(int, int, string[]);
    
    int main()
    {
    	int i = 0, j = 0, k = 0;
    	string list1[15], list2[10], list3[25];
    	
    	cout << "\nThis program will take 2 unsorted lists and merge them into one sorted list.";
    	
    	for (i = 0; i < 15; i++)
    	{
    		cout << "\nEnter value for the number " << (i + 1) << " item of your first list.\n";
    		cin >> list1[i];
    	}
    
    	qsort (0, 14, list1);
    	cout << endl;
    
    	for (j = 0; j < 15; j++)
    	{
    		cout << list1[j];
    		cout << endl;
    	}
    
    	i = 0;
    	j = 0;
    
    	for (i = 0; i < 10; i++)
    	{
    		cout << "\nEnter value for the number " << (i + 1) << " item of your second list.\n";
    		cin >> list2[i];
    	}	
    	
    	qsort(0, 9, list2);
    	cout << endl;
    
    	for (j = 0; j < 10; j++)
    	{
    		cout << list2[j];
    		cout << endl;
    	}
    
    
    	return 0;
    }
    
    
    //Purpose:    Function to quickly sort a group of data.
    //Calls:      Recursive.
    //Globals:    None.
    
    void qsort(int first, int last, string list[])
    {
    	int middle, p, index;
    	string temp, partition;
    	if (first < last)
    	{
    		middle = int(first + last)/2;
    		temp = list[middle];
    		list[middle] = list[first];
    		list[first] = temp;
    		partition = list[first];
    		p = first;
    		for (index = first + 1; index <= last; index++)
    		{
    			if(list[index] < partition)
    			{
    				p = p + 1;
    				temp = list[index];
    				list[index] = list[p];
    				list[p] = temp;
    			}
    		}	
    		temp = list[first];
    		list[first] = list[p];
    		list[p] = temp;
    		qsort(first, p - 1, list);
    		qsort(p + 1, last, list);
    	}
    }
    I can input the two lists, and I have them sorted then printed out, but how would I go about merging them without having to sort them again?

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    heres an example of what i did, maybe you can check the logic of it, it works perfectly just a different setup.

    http://cboard.cprogramming.com/showthread.php?t=64249

    EDIT: my example is of type linear, currently working on a recursive function.
    Last edited by JoshR; 04-13-2005 at 11:31 PM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    Well, I got it all to work now, except for the fact that when one of the arrays runs out of items I get a run-time error because of falling out of bounds of the array. How do I account for that and just add the remainder of the array to the end of the third array?

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    well first you have to put it in a loop statement, that is true when the arrays arent done yet. so that it will exit when one or both arrays are done. after that you need to check which array is infact done, you can check that in the algorithm itself or after, once you establish which array is done, for the array that is not done, you must copy from the last point used, and place in the third array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. separating mixed list into sorted two lists
    By transgalactic2 in forum C Programming
    Replies: 24
    Last Post: 04-20-2009, 03:32 PM
  2. Merging two lists as one
    By Lone in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 03:59 PM
  3. Radix Sort, Strings, and Linked Lists
    By dark paladin in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 03:24 PM
  4. merging two linked lists..
    By Makimura in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2001, 12:34 PM
  5. Merging Linked Lists
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2001, 07:08 PM