{"id":373,"title":"\u4e8c\u5206\u67e5\u627e\u6cd5","good":0,"bad":0,"hit":1795,"created_at":"2018-07-18 11:45:48","content":"
\u9996\u5148\uff0c\u5047\u8bbe\u8868\u4e2d\u5143\u7d20\u662f\u6309\u5347\u5e8f\u6392\u5217\uff0c\u5c06\u8868\u4e2d\u95f4\u4f4d\u7f6e\u8bb0\u5f55\u7684\u5173\u952e\u5b57\u4e0e\u67e5\u627e\u5173\u952e\u5b57\u6bd4\u8f83\uff0c\u5982\u679c\u4e24\u8005\u76f8\u7b49\uff0c\u5219\u67e5\u627e\u6210\u529f\uff1b<\/p>
\u5426\u5219\u5229\u7528\u4e2d\u95f4\u4f4d\u7f6e\u8bb0\u5f55\u5c06\u8868\u5206\u6210\u524d\u3001\u540e\u4e24\u4e2a\u5b50\u8868\uff0c\u5982\u679c\u4e2d\u95f4\u4f4d\u7f6e\u8bb0\u5f55\u7684\u5173\u952e\u5b57\u5927\u4e8e\u67e5\u627e\u5173\u952e\u5b57\uff0c\u5219\u8fdb\u4e00\u6b65\u67e5\u627e\u524d\u4e00\u5b50\u8868\uff0c\u5426\u5219\u8fdb\u4e00\u6b65\u67e5\u627e\u540e\u4e00\u5b50\u8868\u3002<\/p>
\u91cd\u590d\u4ee5\u4e0a\u8fc7\u7a0b\uff0c\u76f4\u5230\u627e\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u8bb0\u5f55\uff0c\u4f7f\u67e5\u627e\u6210\u529f\uff0c\u6216\u76f4\u5230\u5b50\u8868\u4e0d\u5b58\u5728\u4e3a\u6b62\uff0c\u6b64\u65f6\u67e5\u627e\u4e0d\u6210\u529f\u3002
<\/p>
<?php\n\nclass suanfa\n{\n\n private $data;\n\n public function __construct()\n {\n $this->data = [87, 22, 5, 1, 2, 8, 3];\n }\n\n \/**\n * \u5192\u6ce1\u6392\u5e8f\u6cd5\n * @return array\n *\/\n public function maopao()\n {\n $num = count($this->data);\n for ($i = 0; $i < $num; $i++) {\n for ($j = 0; $j < $num - 1; $j++) {\n if ($this->data[$j] > $this->data[$j + 1]) {\n $temp = $this->data[$j + 1];\n $this->data[$j + 1] = $this->data[$j];\n $this->data[$j] = $temp;\n }\n }\n }\n return $this->data;\n }\n\n \n\n \/**\n * \u4e8c\u5206\u67e5\u627e\u6cd5\uff1a\u8981\u6c42\u6570\u636e\u5fc5\u987b\u662f\u5347\u5e8f\u7684\n * @param $x\n * @return int\n *\/\n public function binsearch($x)\n {\n $a = self::maopao();\/\/\u5148\u5c06\u6570\u636e\u5347\u5e8f\u6392\u5e8f\n $c = count($a);\n $lower = 0;\n $high = $c - 1;\n while ($lower <= $high) {\n $middle = intval(($lower + $high) \/ 2);\n if ($a[$middle] > $x) {\n $high = $middle - 1;\n } elseif ($a[$middle] < $x) {\n $lower = $middle + 1;\n } else {\n return $middle;\n }\n }\n return -1;\n }\n}\n\n$obj = new suanfa();\nprint_r($obj->binsearch(22));\/\/5<\/pre>
<\/p>"}