-
Notifications
You must be signed in to change notification settings - Fork 21.1k
refactor: MergeSortNoExtraSpace
#5277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vil02
merged 8 commits into
TheAlgorithms:master
from
alxkm:refactor/merge_sort_no_extra_stace
Jul 9, 2024
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f361f7d
refactor: MergeSortNoExtraSpace, change naming, adding test
089b88f
Merge branch 'master' into refactor/merge_sort_no_extra_stace
alxkm 6c73093
checkstyle: fix import ordering, and formatting
e3f2910
Merge remote-tracking branch 'origin/refactor/merge_sort_no_extra_sta…
19a2954
Merge branch 'master' into refactor/merge_sort_no_extra_stace
alxkm 06b026e
fix: adding negative numbers check, fix possible overflow
f5b6eb7
Merge remote-tracking branch 'origin/refactor/merge_sort_no_extra_sta…
c80a67e
checkstyle: remove newline
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 48 additions & 38 deletions
86
src/main/java/com/thealgorithms/sorts/MergeSortNoExtraSpace.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,73 +1,83 @@ | ||
| package com.thealgorithms.sorts; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Scanner; | ||
|
|
||
| /*This code implements the mergeSort algorithm without extra space | ||
| For understanding about mergesort visit :https://www.geeksforgeeks.org/merge-sort/ | ||
| /** | ||
| * Implementation of Merge Sort without using extra space for merging. | ||
| * This implementation performs in-place merging to sort the array of integers. | ||
| */ | ||
| public final class MergeSortNoExtraSpace { | ||
| private MergeSortNoExtraSpace() { | ||
| } | ||
|
|
||
| public static void callMergeSort(int[] a, int n) { | ||
| int maxele = Arrays.stream(a).max().getAsInt() + 1; | ||
| mergeSort(a, 0, n - 1, maxele); | ||
| /** | ||
| * Sorts the array using in-place merge sort algorithm. | ||
| * | ||
| * @param array the array to be sorted | ||
| * @return the sorted array | ||
| */ | ||
| public static int[] sort(int[] array) { | ||
| if (array.length == 0) { | ||
| return array; | ||
| } | ||
| int maxElement = Arrays.stream(array).max().getAsInt() + 1; | ||
|
alxkm marked this conversation as resolved.
Outdated
|
||
| mergeSort(array, 0, array.length - 1, maxElement); | ||
| return array; | ||
| } | ||
|
|
||
| public static void mergeSort(int[] a, int start, int end, int maxele) { // this function divides the array into 2 halves | ||
| /** | ||
| * Recursively divides the array into two halves, sorts and merges them. | ||
| * | ||
| * @param array the array to be sorted | ||
| * @param start the starting index of the array | ||
| * @param end the ending index of the array | ||
| * @param maxElement the value greater than any element in the array, used for encoding | ||
| */ | ||
| public static void mergeSort(int[] array, int start, int end, int maxElement) { | ||
| if (start < end) { | ||
| int mid = (start + end) / 2; | ||
| mergeSort(a, start, mid, maxele); | ||
| mergeSort(a, mid + 1, end, maxele); | ||
| implementMergeSort(a, start, mid, end, maxele); | ||
| int middle = (start + end) / 2; | ||
|
alxkm marked this conversation as resolved.
Outdated
|
||
| mergeSort(array, start, middle, maxElement); | ||
| mergeSort(array, middle + 1, end, maxElement); | ||
| merge(array, start, middle, end, maxElement); | ||
| } | ||
| } | ||
|
|
||
| public static void implementMergeSort(int[] a, int start, int mid, int end, | ||
| int maxele) { // implementation of mergesort | ||
| /** | ||
| * Merges two sorted subarrays [start...middle] and [middle+1...end] in place. | ||
| * | ||
| * @param array the array containing the subarrays to be merged | ||
| * @param start the starting index of the first subarray | ||
| * @param middle the ending index of the first subarray and starting index of the second subarray | ||
| * @param end the ending index of the second subarray | ||
| * @param maxElement the value greater than any element in the array, used for encoding | ||
| */ | ||
| private static void merge(int[] array, int start, int middle, int end, int maxElement) { | ||
| int i = start; | ||
| int j = mid + 1; | ||
| int j = middle + 1; | ||
| int k = start; | ||
| while (i <= mid && j <= end) { | ||
| if (a[i] % maxele <= a[j] % maxele) { | ||
| a[k] = a[k] + (a[i] % maxele) * maxele; | ||
| while (i <= middle && j <= end) { | ||
| if (array[i] % maxElement <= array[j] % maxElement) { | ||
| array[k] = array[k] + (array[i] % maxElement) * maxElement; | ||
| k++; | ||
| i++; | ||
| } else { | ||
| a[k] = a[k] + (a[j] % maxele) * maxele; | ||
| array[k] = array[k] + (array[j] % maxElement) * maxElement; | ||
| k++; | ||
| j++; | ||
| } | ||
| } | ||
| while (i <= mid) { | ||
| a[k] = a[k] + (a[i] % maxele) * maxele; | ||
| while (i <= middle) { | ||
| array[k] = array[k] + (array[i] % maxElement) * maxElement; | ||
| k++; | ||
| i++; | ||
| } | ||
| while (j <= end) { | ||
| a[k] = a[k] + (a[j] % maxele) * maxele; | ||
| array[k] = array[k] + (array[j] % maxElement) * maxElement; | ||
| k++; | ||
| j++; | ||
| } | ||
| for (i = start; i <= end; i++) { | ||
| a[i] = a[i] / maxele; | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner inp = new Scanner(System.in); | ||
| System.out.println("Enter array size"); | ||
| int n = inp.nextInt(); | ||
| int[] a = new int[n]; | ||
| System.out.println("Enter array elements"); | ||
| for (int i = 0; i < n; i++) { | ||
| a[i] = inp.nextInt(); | ||
| } | ||
| callMergeSort(a, n); | ||
| for (int i = 0; i < a.length; i++) { | ||
| System.out.print(a[i] + " "); | ||
| array[i] = array[i] / maxElement; | ||
| } | ||
| inp.close(); | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
src/test/java/com/thealgorithms/sorts/MergeSortNoExtraSpaceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.thealgorithms.sorts; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
|
|
||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| public class MergeSortNoExtraSpaceTest { | ||
| record TestCase(int[] inputArray, int[] expectedArray) { | ||
| } | ||
|
|
||
| static Stream<TestCase> provideTestCases() { | ||
| return Stream.of(new TestCase(new int[] {}, new int[] {}), new TestCase(new int[] {1}, new int[] {1}), new TestCase(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), new TestCase(new int[] {5, 4, 3, 2, 1}, new int[] {1, 2, 3, 4, 5}), | ||
| new TestCase(new int[] {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}, new int[] {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}), new TestCase(new int[] {4, 2, 4, 3, 2, 1, 5}, new int[] {1, 2, 2, 3, 4, 4, 5}), new TestCase(new int[] {0, 0, 0, 0}, new int[] {0, 0, 0, 0}), | ||
| new TestCase(new int[] {1000, 500, 100, 50, 10, 5, 1}, new int[] {1, 5, 10, 50, 100, 500, 1000}), new TestCase(new int[] {1, 2, 3, 1, 2, 3, 1, 2, 3}, new int[] {1, 1, 1, 2, 2, 2, 3, 3, 3}), | ||
| new TestCase(new int[] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), new TestCase(new int[] {}, new int[] {}), new TestCase(new int[] {1}, new int[] {1}), new TestCase(new int[] {2, 1}, new int[] {1, 2}), | ||
| new TestCase(new int[] {1, 3, 2}, new int[] {1, 2, 3})); | ||
| } | ||
|
alxkm marked this conversation as resolved.
|
||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("provideTestCases") | ||
| public void testCountingSort(TestCase testCase) { | ||
| int[] outputArray = MergeSortNoExtraSpace.sort(testCase.inputArray); | ||
| assertArrayEquals(testCase.expectedArray, outputArray); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.