Skip to content

Commit

Permalink
Refactor MergeSort
Browse files Browse the repository at this point in the history
  • Loading branch information
magaupp committed Oct 12, 2024
1 parent 8c67996 commit 85088f7
Showing 1 changed file with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class MergeSort<T extends Comparable> implements SortStrategy<T>
* @param high {number}
*/
function mergesort<T extends Comparable>(input: Array<T>, low: number, high: number) {
if (high - low < 1) {
if (low >= high) {
return;
}
const mid = Math.floor((low + high) / 2);
Expand Down Expand Up @@ -56,14 +56,11 @@ function merge<T extends Comparable>(input: Array<T>, low: number, middle: numbe
wholeIndex++;
}

if (leftIndex <= middle && rightIndex > high) {
while (leftIndex <= middle) {
temp[wholeIndex++] = input[leftIndex++];
}
} else {
while (rightIndex <= high) {
temp[wholeIndex++] = input[rightIndex++];
}
while (leftIndex <= middle) {
temp[wholeIndex++] = input[leftIndex++];
}
while (rightIndex <= high) {
temp[wholeIndex++] = input[rightIndex++];
}

for (wholeIndex = 0; wholeIndex < temp.length; wholeIndex++) {
Expand Down

0 comments on commit 85088f7

Please sign in to comment.