Compare commits
2 Commits
7827051d82
...
35e5acb50f
| Author | SHA1 | Date |
|---|---|---|
|
|
35e5acb50f | |
|
|
4f478c08ac |
|
|
@ -1,5 +1,4 @@
|
||||||
package com.nbee.solution.practice;
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
class BinarySearch {
|
class BinarySearch {
|
||||||
public static int binarySearchBasic(int[] nums, int target){
|
public static int binarySearchBasic(int[] nums, int target){
|
||||||
int i = 0 ,j = nums.length - 1;//设置指针和初始值
|
int i = 0 ,j = nums.length - 1;//设置指针和初始值
|
||||||
|
|
@ -17,10 +16,28 @@ class BinarySearch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* 为什么必须是i<=j?
|
* 为什么必须是i<=j?
|
||||||
* 在二分搜索中,使用条件 i <= j 作为循环的继续条件是为了确保在数组只剩下一个元素时,仍然能够检查这个元素。这是因为在二分搜索中
|
* 在二分搜索中,使用条件 i <= j 作为循环的继续条件是为了确保在数组只剩下一个元素时,仍然能够检查这个元素。这是因为在二分搜索中
|
||||||
* 你每次都在缩小搜索范围,直到没有元素剩余。如果用 i < j 作为条件,当 i 和 j 相等时,这个位置的元素将不会被检查,可能导致漏掉目标元素。
|
* 你每次都在缩小搜索范围,直到没有元素剩余。如果用 i < j 作为条件,当 i 和 j 相等时,这个位置的元素将不会被检查,可能导致漏掉目标元素。
|
||||||
*/
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static int binarySearchAlternative(int[] nums, int target){
|
||||||
|
int i = 0 ,j = nums.length;
|
||||||
|
while (i < j){
|
||||||
|
int m = (i + j) >>> 1;
|
||||||
|
if (target < nums[m]){
|
||||||
|
j = m;
|
||||||
|
}
|
||||||
|
else if (nums[m] < target){
|
||||||
|
i = m + 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,14 @@ package com.nbee.solution.practice;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class BinarySearchTest {
|
public class BinarySearchTest {
|
||||||
|
private int[] nums = {1, 3, 5, 7, 9};
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBinarySearchBasic_Found() {
|
public void testBinarySearchBasic_Found() {
|
||||||
int[] nums = {1, 3, 5, 7, 9};
|
int[] nums = {1, 3, 5, 7, 9, 11, 13};
|
||||||
int target = 5;
|
int target = 5;
|
||||||
int result = BinarySearch.binarySearchBasic(nums, target);
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
Assert.assertEquals("Element should be found at index 2", 2, result);
|
Assert.assertEquals("Element should be found at index 2", 2, result);
|
||||||
|
|
@ -48,4 +50,38 @@ public class BinarySearchTest {
|
||||||
int result = BinarySearch.binarySearchBasic(nums, target);
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
Assert.assertEquals("Binary search should work on decreasing arrays", 2, result);
|
Assert.assertEquals("Binary search should work on decreasing arrays", 2, result);
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchAlternativeFound() {
|
||||||
|
// Test that the method correctly returns the index of an existing element
|
||||||
|
int target = 7;
|
||||||
|
int result = BinarySearch.binarySearchAlternative(nums, target);
|
||||||
|
assertEquals(3, result, "The target should be found at index 3");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchAlternativeNotFound() {
|
||||||
|
// Test that the method correctly returns -1 when the element is not present
|
||||||
|
int target = 4;
|
||||||
|
int result = BinarySearch.binarySearchAlternative(nums, target);
|
||||||
|
assertEquals(-1, result, "The target should not be found");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchAlternativeWithEmptyArray() {
|
||||||
|
// Test with an empty array
|
||||||
|
int[] emptyNums = new int[]{};
|
||||||
|
int target = 1;
|
||||||
|
int result = BinarySearch.binarySearchAlternative(emptyNums, target);
|
||||||
|
assertEquals(-1, result, "The target should not be found in an empty array");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchAlternativeWithSingleElement() {
|
||||||
|
// Test with a single element array
|
||||||
|
int[] singleNums = new int[]{5};
|
||||||
|
int target = 5;
|
||||||
|
int result = BinarySearch.binarySearchAlternative(singleNums, target);
|
||||||
|
assertEquals(0, result, "The target should be found at index 0 in a single-element array");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue