###### 在一个基本有序的数组中查询指定元素

arr=[1,2,3,4,5,7,6,8,9,10]$arr=[1,2,3,4,5,7,6,8,9,10]$

public class SearchAlmost {
// A recursive binary search based function. It returns index of x in given array arr[l..r] is present, otherwise -1
public static int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;

// If the element is present at one of the middle 3 positions
if (arr[mid] == x) return mid;
if (mid > l && arr[mid - 1] == x) return (mid - 1);
if (mid < r && arr[mid + 1] == x) return (mid + 1);

// If element is smaller than mid, then it can only be present in left subarray
if (arr[mid] > x) return binarySearch(arr, l, mid - 2, x);

// Else the element can only be present in right subarray
return binarySearch(arr, mid + 2, r, x);
}

// We reach here when element is not present in array
return -1;
}

public static void main(String args[]) {
int arr[] = {3, 2, 10, 4, 40};
int n = arr.length;
int x = 4;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element is not present in array");
else
System.out.println("Element is present at index " + result);
}
}

#### 在数组（序列、有序集合）中查找特定数组

2016-07-01 14:24:22

#### 数组排序的几种方式

2017-12-07 19:19:36

#### 行列有序的二维数组查找

2016-03-17 19:51:47

#### js实现数组去重的三个方法、数组的快速排序

2016-03-29 11:43:46

#### 6-2 在数组中查找指定元素（15 分）

2017-12-28 22:45:38

#### Problem  M 在数组中查找指定元素 函数

2017-11-28 09:24:29

#### 数组中查找元素的存在

2017-09-12 20:48:22

#### 数组---在数组中查找元素

2017-10-30 14:29:43

#### 数组中查找指定元素

2015-09-09 23:51:10

#### 在数组中快速查找指定元素

2018-03-17 19:38:10