leccion 16 p2.ppt [Modo de compatibilidad] -...
Transcript of leccion 16 p2.ppt [Modo de compatibilidad] -...
![Page 1: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/1.jpg)
Programación 2
Lección 16. Diseño de algoritmos de distribución y de ordenación
de los datos de un vector
1
![Page 2: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/2.jpg)
1. Especificación de algoritmos de distribución y de ordenación de los datos de un vector
2. Algoritmo de distribución de los datos de un vector
3. Algoritmos de ordenación de ordenación interna de los datos de un vector
– Ordenación por selección directa– Ordenación por inserción directa– Ordenación por intercambio directo– Ordenación rápida o quicksort
4. Análisis comparativo de sus costes2
![Page 3: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/3.jpg)
1. Especificación de algoritmos de distribución y de ordenación de los datos de un vector
Consideraremos estos predicados parametrizados para facilitar laescritura de predicados más complejos:
esPermutación(v1,v2,desde,hasta) =
(PT alfa EN [desde,hasta].
(Núm beta EN [desde,hasta]. v1[beta] = v1[alfa])
= (Núm beta EN [desde,hasta]. v2[beta] = v1[alfa]) )
ordenado(v,desde,hasta) =
(PT alfa EN [desde,hasta‐1]. v[alfa] <= v[alfa+1])
3
![Page 4: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/4.jpg)
Función de distribución de los datos de un vector:
/** Pre: v = Va AND n > 0 AND n <= #v * Post: esPermutación(v,Va,0,n‐1) AND* (PT alfa EN [0,n‐1].* (v[alfa] <= frontera ‐>* (PT beta EN [0,alfa]. v[beta] <= frontera)) AND* (v[alfa] > frontera ‐>* (PT beta EN [alfa,n‐1]. v[beta] > frontera)) )*/
template <typename T>void distribucion (T v[], const int n, const T frontera);
4
![Page 5: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/5.jpg)
Función de ordenación de los datos de un vector:
/** Pre: v = Vo AND n > 0 AND n <= #v * Post: esPermutación(v,Vo,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void ordenacion (T v[], const int n) ;
5
![Page 6: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/6.jpg)
2. Algoritmo de distribución de los datos de un vector
Se va a diseñar un algoritmo de distribución de losdatos de un vector:
/** Pre: v = Va AND n > 0 AND n <= #v * Post: esPermutación(v,Va,0,n‐1) AND* (PT alfa EN [0,n‐1].* (v[alfa] <= frontera ‐>* (PT beta EN [0,alfa]. v[beta] <= frontera)) AND* (v[alfa] > frontera ‐>* (PT beta EN [alfa,n‐1]. v[beta] > frontera)) )*/template <typename T>void distribucion (T v[], const int n, const T frontera);
6
![Page 7: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/7.jpg)
2. Algoritmo de distribución de los datos de un vector
Se va a diseñar un algoritmo de distribución de losdatos de un vector:
7
<= frontera > frontera
Almacena una permutación de los datos iniciales Vo de v
v
0 n-1
![Page 8: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/8.jpg)
Algoritmo de distribución de los datos de un vector:
8
inf sup
<= frontera > frontera
Almacena una permutación de los datos iniciales Vo de v
v
0 n-1
![Page 9: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/9.jpg)
/** Pre: v = va AND n > 0 AND n <= #v * Post: esPermutación(v,va,0,n‐1) AND …*/template <typename T>void distribucion (T v[], const int n, const T frontera) {
int inf = 0, sup = n ‐ 1;while (inf != sup + 1) {
// Inv: n > 0 AND n <= #v AND esPermutación(v,va,0,n‐1)// AND inf > 0 AND inf <= sup + 1 AND sup <= n ‐ 1// AND (PT alfa [0,inf‐1].v[alfa] <= frontera)// AND (PT alfa [sup+1,n‐1].v[alfa] > frontera)if (v[inf] <= frontera) { inf = inf + 1; }else if (v[sup] > frontera) { sup = sup ‐ 1; }else { // Permuta los elementos v[inf] y v[sup]
T dato = v[inf]; v[inf] = v[sup]; v[sup] = dato;inf = inf + 1; sup = sup ‐ 1;
}}
}
9
![Page 10: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/10.jpg)
3. Algoritmos de ordenación interna de los datos de un vector
Se van a diseñar estos algoritmos de ordenacióninterna de vectores:
– Ordenación por selección directa– Ordenación por inserción directa– Ordenación por intercambio directo o método de la
burbuja– Ordenación rápida o ‘quicksort’
10
![Page 11: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/11.jpg)
Ordenación por selección directa
11
Aquí están los menores y están ordenados
Se busca la posicióniMenor del menor, v[iMenor], y sepermuta con v[i]
i
Almacena una permutación de los datos iniciales Vo de v
v
0 n-1
![Page 12: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/12.jpg)
Algoritmo de ordenación interna de vectorespor selección directa:
12
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
![Page 13: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/13.jpg)
13
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
102 814 813 333 754 367 401 7541 2 3 4 5 6 7 8
![Page 14: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/14.jpg)
14
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
102 814 813 333 754 367 401 7541 2 3 4 5 6 7 8
102 333 813 814 754 367 401 7541 2 3 4 5 6 7 8
![Page 15: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/15.jpg)
15
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
102 814 813 333 754 367 401 7541 2 3 4 5 6 7 8
102 333 367 814 754 813 401 7541 2 3 4 5 6 7 8
102 333 813 814 754 367 401 7541 2 3 4 5 6 7 8
![Page 16: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/16.jpg)
16
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
102 814 813 333 754 367 401 7541 2 3 4 5 6 7 8
102 333 367 814 754 813 401 7541 2 3 4 5 6 7 8
102 333 813 814 754 367 401 7541 2 3 4 5 6 7 8
102 333 367 401 754 813 814 7541 2 3 4 5 6 7 8
![Page 17: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/17.jpg)
17
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
102 814 813 333 754 367 401 7541 2 3 4 5 6 7 8
102 333 367 814 754 813 401 7541 2 3 4 5 6 7 8
102 333 813 814 754 367 401 7541 2 3 4 5 6 7 8
102 333 367 401 754 813 814 7541 2 3 4 5 6 7 8
102 333 367 401 754 813 814 7541 2 3 4 5 6 7 8
![Page 18: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/18.jpg)
18
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
102 814 813 333 754 367 401 7541 2 3 4 5 6 7 8
102 333 367 814 754 813 401 7541 2 3 4 5 6 7 8
102 333 813 814 754 367 401 7541 2 3 4 5 6 7 8
102 333 367 401 754 813 814 7541 2 3 4 5 6 7 8
102 333 367 401 754 813 814 7541 2 3 4 5 6 7 8
102 333 367 401 754 754 814 8131 2 3 4 5 6 7 8
![Page 19: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/19.jpg)
19
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
102 814 813 333 754 367 401 7541 2 3 4 5 6 7 8
102 333 367 814 754 813 401 7541 2 3 4 5 6 7 8
102 333 813 814 754 367 401 7541 2 3 4 5 6 7 8
102 333 367 401 754 813 814 7541 2 3 4 5 6 7 8
102 333 367 401 754 813 814 7541 2 3 4 5 6 7 8
102 333 367 401 754 754 814 8131 2 3 4 5 6 7 8
102 333 367 401 754 754 813 8141 2 3 4 5 6 7 8
![Page 20: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/20.jpg)
/** Pre: v = Vo AND n > 0 AND n <= #v * Post: esPermutación(v,v0,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void seleccion (T v[], const int n) {
for (int i = 0; i != n ‐ 1; ++i) {// Inv1: Seleccionados y reubicados los i datos menores de Vo[0,n‐1]int iMenor = i, j = i;while (j != n ‐ 1) {
// Inv2: Recorrido de v[i,n‐1] buscando el menor de sus elementosj = j + 1;if (v[j] < v[iMenor]) {
iMenor = j; }
}// Permuta v[i] y v[iMenor]T dato = v[i]; v[i] = v[iMenor]; v[iMenor] = dato;
}}
20
![Page 21: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/21.jpg)
/** Pre: v = Vo AND n > 0 AND n <= #v * Post: esPermutación(v,v0,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void seleccion (T v[], const int n) {
for (int i = 0; i != n ‐ 1; ++i) {// Inv1: n > 0 AND n <= #v AND esPermutación(v,Vo,0,n‐1) AND ordenado(v,0,i‐1)// AND (PT alfa EN [0,i‐1].(PT beta EN [i,n‐1]. v[alfa] <= v[beta]) )
int iMenor = i, j = i;while (j != n ‐ 1) {
// Inv2: Recorrido de v[i,n‐1] buscando el menor de sus elementosj = j + 1;if (v[j] < v[iMenor]) {
iMenor = j; }
}// Permuta v[i] y v[iMenor]T dato = v[i]; v[i] = v[iMenor]; v[iMenor] = dato;
}}
21
![Page 22: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/22.jpg)
/** Pre: v = Vo AND n > 0 AND n <= #v * Post: esPermutación(v,v0,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void seleccion (T v[], const int n) {
for (int i = 0; i != n ‐ 1; ++i) {// Inv1: n > 0 AND n <= #v AND esPermutación(v,Vo,0,n‐1) AND ordenado(v,0,i‐1)// AND (PT alfa EN [0,i‐1].(PT beta EN [i,n‐1].v[alfa] <= v[beta]) )int iMenor = i, j = i;while (j != n ‐ 1) {
// Inv2: n > 0 AND esPermutación(v,Vo,0,n‐1) AND ordenado(v,0,i‐1) AND// (PT alfa EN [0,i‐1].(PT beta EN [i,n‐1].v[alfa] <= v[beta]) ) AND// (PT alfa EN [i,j].v[iMenor] <= v[alfa])j = j + 1;if (v[j] < v[iMenor]) {
iMenor = j; }
}// Permuta v[i] y v[iMenor]T dato = v[i]; v[i] = v[iMenor]; v[iMenor] = dato;
}}
22
![Page 23: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/23.jpg)
Ordenación por inserción directa
23
Ordenados
Debe insertar v[i] en la posición v[j] que le corresponda en v[0..i]
ij
Almacena una permutación de los datos iniciales Vo de v
v
0 n-1
![Page 24: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/24.jpg)
Ordenación por inserción directa
24
Ordenados
Inserta v[i] en la posición v[j] que lecorresponda, previo desplazamientode los elementos v[j..i-1]
ij
dato
Almacena una permutación de los datos iniciales Vo de v
v
0 n-1
![Page 25: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/25.jpg)
Algoritmo de ordenación interna de vectorespor inserción directa:
25
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
![Page 26: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/26.jpg)
26
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
![Page 27: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/27.jpg)
27
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
367 813 814 333 754 102 401 7541 2 3 4 5 6 7 8
![Page 28: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/28.jpg)
28
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
333 367 813 814 754 102 401 7541 2 3 4 5 6 7 8
367 813 814 333 754 102 401 7541 2 3 4 5 6 7 8
![Page 29: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/29.jpg)
29
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
333 367 813 814 754 102 401 7541 2 3 4 5 6 7 8
367 813 814 333 754 102 401 7541 2 3 4 5 6 7 8
333 367 754 813 814 102 401 7541 2 3 4 5 6 7 8
![Page 30: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/30.jpg)
30
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
333 367 813 814 754 102 401 7541 2 3 4 5 6 7 8
367 813 814 333 754 102 401 7541 2 3 4 5 6 7 8
333 367 754 813 814 102 401 7541 2 3 4 5 6 7 8
102 333 367 754 813 814 401 7541 2 3 4 5 6 7 8
![Page 31: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/31.jpg)
31
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
333 367 813 814 754 102 401 7541 2 3 4 5 6 7 8
367 813 814 333 754 102 401 7541 2 3 4 5 6 7 8
333 367 754 813 814 102 401 7541 2 3 4 5 6 7 8
102 333 367 754 813 814 401 7541 2 3 4 5 6 7 8
102 333 367 401 754 813 814 7541 2 3 4 5 6 7 8
![Page 32: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/32.jpg)
32
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
333 367 813 814 754 102 401 7541 2 3 4 5 6 7 8
367 813 814 333 754 102 401 7541 2 3 4 5 6 7 8
333 367 754 813 814 102 401 7541 2 3 4 5 6 7 8
102 333 367 754 813 814 401 7541 2 3 4 5 6 7 8
102 333 367 401 754 813 814 7541 2 3 4 5 6 7 8
102 333 367 401 754 754 813 8141 2 3 4 5 6 7 8
![Page 33: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/33.jpg)
/* * Pre: v = Vo AND n > 0 AND n <= #v * Post: esPermutación(v,v0,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void insercion (T v[], const int n) {
for (int i = 1; i <= n ‐ 1; ++i) {// Inv1: Insertados de forma ordenada los elementos de v[0,i‐1]T dato = v[i];int j = i, iLimite = 0;while (j != iLimite) {
// Inv2: Búsqueda del punto donde insertar el elemento datoif (v[j‐1] <= dato) {
iLimite = j; }else {
v[j] = v[j‐1]; j = j ‐ 1;
}}v[j] = dato;
}}
33
![Page 34: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/34.jpg)
/* * Pre: v = Vo AND n > 0 AND n <= #v * Post: esPermutación(v,Vo,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void insercion (T v[], const int n) {
for (int i = 1; i <= n ‐ 1; ++i) {// Inv1: n > 0 AND n <= #v AND esPermutación(v,Vo,0,n‐1) // AND ordenado(v,0,i‐1)T dato = v[i];int j = i, iLimite = 0;while (j != iLimite) {
// Inv2: Búsqueda del punto donde insertar el elemento datoif (v[j‐1] <= dato) {
iLimite = j; }else {
v[j] = v[j‐1]; j = j ‐ 1;
}}v[j] = dato;
}}
34
![Page 35: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/35.jpg)
/* * Pre: v = Vo AND n > 0 AND n <= #v * Post: esPermutación(v,Vo,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void insercion (T v[], const int n) {
for (int i = 1; i <= n ‐ 1; ++i) {// Inv1: n > 0 AND n <= #v AND esPermutación(v,Vo,0,n‐1) // AND ordenado(v,0,i‐1)T dato = v[i];int j = i, iLimite = 0;while (j != iLimite) {
// Inv2: n > 0 AND n <= #v AND esPermutación*(v,Vo,0,n‐1) AND// ordenado(v,0,j‐1) AND ordenado(v,j,i) AND// (PT alfa EN [0,j‐1].// (PT beta EN [j+1,i].v[alfa] <= v[beta]) ) AND// j <= i AND dato = Vo[i] AND// (PT alfa EN [j+1,i].dato < v[alfa])if (v[j‐1] <= dato) { iLimite = j; }else { v[j] = v[j‐1]; j = j ‐ 1; }
}v[j] = dato;
}} 35
![Page 36: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/36.jpg)
Ordenación por intercambio directo
36
Aquí están los mayores y
están ordenadosHay que ‘arrastrar’el mayor de v[0,i]
hasta v[i]
i
v
0 n-1
Almacena una permutación de los datos iniciales Vo de v
![Page 37: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/37.jpg)
Ordenación por intercambio directo
37
Aquí están los mayores y
están ordenadosHay que arrastrar el mayor de v[0,i]hasta v[i], comparando paresconsecutivos de v[j,j+1] y, si no estánordenados entre sí, se permutan (seintercambian)
Almacena una permutación de los datos iniciales Vo de v
ij j+1
v[0..i-1]
?
0 n-1
v
![Page 38: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/38.jpg)
Algoritmo de ordenación interna de vectorespor intercambio directo (método de la burbuja):
38
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
367 813 333 754 102 401 754 8141 2 3 4 5 6 7 8
![Page 39: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/39.jpg)
39
367 813 333 754 102 401 754 8141 2 3 4 5 6 7 8
367 333 754 102 401 754 813 8141 2 3 4 5 6 7 8
![Page 40: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/40.jpg)
40
333 367 102 401 754 754 813 8141 2 3 4 5 6 7 8
367 333 754 102 401 754 813 8141 2 3 4 5 6 7 8
![Page 41: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/41.jpg)
41
333 367 102 401 754 754 813 8141 2 3 4 5 6 7 8
333 102 367 401 754 754 813 8141 2 3 4 5 6 7 8
![Page 42: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/42.jpg)
42
333 102 367 401 754 754 813 8141 2 3 4 5 6 7 8
102 333 367 401 754 754 813 8141 2 3 4 5 6 7 8
![Page 43: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/43.jpg)
43
1 2 3 4 5 6 7 8
102 333 367 401 754 754 813 8141 2 3 4 5 6 7 8
102 333 367 401 754 754 813 8141 2 3 4 5 6 7 81 2 3 4 5 6 7 8
![Page 44: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/44.jpg)
44
102 333 367 401 754 754 813 8141 2 3 4 5 6 7 8
102 333 367 401 754 754 813 8141 2 3 4 5 6 7 8
![Page 45: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/45.jpg)
45
367 814 813 333 754 102 401 7541 2 3 4 5 6 7 8
367 813 333 754 102 401 754 8141 2 3 4 5 6 7 8
333 367 102 401 754 754 813 8141 2 3 4 5 6 7 8
367 333 754 102 401 754 813 8141 2 3 4 5 6 7 8
333 102 367 401 754 754 813 8141 2 3 4 5 6 7 8
102 333 367 401 754 754 813 8141 2 3 4 5 6 7 8
102 333 367 401 754 754 813 8141 2 3 4 5 6 7 8
102 333 367 401 754 754 813 8141 2 3 4 5 6 7 8
![Page 46: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/46.jpg)
/** Pre: v = v0 AND n > 0 AND n <= #v* Post: esPermutación(v,v0,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void intercambio (T v[], const int n) {
for (int i = 0; i < n ‐ 1; i++) {// Inv1: Ubicados en su posición definitiva los i mayores elementosfor (int j = 0; j != n – 1 ‐ i; j++) {
// Inv2: Recorrido de v[0,n‐1‐j] permutando pares de elementos // consecutivos en desordenif (v[j+1] < v[j]) {
// Permuta los valores de v[j] y v[j+1]T dato = v[j];v[j] = v[j+1]; v[j+1] = dato;
}}
}}
46
![Page 47: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/47.jpg)
/** Pre: v = Vo AND n > 0 AND n <= #v* Post: esPermutación(v,Vo,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void intercambio (T v[], const int n) {
for (int i = 0; i < n ‐ 1; i++) {// Inv1: n > 0 AND n <= #v AND esPermutación(v,Vo,0,n‐1) AND// ordenado(v,n‐1‐i,n‐1) AND// (PT alfa EN [0,n‐1‐i].(PT beta EN [n‐i,n‐1]. v[alfa]<=v[beta]) )for (int j = 0; j != n – 1 ‐ i; j++) {
// Inv2: Recorrido de v[0,n‐1‐j] permutando pares de elementos // consecutivos en desordenif (v[j+1] < v[j]) {
// Permuta los valores de v[j] y v[j+1]T dato = v[j];v[j] = v[j+1]; v[j+1] = dato;
}}
}}
47
![Page 48: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/48.jpg)
/** Pre: v = v0 AND n > 0 AND n <= #v* Post: esPermutación(v,v0,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void intercambio (T v[], const int n) {
for (int i = 0; i < n ‐ 1; i++) {// Inv1: n > 0 AND n <= #v AND esPermutación(v,Vo,0,n‐1) AND// ordenado(v,n‐1‐i,n‐1) AND// (PT alfa EN [0,n‐1‐i].(PT beta EN [n‐i,n‐1]. v[alfa]<=v[beta]) )for (int j = 0; j != n – 1 ‐ i; j++) {
// Inv2: n > 0 AND n <= #v AND esPermutación(v,v0,0,n‐1) AND// ordenado(v,0,j‐1) AND j < n – 1 ‐ i AND// (PT alfa EN [0,n‐i].// (PT beta EN [n‐i,n‐1].v[alfa] <= v[beta]) ) AND// (PT alfa EN [0,j]. v[alfa] <= v[j])if (v[j+1] < v[j]) {
// Permuta los valores de v[j] y v[j+1]T dato = v[j];v[j] = v[j+1]; v[j+1] = dato;
}}
}} 48
![Page 49: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/49.jpg)
Algoritmo rápido de ordenación interna de vectores o ‘quicksort’:/** Pre: v = Vo AND n > 0 AND n <= #v* Post: esPermutación(v,Vo,0,n‐1) AND ordenado(v,0,n‐1)*/template <typename T>void quicksort (T v[], const int n) {
// Ordenación del vector v[0,n‐1] aplicando el método de ordenación// rápida de C.A.R.Hoare, también conocido como quicksortquicksort(v, 0, n‐1);
}
49
![Page 50: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/50.jpg)
/** Pre: v = Vo AND n > 0 AND n <= #v* Post: esPermutación(v,Vo,0,n‐1) AND ordenado(v,0,n‐1)*/
template <typename T>void quicksort (T v[], const int n) {
quicksort(v, 0, n‐1);}
/** Pre: v = Va AND desde >= 0 AND hasta < #v* Post: esPermutación(v,Va,izda,dcha) AND ordenado(v,izda,dcha)*/
template <typename T>void quicksort (T v[], const int izda, const int dcha) {
… código de la función …
}
50
![Page 51: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/51.jpg)
Algoritmo rápido de ordenación o quicksort
51
di
v[i]
Distribuir según v[i]:
<=v[i] >v[i]
ordenar ordenar
v
v
0 n-1
di0 n-1
![Page 52: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/52.jpg)
/** Pre: v = Va AND izda >= 0 AND dcha < #v* Post: esPermutación(v,Va,izda,dcha) AND ordenado(v,izda,dcha)*/
template <typename T>void quicksort (T v[], const int izda, const int dcha) {
if (izda < dcha) { // Son dos, como mínimo, los datos a ordenarT pivote = v[izda]; // Selecciona el "pivote"int i = izda + 1, d = dcha;while (i != d + 1) {
if (v[i] <= pivote) { i = i + 1; }else if (v[d] >= pivote) { d = d ‐ 1; }else {
T dato = v[i]; v[i] = v[d]; v[d] = dato;i = i + 1; d = d ‐ 1;
}}// Sitúa el pivote en la frontera entre los datos cuyo valor // es menor o igual que él y los que son mayor o igual que élv[izda] = v[d]; v[d] = pivote;quicksort(v, izda, d‐1); quicksort(v, d+1, dcha);
}}
52
![Page 53: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/53.jpg)
// Pre: v = Va AND izda >= 0 AND dcha < #v// Post: esPermutación(v,Va,izda,dcha) AND ordenado(v,izda,dcha)template <typename T>void quicksort (T v[], const int izda, const int dcha) {
if (izda < dcha) {T pivote = v[izda]; // Selecciona el "pivote“// Distribuye los datos de v[izda+1,dcha] en función de su valor// respecto al “pivote”int i = izda + 1, d = dcha;while (i != d + 1) {
if (v[i] <= pivote) { i = i + 1; }else if (v[d] >= pivote) { d = d ‐ 1; }else {
T dato = v[i]; v[i] = v[d]; v[d] = dato;i = i + 1; d = d ‐ 1;
}}// Sitúa el “pivote” en la frontera entre los datos cuyo valor // es menor o igual que él y los de valor mayor o igual que élv[izda] = v[d]; v[d] = pivote;quicksort(v, izda, d‐1); quicksort(v, d+1, dcha);
}}
53
![Page 54: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/54.jpg)
4. Análisis comparativo de sus costes
¿Qué algoritmo de los anteriores debo programarpara ordenar un vector? ¿Por qué?
54
Algoritmo de ordenación
Coste en elcaso mejor
Coste en el caso peor
selección directa (n2) (n2)
inserción directa (n) (n2)
intercambio directo (n2) (n2)
rápido o quicksort (n log n) (n2)
![Page 55: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/55.jpg)
4. Análisis comparativo de sus costes
55
![Page 56: leccion 16 p2.ppt [Modo de compatibilidad] - …webdiis.unizar.es/asignaturas/PROG2/doc/grupo_412/transparencias/... · void ordenacion (T v[], const int n) ; 5. 2. Algoritmo de distribución](https://reader031.fdocuments.in/reader031/viewer/2022022701/5bc0eaa409d3f2fb5b8ca2f3/html5/thumbnails/56.jpg)