Search code examples
arraysquicksortmergesort

3 partition mergesort


My professor assigned my class with implementing mergesort in arrays with 3-part partitioning and merging.

That was the exact question from the professor. Problem is I have found no such thing as a 3-way mergesort I only know of a 3-way quicksort so I thought that he probably meant to take an array, split it into 3 parts and then mergesort those 3 parts together and I'm doing this by mergesorting the first 2 parts together and then mergesorting the combined part with the 3rd part.

Did I think correctly and did I do the right thing (already implemented but I'm not posting the code since it doesn't have anything to do with my question) or have I understood wrong and there is something like a 3-way mergesort that I am not aware of.

The professor tends to give us assignments that have to do with stuff that we have not learned yet that's why I'm so sceptical about this and I looked as much as I could in Google etc.


Solution

  • Once you mergesorted the three sub arrays, you can merge them together in one go: compare the first elements of all three and place the smallest into the combined array, then take the next element from the array you took the smallest from and do the compariosn again until all subarray elements are accounted for.

    Make sure you hande the case where there are only two elements in the array (so you cannot partition it into three non-empty parts). Also, in the above paragraph, one array will be empty before the others when merging, so you need to account for that too.