{"id":65,"date":"2021-01-04T11:30:29","date_gmt":"2021-01-04T11:30:29","guid":{"rendered":"https:\/\/blog.gwlin.com\/?p=65"},"modified":"2023-03-31T09:52:51","modified_gmt":"2023-03-31T09:52:51","slug":"\u4e00\u4e9b\u6392\u5e8f\u7b97\u6cd5\u7684php\u5b9e\u73b0","status":"publish","type":"post","link":"https:\/\/www.gwlin.com\/blog\/posts\/65","title":{"rendered":"\u4e00\u4e9b\u6392\u5e8f\u7b97\u6cd5\u7684php\u5b9e\u73b0"},"content":{"rendered":"<p>\u5728 VisuAlgo \u4e0a\u770b\u5230\u4e00\u4e9b\u6392\u5e8f\u7b97\u6cd5\u7684\u539f\u7406\uff0c\u4e8e\u662f\u5c31\u60f3\u7740\u7528php\u6765\u505a\u4e00\u904d\u3002\u505a\u5b8c\u4e4b\u540e\u8fd8\u662f\u5f88\u6709\u6536\u83b7\u7684\u3002\u8fd9\u91cc\u8d34\u4e00\u4e0b\u4ee3\u7801\u3002\u6ca1\u6709\u4ed4\u7ec6\u6253\u78e8\uff0c\u5e94\u8be5\u591a\u591a\u5c11\u5c11\u53ef\u4ee5\u5199\u7684\u66f4\u597d\u7684\u3002<\/p>\n<p>\u8fd9\u91cc\u987a\u4fbf\u8bf4\u4e00\u4e0b\u8fd9\u4e9b\u6392\u5e8f\u7b97\u6cd5\u7684\u601d\u8def\u5427\uff0c\u4e0d\u8fc7\u90fd\u662f\u4e2a\u4eba\u7406\u89e3\uff0c\u4ec5\u5f53\u53c2\u8003\u3002<\/p>\n<p><strong>\u5192\u6ce1\u6392\u5e8f\uff1a<\/strong>\u5927\u6982\u662f\u6211\u4eec\u6700\u719f\u6089\u7684\u4e86\uff0c\u5c31\u662f\u4e0d\u65ad\u4e00\u904d\u4e00\u904d\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u7684\u5927\u5c0f\uff0c\u76f4\u5230\u6ca1\u6709\u5143\u7d20\u518d\u4ea4\u6362\u4e3a\u6b62\u3002\u56e0\u6b64\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5bf9\u6bd4\u4ea4\u6362 n\u00b2 \u6b21\u3002<\/p>\n<p><strong>\u5feb\u901f\u6392\u5e8f\uff1a<\/strong>\u8fd9\u4e2a\u7b97\u6cd5\u5728\u81ea\u5df1\u7684\u6d4b\u8bd5\u91cc\u662f\u6700\u5feb\u7684\u3002\u5176\u601d\u8def\u5c31\u662f\u9009\u62e9\u7b2c\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u4e00\u4e2a\u201c\u8f74\u201d\uff0c\u904d\u5386\u5269\u4f59\u6570\u7ec4\uff0c\u628a\u6bd4\u5b83\u5927\u7684\u653e\u53f3\u8fb9\uff0c\u6bd4\u5b83\u5c0f\u7684\u653e\u5de6\u8fb9\uff0c\u518d\u5bf9\u5de6\u53f3\u4e24\u8fb9\u9012\u5f52\u91cd\u590d\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u968f\u673a\u5feb\u901f\u6392\u5e8f\uff1a<\/strong>\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u5feb\u901f\u6392\u5e8f\u7684\u4f18\u5316\u7248\uff0c\u5b83\u4e0d\u9009\u62e9\u7b2c\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u201c\u8f74\u201d\uff0c\u800c\u662f\u968f\u673a\u9009\u62e9\uff0c\u8fd9\u80fd\u9632\u6b62\u5bf9\u4e00\u4e9b\u5df2\u7ecf\u63a5\u8fd1\u6392\u5e8f\u597d\u7684\u6570\u7ec4\u51fa\u73b0\u6548\u7387\u4f4e\u4e0b\u7684\u60c5\u51b5\uff0c\u9700\u8981 n\u00b2 \u6b21\u4ea4\u6362\u3002\u5728\u81ea\u5df1\u7684\u6d4b\u8bd5\u91cc\uff0c\u76f8\u8f83\u4e8e\u901a\u5e38\u7684\u5feb\u901f\u6392\u5e8f\uff0c\u968f\u673a\u5feb\u6392\u8c8c\u4f3c\u7a33\u5b9a\u4e00\u4e9b\uff08\u8017\u65f6\u6ce2\u52a8\u5c0f\uff09\u3002<\/p>\n<p><strong>\u9009\u62e9\u6392\u5e8f\uff1a<\/strong>\u8bf4\u767d\u4e86\u5c31\u662f\u4e0d\u65ad\u628a\u6700\u5c0f\u7684\u5143\u7d20\u79fb\u5230\u524d\u9762\u3002\u4e0d\u65ad\u904d\u5386\u5269\u4f59\u7684\u6570\u7ec4\uff0c\u627e\u5230\u6b21\u5c0f\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u63d2\u5165\u6392\u5e8f\uff1a<\/strong>\u5982\u679c\u770b\u7740 VisuAlgo\uff0c\u5176\u5b9e\u8fd8\u662f\u633a\u76f4\u89c2\u7684\u3002\u5c31\u662f\u904d\u5386\u5143\u7d20\uff0c\u5f53\u524d\u5143\u7d20\u4e0d\u65ad\u548c\u4e4b\u524d\u7684\u5143\u7d20\u6bd4\u8f83\uff0c\u76f4\u5230\u4e4b\u524d\u7684\u5143\u7d20\u6bd4\u81ea\u5df1\u5c0f\uff0c\u5e76\u79fb\u52a8\u5f53\u524d\u5143\u7d20\u5230\u8fd9\u4e2a\u5143\u7d20\u4e4b\u540e\u3002<\/p>\n<p><strong>\u5f52\u5e76\u6392\u5e8f\uff1a<\/strong>\u521a\u5f00\u59cb\u8ba9\u4eba\u633a\u96be\u7406\u89e3\u7684\u4e00\u79cd\u7b97\u6cd5\u3002\u73b0\u5728\u60f3\u6765\uff0c\u5c31\u662f\u4e0d\u65ad\u9012\u5f52\u7684\u548c\u76f8\u90bb\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u6bd4\u8f83\u3002\u4f8b\u5982\uff0c1\u52308\u7684\u6570\u7ec4\uff0c\u5206\u62101\u52308\u4e2a\u5b50\u6570\u7ec4\uff0c[1]\u548c[2]\u6bd4\u8f83\uff0c[3]\u548c[4]\u6bd4\u8f83\uff0c\u7b49\u7b49\u3002\u5b8c\u4e86\u4e4b\u540e\u5408\u5e76\u4e3a[1,2]\u548c[3,4]\uff0c\u7136\u540e[1,2]\u548c[3,4]\u7684\u5143\u7d20\u6bd4\u8f83\uff0c\u518d\u5408\u5e76\uff0c\u76f4\u5230\u9876\u5c42 \u3002<\/p>\n<p><strong>\u8ba1\u6570\u6392\u5e8f\uff1a<\/strong>\u611f\u89c9\u53ea\u9002\u7528\u4e8e\u5df2\u77e5\u4e00\u5b9a\u8303\u56f4\u7684\u968f\u673a\u6570\u7ec4\u7684\u7b97\u6cd5\u3002\u4f8b\u5982\uff0c\u5df2\u77e5\u4e00\u4e2a\u968f\u673a\u6570\u7ec4\u7684\u5143\u7d20\u7684\u8303\u56f4\u57281\u5230100\u4e4b\u95f4\uff0c\u90a3\u4e48\u76f4\u63a5\u7edf\u8ba11\u5230100\u7684\u6570\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u7136\u540e\u6839\u636e\u7edf\u8ba1\u7ed3\u679c\u91cd\u65b0\u7ec4\u5408\u6570\u7ec4\u3002<\/p>\n<p><strong>\u57fa\u6570\u6392\u5e8f\uff1a<\/strong>\u611f\u89c9\u540c\u4e0a\u3002\u5c06\u6240\u6709\u5f85\u6bd4\u8f83\u6570\u503c\uff08\u6b63\u6574\u6570\uff09\u7edf\u4e00\u4e3a\u540c\u6837\u7684\u6570\u4f4d\u957f\u5ea6\uff0c\u6570\u4f4d\u8f83\u77ed\u7684\u6570\u524d\u9762\u8865\u96f6\u3002\u7136\u540e\uff0c\u4ece\u6700\u4f4e\u4f4d\u5f00\u59cb\uff0c\u4f9d\u6b21\u8fdb\u884c\u4e00\u6b21\u6392\u5e8f\u3002\u8fd9\u6837\u4ece\u6700\u4f4e\u4f4d\u6392\u5e8f\u4e00\u76f4\u5230\u6700\u9ad8\u4f4d\u6392\u5e8f\u5b8c\u6210\u4ee5\u540e\uff0c\u6570\u5217\u5c31\u53d8\u6210\u4e00\u4e2a\u6709\u5e8f\u5e8f\u5217\u3002<code class=\"prettyprint\"  class=\"php\">\n<\/code><\/p>\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\"  lang=\"php\" class=\"language-php\">&lt;?php\nset_time_limit(0);\nini_set(&#039;display_errors&#039;, &#039;On&#039;);\nerror_reporting(7);\n\n\/**\n * \u521b\u5efa\u968f\u673a\u6570\u7ec4\n * @param int $length\n * @return array\n *\/\nfunction create($length = 100)\n{\n    $array = [];\n    for ($i = 0; $i &amp;lt; $length; $i++) {\n        $array[] = mt_rand(0, 999);\n    }\n    return $array;\n}\n\n\/**\n * \u5192\u6ce1\u6392\u5e8f\n * @param $array\n * @return mixed\n *\/\nfunction bubble($array)\n{\n    do {\n        $has = 0;\n        for ($i = 0; $i 1; $i++) {\n            if ($array[$i] &amp;gt; $array[$i + 1]) {\n                $temp = $array[$i];\n                $array[$i] = $array[$i + 1];\n                $array[$i + 1] = $temp;\n                $has++;\n            }\n        }\n    } while ($has);\n    return $array;\n}\n\n\/**\n * \u5feb\u901f\u6392\u5e8f\n * @param $array\n * @return array\n *\/\nfunction quick($array)\n{\n    $length = count($array);\n    if ($length &amp;lt;= 1) {\n        return $array;\n    } else {\n        $pivot = $array[0];\n        $less = $greater = [];\n        for ($i = 1; $i &amp;lt; $length; $i++) {\n            if ($array[$i] &amp;lt; $pivot) {\n                $less[] = $array[$i];\n            } else {\n                $greater[] = $array[$i];\n            }\n        }\n        return array_merge(quick($less), [$pivot], quick($greater));\n    }\n}\n\n\/**\n * \u9009\u62e9\u6392\u5e8f\n * @param $array\n * @return mixed\n *\/\nfunction selection($array)\n{\n    $count = count($array);\n    for ($i = 0; $i &amp;lt; $count - 1; $i++) {\n        $min = $array[$i];\n        $key = $i;\n        for ($j = $i + 1; $j &amp;lt; $count; $j++) {\n            if ($array[$j] &amp;lt; $min) {\n                $min = $array[$j];\n                $key = $j;\n            }\n        }\n        if ($key &amp;gt; $i) {\n            $temp = $array[$i];\n            $array[$i] = $min;\n            $array[$key] = $temp;\n        }\n    }\n    return $array;\n}\n\n\/**\n * \u63d2\u5165\u6392\u5e8f\n * @param $array\n * @return array\n *\/\nfunction insertion($array)\n{\n    $length = count($array);\n    $sort[] = $array[0];\n    for ($i = 1; $i &amp;lt; $length; $i++) {\n        $j = $i;\n        do {\n            $continue = false;\n            if ($array[$i] &amp;lt; $sort[$j - 1]) {\n                $j--;\n                $continue = true;\n            }\n        } while ($continue);\n        if ($j &amp;lt; $i) {\n            $sort = array_merge(array_slice($sort, 0, $j), [$array[$i]], array_slice($sort, $j));\n        } else {\n            $sort[] = $array[$i];\n        }\n    }\n    return $sort;\n}\n\n\/**\n * \u5f52\u5e76\u6392\u5e8f\n * @param $array\n * @return array\n *\/\nfunction merge($array)\n{\n\n    $length = count($array);\n    $half = (int)ceil($length \/ 2);\n\n    $result = [];\n\n    if ($length &amp;gt; 2) {\n        $array1 = array_slice($array, 0, $half);\n        $array2 = array_slice($array, $half);\n        $array = array_merge($result, merge($array1), merge($array2));\n    }\n    $array1 = array_slice($array, 0, $half);\n    $array2 = array_slice($array, $half);\n\n    for ($i = 0; $i &amp;lt; $length - 1; $i++) {\n        if (count($array1) == 0 || count($array2) == 0) {\n            break;\n        }\n        if ($array1[0] &amp;lt;= $array2[0]) {\n            $result[] = array_shift($array1);\n        } else {\n            $result[] = array_shift($array2);\n        }\n    }\n    return array_merge($result, $array1, $array2);\n}\n\n\/**\n * \u968f\u673a\u5feb\u901f\u6392\u5e8f\n * @param $array\n * @return array\n *\/\nfunction randomQuick($array)\n{\n    $length = count($array);\n    if ($length &amp;lt;= 1) {\n        return $array;\n    } else {\n        $key = mt_rand(0, ($length - 1));\n        $temp = $array[0];\n        $array[0] = $array[$key];\n        $array[$key] = $temp;\n        $pivot = $array[0];\n        $less = $greater = [];\n        for ($i = 1; $i &amp;lt; $length; $i++) {\n            if ($array[$i] &amp;lt; $pivot) {\n                $less[] = $array[$i];\n            } else {\n                $greater[] = $array[$i];\n            }\n        }\n        return array_merge(quick($less), [$pivot], quick($greater));\n    }\n}\n\n\/**\n * \u57fa\u6570\u6392\u5e8f\n * @param $array\n * @return array\n *\/\nfunction counting($array)\n{\n    $length = count($array);\n    $count_arr = [];\n    for ($i = 0; $i &amp;lt; 1000; $i++) {\n        $count_arr[$i] = 0;\n    }\n    for ($i = 0; $i &amp;lt; $length; $i++) {\n        $count_arr[$array[$i]]++;\n    }\n    $result = [];\n    for ($i = 0; $i &amp;lt; 1000; $i++) {\n        for ($j = 0; $j &amp;lt; $count_arr[$i]; $j++) {\n            $result[] = $i;\n        }\n    }\n    return $result;\n}\n\n\/**\n * \u57fa\u6570\u6392\u5e8f\n * @param $array\n * @return array\n *\/\nfunction radix($array)\n{\n    $length = count($array);\n    $new_array = [];\n\n    for ($i = 0; $i &amp;lt; $length; $i++) {\n        $_num = $array[$i];\n        $num = [];\n        for ($j = 0; $j &amp;lt; 3; $j++) {\n            $int = $_num % 10;\n            array_unshift($num, $int);\n            $_num = $_num \/ 10;\n        }\n        $new_array[] = $num;\n    }\n    for ($i = 0; $i &amp;lt; 3; $i++) {\n        $bucket = [];\n        for ($j = 0; $j &amp;lt; 10; $j++) {\n            $bucket[$j] = [];\n        }\n        for ($j = 0; $j &amp;lt; $length; $j++) {\n            $bucket[$new_array[$j][2 - $i]][] = $new_array[$j];\n        }\n        $new_array = [];\n        for ($j = 0; $j &amp;lt; 10; $j++) {\n            for ($k = 0; $k &amp;lt; count($bucket[$j]); $k++) {\n                $new_array[] = $bucket[$j][$k];\n            }\n        }\n    }\n    for ($i = 0; $i &amp;lt; $length; $i++) {\n        $temp = $new_array[$i];\n        $new_array[$i] = 0;\n        for ($j = 0; $j &amp;lt; 3; $j++) {\n            $new_array[$i] += $temp[2 - $j] * pow(10, $j);\n        }\n    }\n    return $new_array;\n}\n\nfunction print_result($callable, $array, $name)\n{\n    $begin = microtime(true);\n    $result = $callable($array);\n    $end = microtime(true);\n    echo $name . &#039;\u6392\u5e8f\uff0c\u7528\u65f6\uff1a&#039; . (($end - $begin) * 1000) . &#039;ms&#039; . PHP_EOL;\n    echo &#039;\u6392\u5e8f\u7ed3\u679c\uff1a&#039; . implode(&#039;,&#039;, $result) . PHP_EOL . PHP_EOL;\n}\n\necho PHP_EOL;\n$array = create();\n\necho &#039;\u539f\u59cb\u6570\u7ec4\uff1a&#039; . implode(&#039;,&#039;, $array) . PHP_EOL . PHP_EOL;\n\n\nprint_result(&#039;bubble&#039;,$array,&#039;\u5192\u6ce1&#039;);\nprint_result(&#039;quick&#039;,$array,&#039;\u5feb\u901f&#039;);\nprint_result(&#039;selection&#039;,$array,&#039;\u9009\u62e9&#039;);\nprint_result(&#039;insertion&#039;,$array,&#039;\u63d2\u5165&#039;);\nprint_result(&#039;merge&#039;,$array,&#039;\u5f52\u5e76&#039;);\nprint_result(&#039;randomQuick&#039;,$array,&#039;\u968f\u673a\u5feb\u901f&#039;);\nprint_result(&#039;counting&#039;,$array,&#039;\u8ba1\u6570&#039;);\nprint_result(&#039;radix&#039;,$array,&#039;\u57fa\u6570&#039;);<\/code><\/pre>\n\n\n\n<p>\u53c2\u8003\u7f51\u7ad9\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/visualgo.net\/en\">https:\/\/visualgo.net\/en<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 VisuAlgo \u4e0a\u770b\u5230\u4e00\u4e9b\u6392\u5e8f\u7b97\u6cd5\u7684\u539f\u7406\uff0c\u4e8e\u662f\u5c31\u60f3\u7740\u7528php\u6765\u505a\u4e00\u904d\u3002\u505a\u5b8c\u4e4b\u540e\u8fd8\u662f\u5f88\u6709\u6536\u83b7\u7684\u3002\u8fd9\u91cc\u8d34\u4e00\u4e0b\u4ee3\u7801\u3002\u6ca1\u6709\u4ed4\u7ec6\u6253\u78e8\uff0c\u5e94\u8be5\u591a\u591a\u5c11\u5c11\u53ef\u4ee5\u5199\u7684\u66f4\u597d\u7684\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[179],"tags":[186],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-notes","tag-php"],"_links":{"self":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":0,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}