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?