在有序数组c ++中进行递归二进制搜索

时间:2016-01-07 19:55:40

标签: c++ arrays algorithm recursion binary-search

我必须编写一个递归函数来搜索已排序的数组。

extension ViewController: CBPeripheralDelegate {

func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {

  if error != nil {
    print("Error connecting to peripheral: \(error?.localizedDescription)")
    return
  }
}

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
print("Peripheral connected.")

  peripheral.delegate = self
  peripheral.discoverServices(nil)
}

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {

  if error != nil {
    print("Error discovering services \(error?.localizedDescription)")
    return
  }

  for service: CBService in peripheral.services! {
    peripheral.discoverCharacteristics(nil, forService: service)
  }
}

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {

  if error != nil {
    print("Error discovering characteristics \(error?.localizedDescription)")
    return
  }

  for characteristic: CBCharacteristic in service.characteristics! {
    if characteristic.UUID == CBUUID(string: YOUR_CHARACTERISTIC_UUID) {
      peripheral.readValueForCharacteristic(characteristic)
      // for some devices, you can skip readValue() and print the value here
    }
  }
}

func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {

   if characteristic.UUID == CBUUID(string: YOUR_CHARACTERISTIC_UUID) {
      print(characteristic.value)
    }       
}

}

这是给我们的代码的一部分,我们必须完成它 如果你有4个变量,我知道怎么做。 (分和最大) 但我只有三个,有没有办法编辑给下一个函数的数组的起点?

5 个答案:

答案 0 :(得分:3)

假设你有四个param版本:

find(value, array, min, max);

您可以调用三参数版本:

find(value, array + min, max);

请注意,数组的max元素实际上是max-min的{​​{1}}个元素,因此根据您的实现情况,您可能需要调用

array + min

答案 1 :(得分:2)

该功能可以按以下方式编写

#include <iostream>

int find( int value, int* folge, int max ) 
{
    if ( max == 0 ) return -1;

    int middle = max / 2;

    if ( folge[middle] == value ) return middle;

    bool lower = value < folge[middle];
    int n = lower ? find( value, folge, middle ) 
                  : find( value, folge + middle + 1, max - middle - 1 );

    return n != -1 && !lower ? n + middle + 1: n; 
}

int main()
{
    int const n = 7;
    int wert1 = 4, wert2 = 13, wert3 = 2, wert4 = 25;
    int folge[n] = {3,4,9,13,13,17,22};

    std::cout << wert1 << " = " << find(wert1, folge, n) << std::endl;
    std::cout << wert2 << " = " << find(wert2, folge, n) << std::endl;
    std::cout << wert3 << " = " << find(wert3, folge, n) << std::endl;
    std::cout << wert4 << " = " << find(wert4, folge, n) << std::endl;

    return 0;
}

程序输出

4 = 1
13 = 3
2 = -1
25 = -1

或另外一个测试程序

#include <iostream>

int find( int value, int* folge, int max ) 
{
    if ( max == 0 ) return -1;

    int middle = max / 2;

    if ( folge[middle] == value ) return middle;

    bool lower = value < folge[middle];
    int n = lower ? find( value, folge, middle ) 
                                  : find( value, folge + middle + 1, max - middle - 1 );

    return n != -1 && !lower ? n + middle + 1: n; 
}

int main()
{
    int const n = 7;
    int folge[n] = {3,4,9,13,13,17,22};

    for ( int x : { 2, 3, 4, 9, 13, 17, 22, 25 } )
    {
        std::cout << x << " -> " << find( x , folge, n ) << std::endl;  
    }        

    return 0;
}

它的输出是

2 -> -1
3 -> 0
4 -> 1
9 -> 2
13 -> 3
17 -> 5
22 -> 6
25 -> -1

答案 2 :(得分:1)

find( &folge[1],...) // ignore first element

第n个元素的地址是Size-n

大小的数组

答案 3 :(得分:0)

您可以更改int指针folge

的值
int find(int value, int* folge, int max) {
   if (max == 0) {
        return -1;
   }

   int mid=max/2;

   if (value == folge[mid])
   {
        return mid;
   }

   int base=0;

   if (value < folge[mid])
   {
       max=mid-1;
   }
   else
   {
       max-=(mid+1);
       base=mid+1;
   }

   int ret=find(value,folge+base,max);

   if (ret == -1)
   {
       return -1;
   }

   return ret+base;
}

答案 4 :(得分:0)

如果a this is a a b this is a b c this is a c d this is a d e this is a e 指向第一个元素,则folge将指向第二个元素。

folge+1