## Algorithms

Moderators: None (Apply to moderate this forum)
Number of threads: 402
Number of posts: 786

This Forum Only

Re: Merge sort Posted by zibadian on 20 May 2007 at 8:38 AM
: 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.
lobbwill Merge sort on 20 May 2007 at 6:30 AM
zibadian Re: Merge sort on 20 May 2007 at 8:38 AM
lobbwill Re: Merge sort on 21 May 2007 at 3:16 PM

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic