二进制搜索,如果数组包含重复项

时间:2012-03-14 13:06:05

标签: duplicates binary-search

您好,

如果我们使用二进制搜索在以下数组中搜索24,那么搜索关键字的索引是什么。

array = [10,20,21,24,24,24,24,24,30,40,45]

我对二进制搜索有疑问,如果数组有重复值,它是如何工作的。任何人都可以澄清......

5 个答案:

答案 0 :(得分:6)

您建议的数组在中间索引中具有目标值,并且在最有效的实现中,将在第一个递归级别之前返回此值。此实现将返回'5'(中间索引)。

要理解算法,只需在调试器中单步调试代码即可。

public class BinarySearch {
    public static int binarySearch(int[] array, int value, int left, int right) {
          if (left > right)
                return -1;
          int middle = left + (right-left) / 2;
          if (array[middle] == value)
                return middle;
          else if (array[middle] > value)
                return binarySearch(array, value, left, middle - 1);
          else
                return binarySearch(array, value, middle + 1, right);           
    }

    public static void main(String[] args) {
        int[] data = new int[] {10,20,21,24,24,24,24,24,30,40,45};

        System.out.println(binarySearch(data, 24, 0, data.length - 1));
    }
}

答案 1 :(得分:2)

正如@Pleepleus所指出的,它将从第一级递归本身返回索引5。但是我想指出一些关于二进制搜索的事情:

  1. 请使用void MainFormLoad(object sender, EventArgs e) { MessageBox.Show(AdhesionCharting.Properties.MySettings.Default.s_Username); }
  2. ,而不是使用mid = (left + right)/2
  3. 如果您要搜索元素的mid = left + (right-left)/2lower_bound,请使用以下算法:

    upper_bound

答案 2 :(得分:1)

为完整起见,这里有一个非递归版本的打字稿示例(二进制运算符用于对整数执行操作,而不是浮点运算)该示例很容易转换为其他类似C的语言:

res.json()

使用方法如下:

function binarySearch(array: number[], query: number): [number, number] {
    let from: number;
    let till: number;

    let mid = 0 | 0;
    let min = 0 | 0;
    let max = array.length - 1 | 0;

    while (min < max) {
        mid = (min + max) >>> 1;

        if (array[mid] < query) {
            min = mid + 1 | 0;
        } else {
            max = mid - 1 | 0;
        }
    }

    mid = min;
    min--;
    max++;

    from = array[mid] < query ? (array[max] === query ? max : mid) : (array[mid] === query ? mid : min);

    min = 0 | 0;
    max = array.length - 1 | 0;

    while (min < max) {
        mid = (min + max) >>> 1;

        if (query < array[mid]) {
            max = mid - 1 | 0;
        } else {
            min = mid + 1 | 0;
        }
    }

    mid = min;
    min--;
    max++;

    till = array[mid] > query ? (array[min] === query ? min : mid) : (array[mid] === query ? mid : max);

    return [from, till];
}

答案 3 :(得分:0)

public class a{
    public static int binarySearch(int[] array, int value, int left, int right) {
          if (left > right)
                return -1;
          int middle = (left + right) / 2;
          if (array[middle] == value)
        {
            if(array[middle-1]<array[middle])
                return middle;
                 //return binarySearch(array, value, left, middle - 1);
                 else
                return binarySearch(array, value, left, middle - 1);
        }
          else if (array[middle] > value)
                return binarySearch(array, value, left, middle - 1);
          else
                return binarySearch(array, value, middle + 1, right);           
    }
public static int binarySearch1(int[] array, int value, int left, int right) {
          if (left > right)
                return -1;
          int middle = (left + right) / 2;
          if (array[middle] == value)
        {
            if(array[middle]<array[middle+1])
                return middle; 
                 else

                    return binarySearch1(array, value, middle + 1, right);           
        }
          else if (array[middle] > value)
                return binarySearch1(array, value, left, middle - 1);
          else
                return binarySearch1(array, value, middle + 1, right);           
    }

    public static void main(String[] args) {
        int[] data = new int[] {10,20,21,24,24,24,24,24,30,40,45};


        System.out.println(binarySearch(data, 24, 0, data.length - 1));     //First Index
        System.out.println(binarySearch1(data, 24, 0, data.length - 1));    //Last Index
    }
}

答案 4 :(得分:0)

它既可以用于唯一数组,也可以用于非唯一数组。

def binary_search(n,s):
  search = s
  if len(n) < 1:
    return "{} is not in array".format(search)
  if len(n) == 1 and n[0] != s:
    return "{} is not in array".format(search)
  
  mid = len(n)//2
  ele = n[mid]
  if search == ele:
    return "{} is in array".format(search)
  elif search > ele:
    return binary_search(n[mid:],search)
  else:
    return binary_search(n[:mid],search)