{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "40c0a8df96934a80a8122884d7bfb6a3",
  "keywords": "动态表,表扩张,算法中阶,摊还分析,高级设计和分析技术",
  "title": "有序表的折半查找",
  "desc": [
    {
      "content": "\n问题描述：\n用有序表表示静态查找表时，通常检索函数可以用折半查找来实现。\n折半查找的查找过程是：首先确定待查记录所在的范围，然后逐步缩小范围直到找到或者确定找不到相应的记录为止。而每次需要缩小的范围均为上一次的一半，这样的查找过程可以被称为折半查找。  \n第二行包含n个用空格隔开的正整数，表示n个有序的整数。输入保证这n个整数是从小到大递增的。\n第三行包含k个用空格隔开的正整数，表示k次查询的目标。\n输出：\n只有1行，包含k个整数，分别表示每一次的查询结果。如果在查询中找到了对应的整数，则输出其相应的位置，否则输出-1。\n请在每个整数后输出一个空格，并请注意行尾输出换行。",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\nint binary( int *a, int key, int n )\n{\n\tint left = 0, right = n - 1, mid = 0;\n\tmid = ( left + right ) / 2;\n\twhile( left < right && a[mid] != key )\n\t{\n\t\tif( a[mid] < key )\n\t\tleft = mid + 1;\n\t\telse if( a[mid] > key )\n\t\tright = mid - 1;\n\t\tmid = ( left + right ) / 2;\n\t}\n\tif( a[mid] == key )   return mid;\n\treturn -1;\n}\nint main (void)\n{\n\tint Base_a[20] = {1,3,5,8,9,40,120,123,125,150,199,200,1250,1255,1900,2000,2001,3000,3950,5000};\n\tint Search_a[5] = {12,199,9,2001,3500};\n\tint result = 0x00;\n\tfor(int i = 0;i < sizeof(Search_a)/sizeof(Search_a[0]);i++)\n\t{\n\t\tresult = binary(Base_a,Search_a[i],sizeof(Base_a)/sizeof(Base_a[0]));\n\t\tprintf(\"[%d %d] \",Search_a[i],result);\n\t}\n\tprintf(\"\\n\");\n\treturn 0;\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-9a4408230ad9414ca2c6ae393d6dfc0c",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470249"
}