: Hi all, i'm new to algorithms and to this forum but was wondering if
: anyone has an algorithm which uses a merge sort to sort three sorted
: sequences into one?
:
: Any help would be much appreciated. Thanks in advance
Here is a simplified algorithm to merge 3 sorted lists. It is not the merge sort, because that's a full sorting algorithm, not just a way to merge sorted lists.
outList MergeLists (inListArray) {
// Loop until all lists are empty
while (length inListArray > 0) {
int small = 0;
// First find smallest item in all the lists
for (i = 0 ; i < length inListArray; i++) {
if (inListArray[i][0] < inListArray[small][0])
small = i;
}
// Then add that item into the result list and remove it from the source list
move inListArray[small][0] to outList
// Finally check if that list becomes empty
if (length inListArray[small] = 0) {
remove inListArray[small] from inListArray
}
}
}
This code runs in O(n) time, where n is the number of total items.