1 of 24

Divide y vencerás

DYV

2 of 24

El método está basado en la resolución recursiva de un problema dividiéndolo en dos o más subproblemas de igual tipo o similar.

3 of 24

Búsqueda binaria

4 of 24

Buscar el número 18 en la siguiente lista ORDENADA de números:

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]

5 of 24

6 of 24

7 of 24

8 of 24

9 of 24

public static int busquedaBinaria(int vector[], int dato){�

….

� }

10 of 24

public static int busquedaBinaria(int vector[], int dato){� int centro, inf = 0, sup = n - 1;

� }

11 of 24

public static int busquedaBinaria(int vector[], int dato){� int centro, inf = 0, sup = n - 1;

while(inf <= sup) {� � }

� }

12 of 24

public static int busquedaBinaria(int vector[], int dato){� int centro, inf = 0, sup = n - 1;

while(inf <= sup) {� centro = ( sup + inf ) / 2;� }

� }

13 of 24

public static int busquedaBinaria(int vector[], int dato){� int centro, inf = 0, sup = n - 1;

while(inf <= sup) {� centro = ( sup + inf ) / 2;

if(vector[centro] == dato) return centro;

� }

� }

14 of 24

public static int busquedaBinaria(int vector[], int dato){� int centro, inf = 0, sup = n - 1;

while(inf <= sup) {� centro = ( sup + inf ) / 2;

if(vector[centro] == dato) return centro;

else if(dato < vector [centro] ){� sup = centro - 1;� }

� }

� }

15 of 24

public static int busquedaBinaria(int vector[], int dato){� int centro, inf = 0, sup = n - 1;

while(inf <= sup) {� centro = ( sup + inf ) / 2;

if(vector[centro] == dato) return centro;

else if(dato < vector [centro] ){� sup = centro - 1;� } else {� inf = centro + 1;� }� }

� }

16 of 24

public static int busquedaBinaria(int vector[], int dato){� int centro, inf = 0, sup = n - 1;

while(inf <= sup) {� centro = ( sup + inf ) / 2;

if(vector[centro] == dato) return centro;

else if(dato < vector [centro] ){� sup = centro - 1;� } else {� inf = centro + 1;� }� }

return -1;� }

17 of 24

Divide y vencerás

Diseño e implementación

18 of 24

  1. Descomponer el problema en K problemas de MENOR tamaño, pero del MISMO tipo.
  2. Resolverse todos los subproblemas, tanto si son elementales, como si son compuestos.
  3. Combinar las soluciones.

19 of 24

Mergesort

Ordenamiento por mezcla

20 of 24

6

5

3

1

8

7

2

4

21 of 24

1

3

5

6

2

4

7

8

9

11

13

17

10

12

14

20

inf = 0, sup = 7, med = 3

22 of 24

Exponenciación Binaria

23 of 24

24 of 24

Ejercicios