Please enable JavaScript.
Coggle requires JavaScript to display documents.
M1 (Tabelas de Espalhamento, Recursividade, Decrease-and-Conquer, Arvores…
M1
Tabelas de Espalhamento
Sondagem quadrática
A sondagem quadrática é um esquema de endereçamento aberto em programação de computador para resolver colisões de hash em tabelas de hash . A sondagem quadrática opera tomando o índice hash original e adicionando valores sucessivos de um polinômio quadrático arbitrário até que um slot aberto seja encontrado.
-
Hash Duplo
int hash(int x)
{
return x % 10;
}
void insere(int a[], int x){
a[hash(x)] = x;
}
int busca_hash(int a[], int x){
int k;k = hash(x);
if (a[k] == x)
return k;return – 1;
}
-
Colisões
-
-
-
S No pior caso (N elementos ocupados), deve-se percorrer os N elementos antes
-
-
-
-
S Essa segunda função de hash tem que ser escolhida com cuidado, não deve
-
-
-
-
Soluções
-
(C1+2C2) %N, depois (C1+3C2) %N, etc.
-
-
-
-
-
-
-
-
-
Recursividade
Algoritmo
-
Algoritmo Geral
Input -> Um número não negativo inteiro
Output: the value of n!
if n = 0 return 1
else
return F(n - 1 ) * n
-
-
-
Arvores Binárias
Assim como árvores binárias generalizam a ideia de listas encadeadas, árvores binárias de busca (= binary search trees = BSTs) generalizam a ideia de listas encadeadas crescentes.
-
Busca Sequencial
Algoritmo
//Input: An array A of n elements and a search key K
//Output: The index of the first element in A[0..n − 1] whose value is equal to K or −1 if no such element is found
A[n]← K
i ← 0
while A[i] = K do
i ← i + 1
if i<n return i
else return −1
-
-
Gerando Permutações
JohnsonTrotter
//Input: A positive integer n
//Output: A list of all permutations of {1,...,n}
initialize the first permutation with 1←2←... n
while the last permutation has a mobile element do
find its largest mobile element k
swap k with the adjacent element k’s arrow points to
reverse the direction of all the elements that are larger than k
add the new permutation to the list
-
-
Vantagens
-
aumentar o tamanho da tabela hash, o que implica menor número
-
-
-
ao invés de seguir os ponteiros nas listas, calculamos a
-
-
-