Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Busca em Vetores
Esta aula introduz a busca em
vetores que est entre as
tarefas mais freqentemente
encontradas em programao
de computadores
Sero abordados dois tipos
de busca: linear (ou
seqencial) e binria
Busca
Exemplo
int a[N];
45
56
12
43
95
19
67
Exemplo
45
56
12
43
95
19
67
Depende da implementao!
Pode retornar i = -1 (ou outro valor) indicativo
que a busca no teve xito
Algoritmo:
i = 0;
while (i < N && a[i] != x)
i++;
Ao trmino do lao:
Se i == N ento x no foi encontrado
seno a[i] == x, i a posio onde x foi encontrado
Busca de x = 19
i = 0;
Busca de x = 19
i = 0;
V
V
while (i < N && a[i] != x)
i++;
45
56
12
43
95
19
67
45
56
12
43
95
19
67
Busca de x = 19
i = 0;
Busca de x = 19
i = 0;
V
V
while (i < N && a[i] != x)
i++;
45
56
12
43
95
19
67
45
56
12
43
95
19
67
10
Busca de x = 19
i = 0;
Busca de x = 19
i = 0;
V
V
while (i < N && a[i] != x)
i++;
45
56
12
43
95
19
67
45
56
12
43
95
19
67
11
12
Busca de x = 19
i = 0;
Busca de x = 19
i = 0;
V
V
while (i < N && a[i] != x)
i++;
45
56
12
43
95
19
67
45
56
12
43
95
19
67
13
14
Busca de x = 19
i = 0;
Busca de x = 19
i = 0;
V
V
while (i < N && a[i] != x)
i++;
45
56
12
43
95
19
67
45
56
12
43
95
19
67
15
16
Busca de x = 19
i = 0;
Busca de x = 19
i = 0;
V
F
while (i < N && a[i] != x)
i++;
45
56
12
43
95
19
67
45
56
12
43
95
19
67
17
18
Exemplo em C++
Busca de x = 19
i = 0;
#include <iostream>
using namespace std;
int busca_sequencial(int x, int N, int a[])
{ int i = 0;
while (i < N && a[i] != x)
i++;
return (i == N) ? -1 : i;
Busca
Busca
Busca
Busca
45
56
12
43
95
19
67
de
de
de
de
19
45
8
81
=
=
=
=
5
0
6
-1
int main(void)
{ const int m = 8;
int v[m] = {45,56,12,43,95,19,8,67};
cout << "Busca
cout << "Busca
cout << "Busca
cout << "Busca
return 0;
de
de
de
de
19
45
8
81
=
=
=
=
"
"
"
"
<<
<<
<<
<<
}
19
20
21
22
Busca de x = 56
Algoritmo:
item a[N+1];
i = 0;
a[N] = x;
// sentinela
while (a[i] != x)
i++;
Ao final do lao, i == N implica que x no foi
encontrado (exceto o correspondente
sentinela).
i = 0;
a[N] = x;
while (a[i] != x)
i++;
N
45
56
12
43
95
19
67
23
24
Busca de x = 56
Busca de x = 56
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
V
while (a[i] != x)
i++;
45
56
12
43
95
19
67
56
45
56
12
43
95
19
67
56
Sentinela
25
Busca de x = 56
26
Busca de x = 56
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
F
while (a[i] != x)
i++;
45
56
12
43
95
19
67
56
45
56
12
43
95
19
67
56
27
Busca de x = 56
28
Busca de x = 81
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
while (a[i] != x)
i++;
45
56
12
43
95
19
67
56
45
56
12
43
95
19
67
30
Busca de x = 81
Busca de x = 81
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
V
while (a[i] != x)
i++;
45
56
12
43
95
19
67
81
45
56
12
43
95
19
67
81
Sentinela
31
Busca de x = 81
32
Busca de x = 81
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
V
while (a[i] != x)
i++;
45
56
12
43
95
19
67
81
45
56
12
43
95
19
67
81
33
Busca de x = 81
34
Busca de x = 81
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
V
while (a[i] != x)
i++;
45
56
12
43
95
19
67
81
45
56
12
43
95
19
67
81
35
36
Busca de x = 81
Busca de x = 81
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
V
while (a[i] != x)
i++;
45
56
12
43
95
19
67
81
45
56
12
43
95
19
67
81
37
Busca de x = 81
38
Busca de x = 81
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
V
while (a[i] != x)
i++;
45
56
12
43
95
19
67
81
45
56
12
43
95
19
67
81
39
Busca de x = 81
40
Busca de x = 81
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
V
while (a[i] != x)
i++;
45
56
12
43
95
19
67
81
45
56
12
43
95
19
67
81
41
42
Busca de x = 81
Busca de x = 81
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
V
while (a[i] != x)
i++;
45
56
12
43
95
19
67
81
45
56
12
43
95
19
67
81
43
Busca de x = 81
44
Busca de x = 81
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
V
while (a[i] != x)
i++;
45
56
12
43
95
19
67
81
45
56
12
43
95
19
67
81
45
Busca de x = 81
46
Busca de x = 81
i = 0;
a[N] = x;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
F
while (a[i] != x)
i++;
45
56
12
43
95
19
67
81
45
56
12
43
95
19
67
81
i
47
48
Busca de x = 81
Exemplo em C++
#include <iostream>
using namespace std;
i = 0;
a[N] = x;
while (a[i] != x)
i++;
N
45
56
12
43
95
19
67
81
de
de
de
de
19
45
8
81
=
=
=
=
5
0
6
-1
int main(void)
{ const int m = 8;
int v[m+1] ={45,56,12,43,95,19,8,67};
Busca
Busca
Busca
Busca
de
de
de
de
19
45
8
81
=
=
=
=
"
"
"
"
<<
<<
<<
<<
}
49
50
Busca Binria
52
Busca Binria
Busca Binria
Busca de x = 19
Suponha m = 3
Algoritmo:
a 8
12
19
43
L = 0;
R = N - 1;
achou = false;
while (L <= R && ! achou)
{ m = qualquer valor entre L e R;
if (a[m] == x)
achou = true;
else
if (a[m] < x)
L = m + 1;
else
R = m - 1;
}
N=8
12
19
43
45
56
67
95
45
56
67
95
N=8
54
Busca Binria
Busca Binria
56
Busca de x = 19
Algoritmo:
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
Se ao trmino do algoritmo a condio a[R] == x for
verdadeira, ento x foi encontrado na posio R de a; caso
contrrio x no foi encontrado.
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
N=8
12
19
43
45
56
67
95
57
58
Busca de x = 19
Busca de x = 19
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
N=8
12
19
43
45
56
67
95
N=8
12
19
43
45
56
67
95
m
R
59
60
10
Busca de x = 19
Busca de x = 19
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
N=8
12
19
43
45
56
67
95
N=8
12
19
43
45
56
67
95
61
62
Busca de x = 19
Busca de x = 19
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
N=8
12
19
43
45
56
67
95
N=8
12
19
43
45
56
67
95
63
64
Busca de x = 19
Busca de x = 19
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
N=8
12
19
43
45
56
67
95
N=8
12
19
43
45
56
67
95
65
66
11
Busca de x = 19
Busca de x = 19
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
N=8
12
19
43
45
56
67
95
N=8
12
19
43
45
56
67
95
m
R
67
68
Busca de x = 19
Busca de x = 19
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
N=8
12
19
43
45
56
67
95
N=8
12
19
43
45
56
67
95
m
R
69
70
Exemplo em C++
#include <iostream>
using namespace std;
int busca_binaria_rapida(int x, int N, int a[])
{ int L,R,m;
L = 0;
R = N - 1;
while (L < R)
{ m = (L + R) / 2;
if (a[m] < x)
L = m + 1;
else
R = m;
}
return (x == a[R]) ? R : -1;
Busca
Busca
Busca
Busca
de
de
de
de
19
45
8
81
=
=
=
=
5
0
6
-1
}
int main(void)
{ const int m = 8;
int v[m+1] ={8,12,19,43,45,56,67,95};
cout << "Busca
cout << "Busca
cout << "Busca
cout << "Busca
return 0;
}
de
de
de
de
19
45
8
81
=
=
=
=
"
"
"
"
<<
<<
<<
<<
128
1.024
10
32.768
15
1.048.576
20
1.073.741.824
30
1.099.511.627.776
40
1080
266
72
12
Comparao
Comparao
Cmn
O(1)
O(1)
O(1)
O(log2N)
Cmd
O(N)
O(N)
O(log2N)
O(log2N)
Nmero de
comparaes
Cmx
O(N)
O(N)
O(log2N)
O(log2N)
Busca Linear
Busca Binria
74
Resumo
Das anlises dos algoritmos de busca, est claro
que o mtodo de busca binria tem um
desempenho to bom ou melhor do que o mtodo
de busca linear
Entretanto, a atualizao dos ndices esquerdo,
direito e mdio (L, R e m no algoritmo,
respectivamente) requer tempo adicional
Assim, para vetores com poucos elementos, a
busca linear adequada
Para vetores com muitos elementos, a busca
binria mais eficiente, mas isso requer que o
vetor esteja ordenado
75
13