Skip to content

Conversation

@dimitrijjedich
Copy link
Collaborator

@dimitrijjedich dimitrijjedich commented May 2, 2025

πŸ—οΈ Extend Mergesort by additional variant

πŸ—’οΈ General

The process of the 3-way Mergesort is the same as the "normal" mergesort. The idea ist just to split the array into one more part and by this increase it's efficiency.


πŸ’‘πŸ’¬πŸ’­ Implementation

Initially the implementation seemed easy. Just to the same as the first time implementing the algorithm and extend it by the handling of an addtional array. So of i went:

πŸ› οΈ Refactoring

First of all the class needed to be refactored to allow multiple variants of the sorting algorithm. This was done with the same principal as the other algorithms, by extending the sort method by an additional parameter that allows to call different variants dynamic. The initial implementation was moved into a new method variant1 and was set as the default of the parameter.

πŸ”© Implementation

Now the fun part begun and a new method was added for the new implementation named variant2. And initially the methods looks really similar. We devide the provided array and call the method recursivly with the seperated array and use a check of the length agains <=0 as termination condition.

🀲 Merge

The merging on the other hand now has to be extended. Before we just had to check the two values provided. Now there are three that need to be checked which requires a few more conditions.

Addtionally we now have three conditions that have to be met while looping over the different arrays and have to concatinate the rest with additional logic instead of just appending the remaining elements to the results array.

Adjusting

Since the code kept growing i decided to more or less scrap the current state and make it dynamic. This approach would, when done correctly reduce the code needed further.

Changing the signature

The first thing that needed to be adjusted is the methods definition. Instead of providing three arrays, the parameter was switched to a single array that contains the arrays and therefore is iterable.

@dimitrijjedich dimitrijjedich self-assigned this May 2, 2025
@sonarqubecloud
Copy link

sonarqubecloud bot commented May 2, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants