Compare commits
2 Commits
35e5acb50f
...
b5059f6996
| Author | SHA1 | Date |
|---|---|---|
|
|
b5059f6996 | |
|
|
022850f76c |
|
|
@ -1,4 +1,7 @@
|
||||||
package com.nbee.solution.practice;
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
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;//设置指针和初始值
|
||||||
|
|
@ -7,11 +10,9 @@ class BinarySearch {
|
||||||
int m = (i + j) >>> 1; //按位右移补零操作符。相当于除2,解决溢出问题
|
int m = (i + j) >>> 1; //按位右移补零操作符。相当于除2,解决溢出问题
|
||||||
if (target < nums[m]) {//如果中间值大于目标值,则将右指针向左移动
|
if (target < nums[m]) {//如果中间值大于目标值,则将右指针向左移动
|
||||||
j = m - 1;
|
j = m - 1;
|
||||||
}
|
} else if (nums[m] < target) {//如果中间值小于目标值,则将左指针向右移动
|
||||||
else if (nums[m] < target){//如果中间值小于目标值,则将左指针向右移动
|
|
||||||
i = m + 1;
|
i = m + 1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -30,14 +31,63 @@ class BinarySearch {
|
||||||
int m = (i + j) >>> 1;
|
int m = (i + j) >>> 1;
|
||||||
if (target < nums[m]) {
|
if (target < nums[m]) {
|
||||||
j = m;
|
j = m;
|
||||||
}
|
} else if (nums[m] < target) {
|
||||||
else if (nums[m] < target){
|
|
||||||
i = m + 1;
|
i = m + 1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int binarySearch01(int[] nums, int target) {
|
||||||
|
int i = 0, j = nums.length;
|
||||||
|
while (1 < j - i) {
|
||||||
|
int m = (i + j) >>> 1;
|
||||||
|
if (target < nums[m]) {
|
||||||
|
j = m;
|
||||||
|
} else {
|
||||||
|
i = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nums[i] == target) {
|
||||||
|
return i;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int binarySearchLeftMost(int[] nums, int target) {
|
||||||
|
int i = 0, j = nums.length - 1;
|
||||||
|
int candidate = -1;
|
||||||
|
while (i <= j) {
|
||||||
|
int m = (i + j) >>> 1;
|
||||||
|
if (target < nums[m]) {
|
||||||
|
j = m - 1;
|
||||||
|
} else if (nums[m] < target) {
|
||||||
|
i = m + 1;
|
||||||
|
} else {
|
||||||
|
candidate = m;
|
||||||
|
j = m - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int binarySearchRightMost(int[] nums, int target) {
|
||||||
|
int i = 0, j = nums.length - 1;
|
||||||
|
int candidate = -1;
|
||||||
|
while (i <= j) {
|
||||||
|
int m = (i + j) >>> 1;
|
||||||
|
if (target < nums[m]) {
|
||||||
|
j = m - 1;
|
||||||
|
} else if (nums[m] < target) {
|
||||||
|
i = m + 1;
|
||||||
|
} else {
|
||||||
|
candidate = m;
|
||||||
|
i = m + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,10 @@ package com.nbee.solution.practice;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class BinarySearchTest {
|
public class BinarySearchTest {
|
||||||
|
|
@ -50,6 +54,7 @@ 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
|
@Test
|
||||||
public void testBinarySearchAlternativeFound() {
|
public void testBinarySearchAlternativeFound() {
|
||||||
// Test that the method correctly returns the index of an existing element
|
// Test that the method correctly returns the index of an existing element
|
||||||
|
|
@ -84,4 +89,41 @@ public class BinarySearchTest {
|
||||||
assertEquals(0, result, "The target should be found at index 0 in a single-element array");
|
assertEquals(0, result, "The target should be found at index 0 in a single-element array");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test Binary Search Java")
|
||||||
|
public void testBinarySearchJava() {
|
||||||
|
int[] nums = {1, 3, 5, 7, 9};
|
||||||
|
int target = 4;
|
||||||
|
int result = Arrays.binarySearch(nums, target);
|
||||||
|
if (result < 0) {
|
||||||
|
int insertIndex = Math.abs(result + 1);
|
||||||
|
System.out.printf("nums: " + Arrays.toString(nums) + "\n");
|
||||||
|
System.out.printf("insertIndex: " + insertIndex + "\n");
|
||||||
|
int[] newNums = new int[nums.length + 1];
|
||||||
|
//第一次拷贝
|
||||||
|
System.arraycopy(nums, 0, newNums, 0, nums.length);
|
||||||
|
System.out.printf("newNums: " + Arrays.toString(newNums) + "\n");
|
||||||
|
newNums[insertIndex] = target;
|
||||||
|
// 第二次拷贝
|
||||||
|
System.arraycopy(nums, insertIndex, newNums, insertIndex + 1, nums.length - insertIndex);
|
||||||
|
System.out.printf("newNums: " + Arrays.toString(newNums) + "\n");
|
||||||
|
}
|
||||||
|
System.out.printf("result: " + result);
|
||||||
|
assertEquals(-3, result, "The target should not be found");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchLeftMost() {
|
||||||
|
int[] nums = new int[]{1, 3, 3, 5, 7};
|
||||||
|
assertEquals(0, BinarySearch.binarySearchLeftMost(nums, 1));
|
||||||
|
assertEquals(1, BinarySearch.binarySearchLeftMost(nums, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchRightMost() {
|
||||||
|
int[] nums = new int[]{1, 3, 3, 5, 5, 5, 5, 5, 7};
|
||||||
|
assertEquals(0, BinarySearch.binarySearchRightMost(nums, 1));
|
||||||
|
assertEquals(2, BinarySearch.binarySearchRightMost(nums, 3));
|
||||||
|
assertEquals(7, BinarySearch.binarySearchRightMost(nums, 5));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue