6. Un ejemplo sencillo: Búsqueda Binaria begin i 1; j n; q false ; repeat k (i+j) div 2; if A[k] = x then q true else if A[k] < x then i k+1 else j k-1 until q or (i > j) end
7.
8. Supongamos un arreglo de 9 elementos: -15 -6 0 7 9 23 54 82 101 1 2 3 4 5 6 7 8 9 Por ejemplo, si buscamos el valor 101 i j k q 1 9 5 false 6 9 7 false 8 9 8 false 9 9 9 true Éxito en 4 comparaciones
9. Ejemplo (continuación) Si buscamos el valor 14 i j k q 1 9 5 false 1 4 2 false 1 1 1 false 2 1 Fracaso: no se encontró con 4 comparaciones
11. Ejemplo (continuación) El número de comparaciones (de elementos) necesarias para encontrar cada uno de los 9 elementos es: A [1] [2] [3] [4] [5] [6] [7] [8] [9] elemento -15 -6 0 7 9 23 54 82 101 comparaciones 3 2 3 4 1 3 2 3 4
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23. Un Ejemplo: Sort por Inserción SortInserción (A, n) { for i = 2 to n { item = A[i]; j = i - 1; while (j > 0) and (A[j] > item) { A[j+1] = A[j] j = j - 1 } A[j+1] = item } }
24. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 10 40 20 1 2 3 4 i = j = key = A[j] = A[j+1] =
25. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 10 40 20 1 2 3 4 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 10
26. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 30 40 20 1 2 3 4 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 30
27. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 30 40 20 1 2 3 4 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 30
28. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 30 40 20 1 2 3 4 i = 2 j = 0 key = 10 A[j] = A[j+1] = 30
29. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 30 40 20 1 2 3 4 i = 2 j = 0 key = 10 A[j] = A[j+1] = 30
30. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 2 j = 0 key = 10 A[j] = A[j+1] = 10
31. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 0 key = 10 A[j] = A[j+1] = 10
32. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 0 key = 40 A[j] = A[j+1] = 10
33. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 0 key = 40 A[j] = A[j+1] = 10
34. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 2 key = 40 A[j] = 30 A[j+1] = 40
35. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 2 key = 40 A[j] = 30 A[j+1] = 40
36. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 2 key = 40 A[j] = 30 A[j+1] = 40
37. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 4 j = 2 key = 40 A[j] = 30 A[j+1] = 40
38. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40
39. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40
40. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 20
41. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 20
42. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 40 1 2 3 4 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40
43. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 40 1 2 3 4 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40
44. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 40 1 2 3 4 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40
45. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 40 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40
46. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 40 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40
47. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 30 40 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 30
48. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 30 40 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 30
49. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 30 40 1 2 3 4 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 30
50. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 30 40 1 2 3 4 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 30
51. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 20 30 40 1 2 3 4 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 20
52. Un Ejemplo: Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 20 30 40 1 2 3 4 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 20 ¡Listo!
53. Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } ¿Cuál es la precondición para este loop?
54. Sort por Inserción SortInserción(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } ¿Cuántas veces se ejecuta este loop?
55. Sort por Inserción Instrucción Esfuerzo SortInserción(A, n) { for i = 2 to n { c 1 n key = A[i] c 2 (n-1) j = i - 1; c 3 (n-1) while (j > 0) and (A[j] > key) { c 4 T A[j+1] = A[j] c 5 (T-(n-1)) j = j - 1 c 6 (T-(n-1)) } 0 A[j+1] = key c 7 (n-1) } 0 } T = t 2 + t 3 + … + t n donde t i es el número de evaluaciones de la expresión while para la i-ésima iteración del loop