-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Add MedianOfTwoSortedArrays.java
new algorithm
#5554
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
siriak
merged 20 commits into
TheAlgorithms:master
from
Hardvan:median_sorted_arrays_new_algo
Oct 9, 2024
Merged
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
fffa253
Add `MedianOfTwoSortedArrays.java` new algorithm
Hardvan e53804a
Update directory
Hardvan 03ef6c4
Fix clang errors
Hardvan da26600
Merge branch 'median_sorted_arrays_new_algo' of https://github.com/Ha…
Hardvan 6be0d1d
Fix test
Hardvan 41fbd46
Fix test
Hardvan ac398d3
Add private constructor
Hardvan f2cf9bf
Declare class as final
Hardvan 3d362ef
Fix
Hardvan 4ce5b55
Merge branch 'master' into median_sorted_arrays_new_algo
Hardvan 9fac92a
Update directory
Hardvan f39737e
Merge branch 'master' into median_sorted_arrays_new_algo
Hardvan a15a53a
Update directory
Hardvan 100d592
Refactor to Parameterized tests
Hardvan 96b67b9
Merge remote-tracking branch 'origin/median_sorted_arrays_new_algo' i…
Hardvan e6c1f7b
Fix
Hardvan b08ceea
Fix
Hardvan 654b635
Fix
Hardvan 1a8aeda
Merge branch 'master' into median_sorted_arrays_new_algo
siriak a3a2237
Update directory
siriak 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
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
53 changes: 53 additions & 0 deletions
53
src/main/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArrays.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,53 @@ | ||
package com.thealgorithms.divideandconquer; | ||
|
||
public final class MedianOfTwoSortedArrays { | ||
|
||
private MedianOfTwoSortedArrays() { | ||
} | ||
|
||
/** | ||
* Finds the median of two sorted arrays in logarithmic time. | ||
* | ||
* @param nums1 the first sorted array | ||
* @param nums2 the second sorted array | ||
* @return the median of the combined sorted array | ||
* @throws IllegalArgumentException if the input arrays are not sorted | ||
*/ | ||
public static double findMedianSortedArrays(int[] nums1, int[] nums2) { | ||
if (nums1.length > nums2.length) { | ||
return findMedianSortedArrays(nums2, nums1); // Ensure nums1 is the smaller array | ||
} | ||
|
||
int m = nums1.length; | ||
int n = nums2.length; | ||
int low = 0; | ||
int high = m; | ||
while (low <= high) { | ||
int partition1 = (low + high) / 2; // Partition in the first array | ||
int partition2 = (m + n + 1) / 2 - partition1; // Partition in the second array | ||
|
||
int maxLeft1 = (partition1 == 0) ? Integer.MIN_VALUE : nums1[partition1 - 1]; | ||
int minRight1 = (partition1 == m) ? Integer.MAX_VALUE : nums1[partition1]; | ||
int maxLeft2 = (partition2 == 0) ? Integer.MIN_VALUE : nums2[partition2 - 1]; | ||
int minRight2 = (partition2 == n) ? Integer.MAX_VALUE : nums2[partition2]; | ||
|
||
// Check if partition is valid | ||
if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) { | ||
// If combined array length is odd | ||
if (((m + n) & 1) == 1) { | ||
return Math.max(maxLeft1, maxLeft2); | ||
} | ||
// If combined array length is even | ||
else { | ||
return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2.0; | ||
} | ||
} else if (maxLeft1 > minRight2) { | ||
high = partition1 - 1; | ||
} else { | ||
low = partition1 + 1; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException("Input arrays are not sorted"); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArraysTest.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,32 @@ | ||
package com.thealgorithms.divideandconquer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class MedianOfTwoSortedArraysTest { | ||
|
||
@Test | ||
void testFindMedianSortedArrays() { | ||
// Test case 1: Arrays of equal length | ||
assertEquals(2.5, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {1, 3}, new int[] {2, 4})); | ||
|
||
// Test case 2: Arrays of different lengths | ||
assertEquals(2.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {1, 3}, new int[] {2})); | ||
|
||
// Test case 3: Arrays with even total length | ||
assertEquals(4.5, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {1, 2, 8}, new int[] {3, 4, 5, 6, 7})); | ||
|
||
// Test case 4: Arrays with odd total length | ||
assertEquals(3.5, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {1, 2, 8}, new int[] {3, 4, 5})); | ||
|
||
// Test case 5: Single element arrays | ||
assertEquals(2.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {1}, new int[] {3})); | ||
|
||
// Test case 6: Empty arrays | ||
assertEquals(0.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {}, new int[] {0})); | ||
|
||
// Test case 7: Same element arrays | ||
assertEquals(2.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {2, 2, 2}, new int[] {2, 2, 2})); | ||
} | ||
} |
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.