✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.
Suponha uma classe SomeClass onde está definida uma variável \tiny a[size]
1 template<int size=10>
2 class SomeClass {
3 public:
4 ...
5 int binSearch(int key);
6 private:
7 int length;
8 int a[size];
9 };
Considere o seguinte método binSearch() dessa classe
11 // Efectua uma pesquisa binária por uma chave numa array.
12 // Pré-condição: A array a[0]...a[a.length-1] está ordenada
13 // por ordem ascendente.
14 // Pós-condição: Devolve index tal que a[index] == key.
15 // Se key não está em v, então devolve -1.
16
17 template<int size>
18 int SomeClass<size>::binSearch(int key)
19 {
20 int low = 0;
21 int high = length - 1;
22 while (low <= high)
23 {
24 int mid = (low + high)/2;
25 if (a[mid] == key)
26 return mid;
27 elseif (a[mid] < key)
28 low = mid + 1;
29 else
30 high = mid - 1;
31 }
32 return -1;
33 }
A pesquisa binária será efectuada na seguinte lista
O que é que ficará armazenado em y depois de executado o seguinte?
32 int y = binSearch(4);