Académique Documents
Professionnel Documents
Culture Documents
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
Notacin
f(n) = (g(n)) c, n0 tal que n f(n) cg(n) n n0
f(n) c g(n)
n0
f(n) = 3n + 2
f(n) = (n)
f(n) = 10n2 + 4n + 2
f(n) = (n2)
f(n) = 6*2n + n2
f(n) = (2n)
f(n) = (1)
f(n) = 9n2 + 4n + 2
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
Notacin
f(n) = (g(n)) c, n0 tal que n f(n) cg(n) n n0
c g(n)
f(n)
n0
f(n) = 3n + 2
f(n) = (n)
f(n) = 10n2 + 4n + 2
f(n) = (n2)
f(n) = 6*2n + n2
f(n) = (2n)
f(n) = (1)
f(n) = 9n2 + 4n + 2
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
Notacin
f(n) = (g(n)) f(n) es (g(n)) y f(n) es (g(n))
f(n)=c g(n)
n0
f(n) = 3n + 2
f(n) = (n)
f(n) = 10n2 + 4n + 2
f(n) = (n2)
f(n) = 6*2n + n2
f(n) = (2n)
f(n) = (1)
no es (n) ni (n2)
4
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
Complejidades
Valores de Varias Funciones [Sahni, 2005]
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
Rank Sort
El rank de un elemento en una secuencia es el nmero de elementos
ms pequeos en la secuencia ms el nmero de elementos iguales
que aparecen a la izquierda del elemento en la secuencia.
Ejemplo:
a[0:8] = [3, 2, 6, 1, 9, 4, 3, 7, 1, 8]
r[0:8] = [3, 2, 6, 0, 9, 5, 4, 7, 1, 8]
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
Rank Sort
template<class T>
void rank(T a[], int n, int r[])
{ // Secuencia de n elementos a[0:n-1]
// Rank de la secuencia r[0:n-1]
// inicializacin
for (int i = 0; i< n; i++)
r[i]=0;
// Comparacin de todos los pares
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (a[j] <= a[i])
r[i]++;
else
r[j]++;
}
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
Rank Sort
a[0:5]
a[0:5]
1
1
2
1
3
1
4
1
3
1
5
1
1
r[0:5]
4
3
r[0:5]
10
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
// inicializacin
for (int i = 0; i< n; i++)
r[i]=0;
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
El nmero de elementos
movidos durante la ejecucin
de la funcin rearrange es
2n.
El total es:
n(n 1)/2 comparaciones +
2n elementos movidos
delete [] u;
}
12
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
0 1 2 3 4 5
0 1 2 3 4 5
3 0 4 5 2 1
0 1 4 3 2 5
d a
a b e
e f
c b
d c f
5 0 4 3 2 1
0 1 2 3 4 5
a b c
e d c b
d e f
1 0 4 3 2 5
0 1 2 3 4 5
b a
a b c
e d c f
d e f
0 1 4 3 2 5
a b e d c f
13
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
Complejidad
template<class T>
void rearrange(T a[], int n, int r[])
{
// En lugar de arreglar los elementos
for (int i = 0; i < n; i++)
El nmero de cambios: de 0
hasta 2(n 1)
14
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
8
15
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
El
nmero
de
elementos
comparados es n(n 1)/2
5
5
4
3
1
6
4
3
1
3
4
3
1
4
4
3
1
5
5
5
1
6
6
6
6
8
8
8
8
8
16
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
0
6
1
1
1
1
1
2
2
2
2
2
5
3
3
3
5
3
4
4
4
3
4
5
5
5
4
6
6
6
Tomadas del curso Algorithms and Data Structure A and B, por Dr. Teofilo Gonzlez, Computer Science Department UCSB
18
19
2.
3.
4.
22
23