{"id":404,"date":"2026-11-29T00:13:50","date_gmt":"2026-11-29T00:13:50","guid":{"rendered":"https:\/\/www.luchonet.com.ar\/aed\/?page_id=404"},"modified":"2026-11-29T00:18:51","modified_gmt":"2026-11-29T00:18:51","slug":"divide-y-venceras","status":"publish","type":"page","link":"https:\/\/www.luchonet.com.ar\/aed\/?page_id=404","title":{"rendered":"Divide y vencer\u00e1s"},"content":{"rendered":"<p><strong>Divide y vencer\u00e1s<\/strong> (<em>divide and conquer<\/em>) es una de las t\u00e9cnicas de dise\u00f1o de algoritmos m\u00e1s productivas, y es recursiva por naturaleza. Consiste en tres pasos:<\/p>\n<ol>\n<li><strong>Dividir<\/strong> el problema en subproblemas del mismo tipo, m\u00e1s chicos (normalmente, de una fracci\u00f3n del tama\u00f1o: la mitad, un tercio).<\/li>\n<li><strong>Conquistar<\/strong>: resolver cada subproblema recursivamente; si es lo bastante chico, resolverlo directamente (el caso base).<\/li>\n<li><strong>Combinar<\/strong> las soluciones de los subproblemas para obtener la del problema original.<\/li>\n<\/ol>\n<p>Esta p\u00e1gina presenta cuatro algoritmos cl\u00e1sicos que siguen el esquema: la b\u00fasqueda binaria, Merge Sort, Quicksort y la potencia r\u00e1pida. Su costo se analiza con las recurrencias y el teorema maestro de la <a href=\"https:\/\/www.luchonet.com.ar\/aed\/?page_id=398\">p\u00e1gina anterior<\/a>. Los ejemplos usan listas de Python y <code>std::vector<\/code> de C++; los arreglos se estudian en detalle en el tema de estructuras de datos.<\/p>\n<h2>B\u00fasqueda binaria<\/h2>\n<p>Para buscar un valor en una lista <strong>ordenada<\/strong> no hace falta mirarlo entero: se compara con el elemento del medio y, seg\u00fan el resultado, se descarta la mitad en la que no puede estar. Es como buscar una palabra en un diccionario abri\u00e9ndolo por la mitad.<\/p>\n<figure class=\"chart\" style=\"background:var(--surface,#fff);padding:12px 12px 4px\"><svg viewBox=\"0 0 760 288\" role=\"img\" aria-label=\"Pasos de la b\u00fasqueda binaria de 23 en un arreglo ordenado\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:100%;height:auto;font-family:Inter,system-ui,sans-serif\"><defs><marker id=\"bbn-a\" viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\" markerWidth=\"7\" markerHeight=\"7\" orient=\"auto-start-reverse\"><path d=\"M0,0 L10,5 L0,10 z\" style=\"fill:var(--muted, #5a6776)\"\/><\/marker><\/defs><text x=\"146.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">0<\/text><text x=\"198.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">1<\/text><text x=\"250.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">2<\/text><text x=\"302.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">3<\/text><text x=\"354.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">4<\/text><text x=\"406.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">5<\/text><text x=\"458.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">6<\/text><text x=\"510.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">7<\/text><text x=\"562.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">8<\/text><text x=\"614.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">9<\/text><text x=\"666.0\" y=\"26\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:10px;font-weight:400\">10<\/text><text x=\"106\" y=\"60\" text-anchor=\"end\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:600\">paso 1<\/text><rect x=\"122\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"146.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"174\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"198.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"226\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"250.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"278\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"302.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">17<\/text><rect x=\"330\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"354.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">23<\/text><rect x=\"382\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:#e8833a;stroke-width:2.2;opacity:1\"\/><text x=\"406.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:700;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"434\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"458.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"486\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"510.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"538\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"562.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">56<\/text><rect x=\"590\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"614.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><rect x=\"642\" y=\"36\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"666.0\" y=\"58\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">91<\/text><text x=\"120\" y=\"86\" text-anchor=\"start\" style=\"fill:var(--muted, #5a6776);font-size:11px;font-weight:400\">inicio=0, fin=10, medio=5 \u2192 23 &lt; 27: mitad izquierda<\/text><text x=\"106\" y=\"122\" text-anchor=\"end\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:600\">paso 2<\/text><rect x=\"122\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"146.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"174\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"198.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"226\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:#e8833a;stroke-width:2.2;opacity:1\"\/><text x=\"250.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:700;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"278\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"302.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">17<\/text><rect x=\"330\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"354.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">23<\/text><rect x=\"382\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"406.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"434\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"458.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"486\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"510.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"538\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"562.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">56<\/text><rect x=\"590\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"614.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><rect x=\"642\" y=\"98\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"666.0\" y=\"120\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">91<\/text><text x=\"120\" y=\"148\" text-anchor=\"start\" style=\"fill:var(--muted, #5a6776);font-size:11px;font-weight:400\">inicio=0, fin=4, medio=2 \u2192 23 &gt; 10: mitad derecha<\/text><text x=\"106\" y=\"184\" text-anchor=\"end\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:600\">paso 3<\/text><rect x=\"122\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"146.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"174\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"198.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"226\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"250.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"278\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:#e8833a;stroke-width:2.2;opacity:1\"\/><text x=\"302.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:700;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">17<\/text><rect x=\"330\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--accent, #1f6fb8);stroke-width:1.4;opacity:1\"\/><text x=\"354.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">23<\/text><rect x=\"382\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"406.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"434\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"458.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"486\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"510.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"538\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"562.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">56<\/text><rect x=\"590\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"614.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><rect x=\"642\" y=\"160\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"666.0\" y=\"182\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">91<\/text><text x=\"276\" y=\"210\" text-anchor=\"start\" style=\"fill:var(--muted, #5a6776);font-size:11px;font-weight:400\">inicio=3, fin=4, medio=3 \u2192 23 &gt; 17: mitad derecha<\/text><text x=\"106\" y=\"246\" text-anchor=\"end\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:600\">paso 4<\/text><rect x=\"122\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"146.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"174\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"198.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"226\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"250.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"278\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"302.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">17<\/text><rect x=\"330\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:#e8833a;stroke-width:2.2;opacity:1\"\/><text x=\"354.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:700;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">23<\/text><rect x=\"382\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"406.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"434\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"458.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"486\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"510.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"538\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"562.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">56<\/text><rect x=\"590\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"614.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><rect x=\"642\" y=\"222\" width=\"48\" height=\"34\" rx=\"5\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1.4;opacity:0.35\"\/><text x=\"666.0\" y=\"244\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:13px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">91<\/text><text x=\"328\" y=\"272\" text-anchor=\"start\" style=\"fill:#e8833a;font-size:11px;font-weight:400\">inicio=4, fin=4, medio=4 \u2192 \u00a1encontrado!<\/text><\/svg><figcaption style=\"color:var(--muted,#5a6776)\">Figura. B\u00fasqueda binaria de 23: en cada paso se mira el elemento del medio y se descarta la mitad que no puede contenerlo.<\/figcaption><\/figure>\n<pre><code>\/\/ Pseudoc\u00f3digo\nfuncion busqueda_binaria(A, x, inicio, fin): entero\n    si inicio &gt; fin entonces\n        retornar -1                                    \/\/ caso base: no est\u00e1\n    fin si\n    medio \u2190 (inicio + fin) div 2\n    si A[medio] = x entonces\n        retornar medio                                 \/\/ caso base: encontrado\n    sino si x &lt; A[medio] entonces\n        retornar busqueda_binaria(A, x, inicio, medio - 1)\n    sino\n        retornar busqueda_binaria(A, x, medio + 1, fin)\n    fin si\nfin funcion\n\n# Python\ndef busqueda_binaria(a, x, inicio=0, fin=None):\n    \"\"\"Precondici\u00f3n: a est\u00e1 ordenada de menor a mayor. Devuelve una posici\u00f3n de x, o -1.\"\"\"\n    if fin is None:\n        fin = len(a) - 1\n    if inicio &gt; fin:\n        return -1\n    medio = (inicio + fin) \/\/ 2\n    if a[medio] == x:\n        return medio\n    if x &lt; a[medio]:\n        return busqueda_binaria(a, x, inicio, medio - 1)\n    return busqueda_binaria(a, x, medio + 1, fin)\n\n\/\/ C++\n#include &lt;vector&gt;\n\nint busqueda_binaria(const std::vector&lt;int&gt;&amp; a, int x, int inicio, int fin) {\n    if (inicio &gt; fin) return -1;\n    int medio = inicio + (fin - inicio) \/ 2;       \/\/ evita desbordar con \u00edndices grandes\n    if (a[medio] == x) return medio;\n    if (x &lt; a[medio]) return busqueda_binaria(a, x, inicio, medio - 1);\n    return busqueda_binaria(a, x, medio + 1, fin);\n}<\/code><\/pre>\n<ul>\n<li><strong>Dividir<\/strong>: elegir la mitad. <strong>Conquistar<\/strong>: buscar en ella. <strong>Combinar<\/strong>: nada, el resultado de la mitad es el resultado.<\/li>\n<li>Recurrencia: <em>T<\/em>(<em>n<\/em>) = <em>T<\/em>(<em>n<\/em>\/2) + \u0398(1), es decir, \u0398(log <em>n<\/em>): con un mill\u00f3n de elementos alcanzan 20 pasos (2<sup>20<\/sup> &gt; 10<sup>6<\/sup>), contra hasta un mill\u00f3n de la b\u00fasqueda secuencial.<\/li>\n<li>Es recursiva de cola, as\u00ed que la versi\u00f3n iterativa es directa (ver <a href=\"https:\/\/www.luchonet.com.ar\/aed\/?page_id=393\">recursi\u00f3n e iteraci\u00f3n<\/a>): <code>while inicio &lt;= fin<\/code> con <code>inicio = medio + 1<\/code> o <code>fin = medio - 1<\/code>.<\/li>\n<li><code>inicio + (fin - inicio) \/ 2<\/code> evita el desbordamiento de <code>(inicio + fin) \/ 2<\/code> cuando los \u00edndices son enormes: ese error estuvo durante a\u00f1os en implementaciones reales, incluida la biblioteca de Java (lo document\u00f3 Joshua Bloch en 2006).<\/li>\n<li>La biblioteca ya la trae: <code>bisect<\/code> en Python y <code>std::lower_bound<\/code> \/ <code>std::binary_search<\/code> en C++.<\/li>\n<\/ul>\n<h2>Merge Sort (ordenamiento por mezcla)<\/h2>\n<p>Merge Sort, propuesto por John von Neumann en 1945, ordena as\u00ed:<\/p>\n<ol>\n<li><strong>Dividir<\/strong> la lista en dos mitades.<\/li>\n<li><strong>Conquistar<\/strong>: ordenar cada mitad recursivamente (una lista de 0 o 1 elementos ya est\u00e1 ordenada).<\/li>\n<li><strong>Combinar<\/strong>: <strong>mezclar<\/strong> las dos mitades ordenadas en una sola lista ordenada.<\/li>\n<\/ol>\n<figure class=\"chart\" style=\"background:var(--surface,#fff);padding:12px 12px 4px\"><svg viewBox=\"0 0 760 380\" role=\"img\" aria-label=\"Merge Sort sobre 38 27 43 3 9 82 10: divisi\u00f3n y mezcla\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:100%;height:auto;font-family:Inter,system-ui,sans-serif\"><defs><marker id=\"msd-a\" viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\" markerWidth=\"7\" markerHeight=\"7\" orient=\"auto-start-reverse\"><path d=\"M0,0 L10,5 L0,10 z\" style=\"fill:var(--muted, #5a6776)\"\/><\/marker><\/defs><rect x=\"209.0\" y=\"17\" width=\"342\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--accent, #1f6fb8);stroke-width:1.6\"\/><rect x=\"213.0\" y=\"20\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"236.0\" y=\"40\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"261.0\" y=\"20\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"284.0\" y=\"40\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"309.0\" y=\"20\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"332.0\" y=\"40\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"357.0\" y=\"20\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"380.0\" y=\"40\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"405.0\" y=\"20\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"428.0\" y=\"40\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"453.0\" y=\"20\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"476.0\" y=\"40\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><rect x=\"501.0\" y=\"20\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"524.0\" y=\"40\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"200.0\" y=\"67\" width=\"150\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--accent, #1f6fb8);stroke-width:1.6\"\/><rect x=\"204.0\" y=\"70\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"227.0\" y=\"90\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"252.0\" y=\"70\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"275.0\" y=\"90\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"300.0\" y=\"70\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"323.0\" y=\"90\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"362.0\" y=\"67\" width=\"198\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--accent, #1f6fb8);stroke-width:1.6\"\/><rect x=\"366.0\" y=\"70\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"389.0\" y=\"90\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"414.0\" y=\"70\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"437.0\" y=\"90\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"462.0\" y=\"70\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"485.0\" y=\"90\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><rect x=\"510.0\" y=\"70\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"533.0\" y=\"90\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"182.0\" y=\"117\" width=\"54\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--accent, #1f6fb8);stroke-width:1.6\"\/><rect x=\"186.0\" y=\"120\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"209.0\" y=\"140\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"248.0\" y=\"117\" width=\"102\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--accent, #1f6fb8);stroke-width:1.6\"\/><rect x=\"252.0\" y=\"120\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"275.0\" y=\"140\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"300.0\" y=\"120\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"323.0\" y=\"140\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"362.0\" y=\"117\" width=\"102\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--accent, #1f6fb8);stroke-width:1.6\"\/><rect x=\"366.0\" y=\"120\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"389.0\" y=\"140\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"414.0\" y=\"120\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"437.0\" y=\"140\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"476.0\" y=\"117\" width=\"102\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--accent, #1f6fb8);stroke-width:1.6\"\/><rect x=\"480.0\" y=\"120\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"503.0\" y=\"140\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><rect x=\"528.0\" y=\"120\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"551.0\" y=\"140\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"155.0\" y=\"167\" width=\"54\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--muted, #5a6776);stroke-width:1.6\"\/><rect x=\"159.0\" y=\"170\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"182.0\" y=\"190\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"221.0\" y=\"167\" width=\"54\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--muted, #5a6776);stroke-width:1.6\"\/><rect x=\"225.0\" y=\"170\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"248.0\" y=\"190\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"287.0\" y=\"167\" width=\"54\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--muted, #5a6776);stroke-width:1.6\"\/><rect x=\"291.0\" y=\"170\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"314.0\" y=\"190\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"353.0\" y=\"167\" width=\"54\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--muted, #5a6776);stroke-width:1.6\"\/><rect x=\"357.0\" y=\"170\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"380.0\" y=\"190\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"419.0\" y=\"167\" width=\"54\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--muted, #5a6776);stroke-width:1.6\"\/><rect x=\"423.0\" y=\"170\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"446.0\" y=\"190\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"485.0\" y=\"167\" width=\"54\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--muted, #5a6776);stroke-width:1.6\"\/><rect x=\"489.0\" y=\"170\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"512.0\" y=\"190\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><rect x=\"551.0\" y=\"167\" width=\"54\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:var(--muted, #5a6776);stroke-width:1.6\"\/><rect x=\"555.0\" y=\"170\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"578.0\" y=\"190\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"182.0\" y=\"217\" width=\"54\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:#2f9e6b;stroke-width:1.6\"\/><rect x=\"186.0\" y=\"220\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"209.0\" y=\"240\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"248.0\" y=\"217\" width=\"102\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:#2f9e6b;stroke-width:1.6\"\/><rect x=\"252.0\" y=\"220\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"275.0\" y=\"240\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"300.0\" y=\"220\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"323.0\" y=\"240\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"362.0\" y=\"217\" width=\"102\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:#2f9e6b;stroke-width:1.6\"\/><rect x=\"366.0\" y=\"220\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"389.0\" y=\"240\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"414.0\" y=\"220\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"437.0\" y=\"240\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"476.0\" y=\"217\" width=\"102\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:#2f9e6b;stroke-width:1.6\"\/><rect x=\"480.0\" y=\"220\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"503.0\" y=\"240\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"528.0\" y=\"220\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"551.0\" y=\"240\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><rect x=\"200.0\" y=\"267\" width=\"150\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:#2f9e6b;stroke-width:1.6\"\/><rect x=\"204.0\" y=\"270\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"227.0\" y=\"290\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"252.0\" y=\"270\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"275.0\" y=\"290\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"300.0\" y=\"270\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"323.0\" y=\"290\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"362.0\" y=\"267\" width=\"198\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:#2f9e6b;stroke-width:1.6\"\/><rect x=\"366.0\" y=\"270\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"389.0\" y=\"290\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"414.0\" y=\"270\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"437.0\" y=\"290\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"462.0\" y=\"270\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"485.0\" y=\"290\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"510.0\" y=\"270\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"533.0\" y=\"290\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><rect x=\"209.0\" y=\"317\" width=\"342\" height=\"36\" rx=\"7\" style=\"fill:none;stroke:#2f9e6b;stroke-width:1.6\"\/><rect x=\"213.0\" y=\"320\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"236.0\" y=\"340\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">3<\/text><rect x=\"261.0\" y=\"320\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"284.0\" y=\"340\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">9<\/text><rect x=\"309.0\" y=\"320\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"332.0\" y=\"340\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">10<\/text><rect x=\"357.0\" y=\"320\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"380.0\" y=\"340\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">27<\/text><rect x=\"405.0\" y=\"320\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"428.0\" y=\"340\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">38<\/text><rect x=\"453.0\" y=\"320\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"476.0\" y=\"340\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">43<\/text><rect x=\"501.0\" y=\"320\" width=\"46\" height=\"30\" rx=\"4\" style=\"fill:var(--surface,#fff);stroke:var(--line, #dde4ec);stroke-width:1\"\/><text x=\"524.0\" y=\"340\" text-anchor=\"middle\" style=\"fill:var(--text, #16202c);font-size:12px;font-weight:400;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace\">82<\/text><text x=\"14\" y=\"40\" text-anchor=\"start\" style=\"fill:var(--accent, #1f6fb8);font-size:11px;font-weight:700\">dividir<\/text><text x=\"14\" y=\"190\" text-anchor=\"start\" style=\"fill:var(--muted, #5a6776);font-size:11px;font-weight:700\">casos base<\/text><text x=\"14\" y=\"340\" text-anchor=\"start\" style=\"fill:#2f9e6b;font-size:11px;font-weight:700\">mezclar<\/text><text x=\"380\" y=\"372\" text-anchor=\"middle\" style=\"fill:var(--muted, #5a6776);font-size:11px;font-weight:400\">Hacia abajo se divide por la mitad hasta tener listas de un elemento; hacia arriba se mezclan pares de listas ordenadas.<\/text><\/svg><figcaption style=\"color:var(--muted,#5a6776)\">Figura. Merge Sort: dividir hasta lo trivial y combinar mezclando.<\/figcaption><\/figure>\n<p>La clave es la mezcla: dos listas ya ordenadas se combinan en tiempo lineal, mirando solo el primer elemento pendiente de cada una y tomando el menor.<\/p>\n<pre><code># Python\ndef merge_sort(datos):\n    if len(datos) &lt;= 1:\n        return datos                              # caso base\n    medio = len(datos) \/\/ 2\n    izquierda = merge_sort(datos[:medio])\n    derecha = merge_sort(datos[medio:])\n    return mezclar(izquierda, derecha)\n\ndef mezclar(a, b):\n    \"\"\"a y b ordenadas; devuelve una lista ordenada con todos sus elementos.\"\"\"\n    resultado = []\n    i = j = 0\n    while i &lt; len(a) and j &lt; len(b):\n        if a[i] &lt;= b[j]:                          # &lt;= mantiene el orden de los iguales\n            resultado.append(a[i])\n            i += 1\n        else:\n            resultado.append(b[j])\n            j += 1\n    resultado.extend(a[i:])                       # lo que qued\u00f3 de una de las dos\n    resultado.extend(b[j:])\n    return resultado\n\nprint(merge_sort([38, 27, 43, 3, 9, 82, 10]))     # [3, 9, 10, 27, 38, 43, 82]\n\n\/\/ C++: ordena v[ini, fin) usando un vector auxiliar para mezclar\n#include &lt;vector&gt;\n\nvoid mezclar(std::vector&lt;int&gt;&amp; v, int ini, int med, int fin) {\n    std::vector&lt;int&gt; aux;\n    aux.reserve(fin - ini);\n    int i = ini, j = med;\n    while (i &lt; med &amp;&amp; j &lt; fin) {\n        if (v[i] &lt;= v[j]) aux.push_back(v[i++]);\n        else aux.push_back(v[j++]);\n    }\n    while (i &lt; med) aux.push_back(v[i++]);\n    while (j &lt; fin) aux.push_back(v[j++]);\n    for (int k = 0; k &lt; (int)aux.size(); k++) v[ini + k] = aux[k];\n}\n\nvoid merge_sort(std::vector&lt;int&gt;&amp; v, int ini, int fin) {\n    if (fin - ini &lt;= 1) return;\n    int med = ini + (fin - ini) \/ 2;\n    merge_sort(v, ini, med);\n    merge_sort(v, med, fin);\n    mezclar(v, ini, med, fin);\n}\n\/\/ uso: merge_sort(v, 0, v.size());<\/code><\/pre>\n<ul>\n<li>Recurrencia: <em>T<\/em>(<em>n<\/em>) = 2<em>T<\/em>(<em>n<\/em>\/2) + \u0398(<em>n<\/em>) = \u0398(<em>n<\/em> log <em>n<\/em>), en <strong>todos<\/strong> los casos (el \u00e1rbol de recursi\u00f3n est\u00e1 en la <a href=\"https:\/\/www.luchonet.com.ar\/aed\/?page_id=398\">p\u00e1gina anterior<\/a>). Para 1000 elementos al azar hizo 8724 comparaciones; el ordenamiento de burbuja har\u00eda 499 500.<\/li>\n<li>Es <strong>estable<\/strong>: los elementos iguales conservan su orden relativo (por eso <code>&lt;=<\/code> en la mezcla).<\/li>\n<li>Necesita memoria auxiliar \u0398(<em>n<\/em>) para mezclar, m\u00e1s \u0398(log <em>n<\/em>) de pila.<\/li>\n<li>El algoritmo de ordenamiento de Python (<code>sorted<\/code>, <code>list.sort<\/code>) es Timsort, una variante muy refinada de Merge Sort.<\/li>\n<\/ul>\n<h2>Quicksort<\/h2>\n<p>Quicksort, inventado por Tony Hoare en 1959 (y publicado en 1961), pone el trabajo en la divisi\u00f3n en lugar de la combinaci\u00f3n:<\/p>\n<ol>\n<li><strong>Dividir<\/strong>: elegir un elemento, el <strong>pivote<\/strong>, y <strong>particionar<\/strong> la lista: los menores que el pivote a su izquierda y los dem\u00e1s a su derecha. El pivote queda en su posici\u00f3n definitiva.<\/li>\n<li><strong>Conquistar<\/strong>: ordenar recursivamente la parte izquierda y la derecha.<\/li>\n<li><strong>Combinar<\/strong>: nada; al terminar, la lista ya est\u00e1 ordenada.<\/li>\n<\/ol>\n<pre><code># Python: ordena v[ini..fin] en el lugar (partici\u00f3n de Lomuto, pivote al azar)\nimport random\n\ndef quicksort(v, ini=0, fin=None):\n    if fin is None:\n        fin = len(v) - 1\n    if ini &gt;= fin:\n        return                                  # 0 o 1 elementos\n    p = particionar(v, ini, fin)\n    quicksort(v, ini, p - 1)\n    quicksort(v, p + 1, fin)\n\ndef particionar(v, ini, fin):\n    k = random.randint(ini, fin)                # pivote al azar, llevado al final\n    v[k], v[fin] = v[fin], v[k]\n    pivote = v[fin]\n    i = ini                                     # v[ini..i-1] son menores que el pivote\n    for j in range(ini, fin):\n        if v[j] &lt; pivote:\n            v[i], v[j] = v[j], v[i]\n            i += 1\n    v[i], v[fin] = v[fin], v[i]                 # el pivote, a su lugar definitivo\n    return i\n\ndatos = [38, 27, 43, 3, 9, 82, 10]\nquicksort(datos)\nprint(datos)                                    # [3, 9, 10, 27, 38, 43, 82]\n\n\/\/ C++ (pivote: el \u00faltimo elemento)\n#include &lt;utility&gt;\n#include &lt;vector&gt;\n\nint particionar(std::vector&lt;int&gt;&amp; v, int ini, int fin) {\n    int pivote = v[fin];\n    int i = ini;\n    for (int j = ini; j &lt; fin; j++) {\n        if (v[j] &lt; pivote) std::swap(v[i++], v[j]);\n    }\n    std::swap(v[i], v[fin]);\n    return i;\n}\n\nvoid quicksort(std::vector&lt;int&gt;&amp; v, int ini, int fin) {\n    if (ini &gt;= fin) return;\n    int p = particionar(v, ini, fin);\n    quicksort(v, ini, p - 1);\n    quicksort(v, p + 1, fin);\n}\n\/\/ uso: quicksort(v, 0, v.size() - 1);<\/code><\/pre>\n<p>Su costo depende de qu\u00e9 tan pareja sea la partici\u00f3n:<\/p>\n<ul>\n<li><strong>Caso promedio<\/strong> (pivotes razonables): <em>T<\/em>(<em>n<\/em>) \u2248 2<em>T<\/em>(<em>n<\/em>\/2) + \u0398(<em>n<\/em>) = \u0398(<em>n<\/em> log <em>n<\/em>). Con pivote al azar, el promedio de comparaciones es de unas 1,39 \u00b7 <em>n<\/em> log<sub>2<\/sub> <em>n<\/em> para <em>n<\/em> grande; para 1000 elementos, en 20 pruebas, dio en promedio unas 11 000 (la f\u00f3rmula exacta tiene t\u00e9rminos de orden <em>n<\/em> que, con <em>n<\/em> = 1000, todav\u00eda restan bastante).<\/li>\n<li><strong>Peor caso<\/strong> (el pivote es siempre el m\u00ednimo o el m\u00e1ximo): <em>T<\/em>(<em>n<\/em>) = <em>T<\/em>(<em>n<\/em> \u2212 1) + \u0398(<em>n<\/em>) = \u0398(<em>n<\/em><sup>2<\/sup>), con profundidad <em>n<\/em>. Con el \u00faltimo elemento como pivote, eso pasa justamente con una lista <strong>ya ordenada<\/strong>: para 500 elementos hace 124 750 comparaciones, las mismas que el ordenamiento de burbuja. Elegir el pivote al azar hace que el peor caso sea extremadamente improbable para cualquier entrada sin elementos repetidos. Con muchos elementos iguales, en cambio, la partici\u00f3n de Lomuto (que compara con <code>&lt;<\/code>) queda siempre desbalanceada y vuelve a ser cuadr\u00e1tica; se resuelve con una partici\u00f3n en tres partes (menores, iguales, mayores) o con la partici\u00f3n original de Hoare.<\/li>\n<li>Ordena <strong>en el lugar<\/strong> (sin lista auxiliar), y en la pr\u00e1ctica suele ser m\u00e1s r\u00e1pido que Merge Sort por su buen uso de la memoria cach\u00e9. No es estable. Las implementaciones habituales de <code>std::sort<\/code> en C++ usan Introsort, un Quicksort que cambia a otro m\u00e9todo si la recursi\u00f3n se vuelve demasiado profunda.<\/li>\n<\/ul>\n<h2>Potencia r\u00e1pida<\/h2>\n<p>Para calcular <em>x<\/em><sup><em>n<\/em><\/sup>, el m\u00e9todo directo hace <em>n<\/em> \u2212 1 multiplicaciones. Dividiendo el exponente por la mitad alcanza con entre log<sub>2<\/sub> <em>n<\/em> y 2 log<sub>2<\/sub> <em>n<\/em> (una o dos por cada mitad):<\/p>\n<span class=\"wp-katex-eq katex-display\" data-display=\"true\">x^n = \\begin{cases} 1 &amp; \\text{si } n = 0 \\\\ \\left(x^{n\/2}\\right)^2 &amp; \\text{si } n \\text{ es par} \\\\ \\left(x^{(n-1)\/2}\\right)^2 \\cdot x &amp; \\text{si } n \\text{ es impar} \\end{cases}<\/span>\n<pre><code># Python\ndef potencia(x, n):\n    if n == 0:\n        return 1\n    y = potencia(x, n \/\/ 2)          # UNA sola llamada, guardada en y\n    if n % 2 == 0:\n        return y * y\n    return y * y * x\n\nprint(potencia(2, 10))               # 1024\n\n\/\/ C++\nlong long potencia(long long x, int n) {\n    if (n == 0) return 1;\n    long long y = potencia(x, n \/ 2);\n    if (n % 2 == 0) return y * y;\n    return y * y * x;\n}<\/code><\/pre>\n<p>Recurrencia: <em>T<\/em>(<em>n<\/em>) = <em>T<\/em>(<em>n<\/em>\/2) + \u0398(1) = \u0398(log <em>n<\/em>). Por ejemplo, con este c\u00f3digo <em>x<\/em><sup>1000<\/sup> necesita 16 multiplicaciones en lugar de 999. Es esencial guardar <code>y<\/code>: escribir <code>potencia(x, n \/\/ 2) * potencia(x, n \/\/ 2)<\/code> har\u00eda dos llamadas y el costo volver\u00eda a ser \u0398(<em>n<\/em>). La misma idea, calculando cada producto m\u00f3dulo <em>m<\/em>, es la <strong>exponenciaci\u00f3n modular<\/strong> en la que se basa la criptograf\u00eda de clave p\u00fablica (RSA); Python la ofrece como <code>pow(x, n, m)<\/code>.<\/p>\n<h2>M\u00e1s all\u00e1 de estos ejemplos<\/h2>\n<table>\n<thead>\n<tr>\n<td>Algoritmo<\/td>\n<td>Dividir \/ combinar<\/td>\n<td>Costo<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>B\u00fasqueda binaria<\/td>\n<td>1 mitad \/ nada<\/td>\n<td>\u0398(log <em>n<\/em>)<\/td>\n<\/tr>\n<tr>\n<td>Potencia r\u00e1pida<\/td>\n<td>exponente a la mitad \/ 1 o 2 productos<\/td>\n<td>\u0398(log <em>n<\/em>)<\/td>\n<\/tr>\n<tr>\n<td>Merge Sort<\/td>\n<td>2 mitades \/ mezcla lineal<\/td>\n<td>\u0398(<em>n<\/em> log <em>n<\/em>)<\/td>\n<\/tr>\n<tr>\n<td>Quicksort<\/td>\n<td>partici\u00f3n lineal \/ nada<\/td>\n<td>\u0398(<em>n<\/em> log <em>n<\/em>) promedio, \u0398(<em>n<\/em><sup>2<\/sup>) peor<\/td>\n<\/tr>\n<tr>\n<td>Multiplicaci\u00f3n de Karatsuba<\/td>\n<td>3 productos de la mitad de cifras \/ sumas<\/td>\n<td>\u0398(<em>n<\/em><sup>log<sub>2<\/sub> 3<\/sup>)<\/td>\n<\/tr>\n<tr>\n<td>Par de puntos m\u00e1s cercanos<\/td>\n<td>2 mitades \/ franja central<\/td>\n<td>\u0398(<em>n<\/em> log <em>n<\/em>)<\/td>\n<\/tr>\n<tr>\n<td>Transformada r\u00e1pida de Fourier (FFT)<\/td>\n<td>pares e impares \/ combinaci\u00f3n lineal<\/td>\n<td>\u0398(<em>n<\/em> log <em>n<\/em>)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Divide y vencer\u00e1s funciona mejor cuando los subproblemas son <strong>independientes<\/strong> y de tama\u00f1o parecido. Si los subproblemas se <strong>superponen<\/strong> (como en Fibonacci, donde <em>F<\/em>(<em>n<\/em> \u2212 1) y <em>F<\/em>(<em>n<\/em> \u2212 2) comparten casi todo el trabajo), dividir sin m\u00e1s recalcula lo mismo una y otra vez: para eso est\u00e1 la t\u00e9cnica de la p\u00e1gina siguiente, <a href=\"https:\/\/www.luchonet.com.ar\/aed\/?page_id=409\">la memoizaci\u00f3n y la programaci\u00f3n din\u00e1mica<\/a>.<\/p>\n<h2>Errores frecuentes<\/h2>\n<ul>\n<li><strong>Aplicar la b\u00fasqueda binaria a una lista no ordenada<\/strong>: el resultado no tiene sentido.<\/li>\n<li><strong>Errores de una posici\u00f3n en los l\u00edmites<\/strong>: usar <code>medio<\/code> en lugar de <code>medio - 1<\/code> o <code>medio + 1<\/code> produce bucles infinitos o recursi\u00f3n sin fin cuando quedan uno o dos elementos.<\/li>\n<li><strong>Calcular <code>(inicio + fin) \/ 2<\/code> con \u00edndices enormes<\/strong> en C++: puede desbordar.<\/li>\n<li><strong>Mezclar con <code>&lt;<\/code> en lugar de <code>&lt;=<\/code><\/strong>: Merge Sort deja de ser estable.<\/li>\n<li><strong>Quicksort con pivote fijo sobre datos ya ordenados<\/strong>, o con muchos elementos iguales y la partici\u00f3n de Lomuto: cae en \u0398(<em>n<\/em><sup>2<\/sup>) y en recursi\u00f3n de profundidad <em>n<\/em>.<\/li>\n<li><strong>Llamar dos veces al mismo subproblema<\/strong>, como en la potencia sin guardar <code>y<\/code>.<\/li>\n<\/ul>\n<h2>Ejercicios propuestos<\/h2>\n<ol>\n<li>Usando b\u00fasqueda binaria, escribir <code>raiz_entera(n)<\/code> que encuentre el mayor <em>r<\/em> con <em>r<\/em><sup>2<\/sup> \u2264 <em>n<\/em> en \u0398(log <em>n<\/em>) pasos (compararla con la versi\u00f3n de <a href=\"https:\/\/www.luchonet.com.ar\/aed\/?page_id=367\">dise\u00f1o de subprogramas<\/a>, que es \u0398(\u221a<em>n<\/em>)).<\/li>\n<li>Escribir una b\u00fasqueda binaria que devuelva la <strong>primera<\/strong> posici\u00f3n de <em>x<\/em> en una lista ordenada con repetidos (por ejemplo, en <code>[1, 2, 2, 2, 5]<\/code>, la primera posici\u00f3n de 2 es 1).<\/li>\n<li>Mostrar los pasos de Merge Sort sobre <code>[5, 1, 4, 2, 3]<\/code>: las divisiones y cada mezcla.<\/li>\n<li>\u00bfCu\u00e1ntas comparaciones hace la partici\u00f3n de Quicksort (con el \u00faltimo elemento como pivote) sobre la lista ya ordenada <code>[1, 2, \u2026, n]<\/code> en total? \u00bfQu\u00e9 profundidad alcanza la recursi\u00f3n?<\/li>\n<li>Escribir la exponenciaci\u00f3n modular <code>potencia_mod(x, n, m)<\/code> y comprobarla con <code>pow(x, n, m)<\/code> para <code>x = 7<\/code>, <code>n = 128<\/code>, <code>m = 13<\/code>.<\/li>\n<\/ol>\n<details>\n<summary><strong>Ver soluciones<\/strong><\/summary>\n<ol>\n<li>\n<pre><code>def raiz_entera(n):\n    \"\"\"Precondici\u00f3n: n &gt;= 0. Devuelve el mayor r con r*r &lt;= n.\"\"\"\n    inicio, fin = 0, n\n    while inicio &lt; fin:\n        medio = (inicio + fin + 1) \/\/ 2      # redondeo hacia arriba\n        if medio * medio &lt;= n:\n            inicio = medio                   # medio sirve: la respuesta es &gt;= medio\n        else:\n            fin = medio - 1                  # medio es demasiado grande\n    return inicio\n\nassert [raiz_entera(k) for k in (0, 1, 15, 16, 17)] == [0, 1, 3, 4, 4]<\/code><\/pre>\n<p>Se busca en el rango de las respuestas posibles [0, <em>n<\/em>]. El redondeo hacia arriba del medio evita un bucle infinito cuando quedan dos candidatos. Para <em>n<\/em> = 10<sup>12<\/sup> hace unos 40 pasos, contra un mill\u00f3n de la versi\u00f3n que prueba uno por uno.<\/li>\n<li>\n<pre><code>def primera_posicion(a, x):\n    inicio, fin = 0, len(a) - 1\n    resultado = -1\n    while inicio &lt;= fin:\n        medio = (inicio + fin) \/\/ 2\n        if a[medio] == x:\n            resultado = medio        # candidato, pero puede haber uno antes\n            fin = medio - 1\n        elif a[medio] &lt; x:\n            inicio = medio + 1\n        else:\n            fin = medio - 1\n    return resultado\n\nassert primera_posicion([1, 2, 2, 2, 5], 2) == 1<\/code><\/pre>\n<p>Al encontrarlo, no se detiene: sigue buscando en la mitad izquierda. Sigue siendo \u0398(log <em>n<\/em>). Algo parecido hacen <code>bisect.bisect_left<\/code> y <code>std::lower_bound<\/code>: devuelven la primera posici\u00f3n con un valor \u2265 <em>x<\/em> (el punto donde se insertar\u00eda), y hay que comprobar aparte si all\u00ed est\u00e1 <em>x<\/em>.<\/li>\n<li>Divisi\u00f3n, un nivel por l\u00ednea: nivel 1, [5, 1] | [4, 2, 3]; nivel 2, [5] | [1] | [4] | [2, 3]; nivel 3, [2] | [3]. Mezclas, de abajo hacia arriba: [5] + [1] \u2192 [1, 5]; [2] + [3] \u2192 [2, 3]; [4] + [2, 3] \u2192 [2, 3, 4]; [1, 5] + [2, 3, 4] \u2192 [1, 2, 3, 4, 5].<\/li>\n<li>Cada partici\u00f3n sobre un segmento de largo <em>k<\/em> compara los <em>k<\/em> \u2212 1 elementos con el pivote, que es el m\u00e1ximo y queda al final; la parte derecha es vac\u00eda y la izquierda tiene <em>k<\/em> \u2212 1 elementos. Total: (<em>n<\/em> \u2212 1) + (<em>n<\/em> \u2212 2) + \u2026 + 1 = <em>n<\/em>(<em>n<\/em> \u2212 1)\/2 comparaciones (124 750 para <em>n<\/em> = 500), y profundidad de recursi\u00f3n <em>n<\/em>. Si en el Quicksort de Python se usara el \u00faltimo elemento como pivote (sin el intercambio al azar), con listas de m\u00e1s de unos mil elementos se producir\u00eda <code>RecursionError<\/code>.<\/li>\n<li>\n<pre><code>def potencia_mod(x, n, m):\n    if n == 0:\n        return 1 % m\n    y = potencia_mod(x, n \/\/ 2, m)\n    y = y * y % m\n    if n % 2 == 1:\n        y = y * x % m\n    return y\n\nassert potencia_mod(7, 128, 13) == pow(7, 128, 13) == 3<\/code><\/pre>\n<p>Reducir m\u00f3dulo <em>m<\/em> en cada paso mantiene los n\u00fameros chicos aunque <em>x<\/em><sup><em>n<\/em><\/sup> sea gigantesco.<\/li>\n<\/ol>\n<\/details>\n<h2>Resumen<\/h2>\n<ul>\n<li><strong>Divide y vencer\u00e1s<\/strong>: dividir en subproblemas m\u00e1s chicos, resolverlos recursivamente y combinar sus soluciones.<\/li>\n<li>La <strong>b\u00fasqueda binaria<\/strong> descarta la mitad en cada paso: \u0398(log <em>n<\/em>), sobre datos ordenados.<\/li>\n<li><strong>Merge Sort<\/strong> divide trivialmente y combina mezclando: \u0398(<em>n<\/em> log <em>n<\/em>) siempre, estable, con memoria auxiliar.<\/li>\n<li><strong>Quicksort<\/strong> divide particionando y no necesita combinar: \u0398(<em>n<\/em> log <em>n<\/em>) en promedio, \u0398(<em>n<\/em><sup>2<\/sup>) en el peor caso; con pivote al azar el peor caso es muy improbable, salvo con muchos elementos repetidos.<\/li>\n<li>La <strong>potencia r\u00e1pida<\/strong> calcula <em>x<\/em><sup><em>n<\/em><\/sup> con \u0398(log <em>n<\/em>) multiplicaciones, siempre que se haga una sola llamada.<\/li>\n<li>La t\u00e9cnica funciona mejor con subproblemas independientes; si se superponen, conviene la programaci\u00f3n din\u00e1mica.<\/li>\n<\/ul>\n<p>Anterior: <a href=\"https:\/\/www.luchonet.com.ar\/aed\/?page_id=398\">An\u00e1lisis de algoritmos recursivos<\/a> \u00b7 Siguiente: <a href=\"https:\/\/www.luchonet.com.ar\/aed\/?page_id=409\">Memoizaci\u00f3n y programaci\u00f3n din\u00e1mica<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Divide y vencer\u00e1s (divide and conquer) es una de las t\u00e9cnicas de dise\u00f1o de algoritmos m\u00e1s productivas, y es recursiva por naturaleza. Consiste en tres pasos: Dividir el problema en subproblemas del mismo tipo, m\u00e1s chicos (normalmente, de una fracci\u00f3n del tama\u00f1o: la mitad, un tercio). Conquistar: resolver cada subproblema recursivamente; si es lo bastante [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":308,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-404","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.luchonet.com.ar\/aed\/index.php?rest_route=\/wp\/v2\/pages\/404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.luchonet.com.ar\/aed\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.luchonet.com.ar\/aed\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.luchonet.com.ar\/aed\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.luchonet.com.ar\/aed\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=404"}],"version-history":[{"count":2,"href":"https:\/\/www.luchonet.com.ar\/aed\/index.php?rest_route=\/wp\/v2\/pages\/404\/revisions"}],"predecessor-version":[{"id":411,"href":"https:\/\/www.luchonet.com.ar\/aed\/index.php?rest_route=\/wp\/v2\/pages\/404\/revisions\/411"}],"wp:attachment":[{"href":"https:\/\/www.luchonet.com.ar\/aed\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}