{"id":611,"date":"2024-01-10T18:49:20","date_gmt":"2024-01-10T18:49:20","guid":{"rendered":"https:\/\/javigomez.org\/?p=611"},"modified":"2025-09-29T20:33:44","modified_gmt":"2025-09-29T20:33:44","slug":"u06-09-busqueda-en-arrays-ordenados","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2024\/01\/10\/u06-09-busqueda-en-arrays-ordenados\/","title":{"rendered":"U06-09-B\u00fasqueda en Arrays ordenados"},"content":{"rendered":"\n<p><strong>static<\/strong><strong> <\/strong><strong>int<\/strong><strong> <\/strong><strong>binarySearch<\/strong><strong>(tipo array[], tipo <\/strong><strong>campo_a_buscar<\/strong><strong>)<\/strong><strong>;<\/strong><\/p>\n\n\n\n<p>Cuando el elemento que buscar no se encuentra, el valor negativo devuelto tiene un significado especial: informa de la posici\u00f3n donde tendr\u00eda que colocarse el elemento buscado para que la tabla contin\u00fae ordenada. El \u00edndice de inserci\u00f3n se calcula:<\/p>\n\n\n\n<p>indiceInsercion = -pos &#8211; 1;siendo pos el valor negativo devuelto por binarySearch(). Veamos un ejemplo:<\/p>\n\n\n\n<p>int a[] = {2, 4, 5, 6, 9};<\/p>\n\n\n\n<p>int pos = Arrays.binarySearch(a, 3); \/\/pos vale -2<\/p>\n\n\n\n<p>int indiceInsercion = -pos &#8211; 1; \/\/vale 1<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package U6;\n\nimport java.util.Arrays;\nimport java.util.Random;\nimport java.util.Scanner;\n\npublic class A000 {\n\n\tpublic static void main(String&#91;] args) {\n\t\t\n\t\tint combGan&#91;]= {2,3,4,7,9,15};\n\t\tint apuesta&#91;]= {1,2,7,15,26,35};\n\t\t\n\t\tint aciertos=primitiva(combGan, apuesta);\n\t\t\n\t\tSystem.out.println(\"Los aciertos son: \" + aciertos);\n\t}\n\n\tprivate static int primitiva(int&#91;] combGan, int&#91;] apuesta) {\n\t\tint aciertos=0;\n\t\tfor (int ele:apuesta) {\n\t\t\tif (Arrays.binarySearch(combGan, ele)>=0)\n\t\t\t\taciertos++;\n\t\t}\n\t\treturn aciertos;\n\t}\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Java U6 09 binarySearch\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/Twz_vN38sYA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>static int binarySearch(tipo array[], tipo campo_a_buscar); Cuando el elemento que buscar no se encuentra, el valor negativo devuelto tiene un significado especial: informa de la &hellip; <\/p>\n","protected":false},"author":1,"featured_media":612,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[43,53,203],"class_list":["post-611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-arrays","tag-binarysearch","tag-java"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=611"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/611\/revisions"}],"predecessor-version":[{"id":900,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/611\/revisions\/900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media\/612"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}