{"id":556,"date":"2024-11-12T01:13:38","date_gmt":"2024-11-11T17:13:38","guid":{"rendered":"http:\/\/www.wengdao.net\/?p=556"},"modified":"2024-11-12T01:13:38","modified_gmt":"2024-11-11T17:13:38","slug":"php-%e7%bb%8f%e5%85%b8%e7%9a%84%e5%9b%9b%e5%a4%a7%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/jiandao.zliby.com\/?p=556","title":{"rendered":"PHP \u7ecf\u5178\u7684\u56db\u5927\u6392\u5e8f\u7b97\u6cd5"},"content":{"rendered":"<p><strong> PHP \u7ecf\u5178\u7684\u56db\u5927\u6392\u5e8f\u7b97\u6cd5\uff0c\u6bcf\u79cd\u7b97\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u5408\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<h4>1.\u5192\u6ce1\u6392\u5e8f\uff08Bubble Sort\uff09<\/h4>\n<p>\u63cf\u8ff0\uff1a\u53cd\u590d\u904d\u5386\u6570\u7ec4\uff0c\u76f8\u90bb\u5143\u7d20\u4e24\u4e24\u6bd4\u8f83\u5e76\u4ea4\u6362\uff0c\u5c06\u6700\u5927\u7684\u5143\u7d20\u9010\u6e10\u201c\u5192\u6ce1\u201d\u5230\u6570\u7ec4\u672b\u5c3e\u3002<br \/>\n\u590d\u6742\u5ea6\uff1a\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 O(n\u00b2)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(1)\u3002<br \/>\n\u4f18\u70b9\uff1a\u5b9e\u73b0\u7b80\u5355\uff0c\u9002\u5408\u5c0f\u89c4\u6a21\u6570\u636e\u3002<br \/>\n\u7f3a\u70b9\uff1a\u6548\u7387\u8f83\u4f4e\uff0c\u4e0d\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u3002<br \/>\n\u5e94\u7528\uff1a\u9002\u5408\u6570\u636e\u91cf\u5c11\u3001\u6392\u5e8f\u8981\u6c42\u4e0d\u9ad8\u7684\u573a\u666f\u3002<\/p>\n<pre><code class=\"prettyprint\" >function bubbleSort($array) {\n    $n = count($array);\n    for ($i = 0; $i &lt; $n - 1; $i++) {\n        for ($j = 0; $j &lt; $n - $i - 1; $j++) {\n            if ($array[$j] &gt; $array[$j + 1]) {\n                \/\/ \u4ea4\u6362\u76f8\u90bb\u5143\u7d20\n                $temp = $array[$j];\n                $array[$j] = $array[$j + 1];\n                $array[$j + 1] = $temp;\n            }\n        }\n    }\n    return $array;\n}<\/code><\/pre>\n<h4>2.\u9009\u62e9\u6392\u5e8f\uff08Selection Sort\uff09<\/h4>\n<p>\u63cf\u8ff0\uff1a\u904d\u5386\u6570\u7ec4\uff0c\u627e\u5230\u6700\u5c0f\u7684\u5143\u7d20\u653e\u5230\u6392\u5e8f\u90e8\u5206\u7684\u672b\u5c3e\uff0c\u9010\u6b65\u6269\u5c55\u6392\u5e8f\u90e8\u5206\u3002<br \/>\n\u590d\u6742\u5ea6\uff1a\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 O(n\u00b2)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(1)\u3002<br \/>\n\u4f18\u70b9\uff1a\u4e0d\u4f9d\u8d56\u6570\u7ec4\u7684\u987a\u5e8f\uff0c\u4ea4\u6362\u6b21\u6570\u8f83\u5c11\u3002<br \/>\n\u7f3a\u70b9\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u4e0d\u9002\u5408\u6570\u636e\u91cf\u8f83\u5927\u7684\u573a\u666f\u3002<br \/>\n\u5e94\u7528\uff1a\u9002\u5408\u5185\u5b58\u53d7\u9650\u3001\u6570\u636e\u91cf\u4e0d\u5927\u7684\u6392\u5e8f\u4efb\u52a1\u3002<\/p>\n<pre><code class=\"prettyprint\" >function selectionSort($array) {\n    $n = count($array);\n    for ($i = 0; $i &lt; $n - 1; $i++) {\n        $minIndex = $i;\n        for ($j = $i + 1; $j &lt; $n; $j++) {\n            if ($array[$j] &lt; $array[$minIndex]) {\n                $minIndex = $j;\n            }\n        }\n        \/\/ \u4ea4\u6362\u5143\u7d20\n        $temp = $array[$i];\n        $array[$i] = $array[$minIndex];\n        $array[$minIndex] = $temp;\n    }\n    return $array;\n}<\/code><\/pre>\n<h4>3.\u5feb\u901f\u6392\u5e8f\uff08Quick Sort\uff09<\/h4>\n<p>\u63cf\u8ff0\uff1a\u9009\u62e9\u4e00\u4e2a\u201c\u57fa\u51c6\u201d\u5143\u7d20\uff0c\u5c06\u6570\u7ec4\u5206\u4e3a\u5c0f\u4e8e\u548c\u5927\u4e8e\u57fa\u51c6\u7684\u4e24\u90e8\u5206\uff0c\u7136\u540e\u5bf9\u8fd9\u4e24\u90e8\u5206\u9012\u5f52\u6392\u5e8f\u3002<br \/>\n\u590d\u6742\u5ea6\uff1a\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 O(n log n)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(log n)\u3002<br \/>\n\u4f18\u70b9\uff1a\u901f\u5ea6\u5feb\u3001\u6548\u7387\u9ad8\uff0c\u662f\u5e38\u7528\u7684\u6392\u5e8f\u7b97\u6cd5\u4e4b\u4e00\u3002<br \/>\n\u7f3a\u70b9\uff1a\u6700\u574f\u60c5\u51b5\u4e0b\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n\u00b2)\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u968f\u673a\u9009\u57fa\u51c6\u5143\u7d20\u6765\u907f\u514d\u3002<br \/>\n\u5e94\u7528\uff1a\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u91cf\u5927\u3001\u6027\u80fd\u8981\u6c42\u9ad8\u7684\u573a\u666f\u3002<\/p>\n<pre><code class=\"prettyprint\" >function quickSort($array) {\n    if (count($array) &lt; 2) {\n        return $array; \/\/ \u57fa\u7ebf\u6761\u4ef6\uff1a\u7a7a\u6570\u7ec4\u6216\u5355\u5143\u7d20\u6570\u7ec4\n    }\n\n    $pivot = $array[0]; \/\/ \u57fa\u51c6\u5143\u7d20\n    $left = [];\n    $right = [];\n\n    for ($i = 1; $i &lt; count($array); $i++) {\n        if ($array[$i] &lt;= $pivot) {\n            $left[] = $array[$i];\n        } else {\n            $right[] = $array[$i];\n        }\n    }\n\n    \/\/ \u5408\u5e76\u6392\u5e8f\u597d\u7684\u5b50\u6570\u7ec4\u548c\u57fa\u51c6\u5143\u7d20\n    return array_merge(quickSort($left), [$pivot], quickSort($right));\n}<\/code><\/pre>\n<h4>4.\u5f52\u5e76\u6392\u5e8f\uff08Merge Sort\uff09<\/h4>\n<p>\u63cf\u8ff0\uff1a\u5c06\u6570\u7ec4\u5206\u6210\u4e24\u534a\uff0c\u5206\u522b\u6392\u5e8f\u540e\u5408\u5e76\u6210\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\uff08\u9012\u5f52\u5b9e\u73b0\uff09\u3002<br \/>\n\u590d\u6742\u5ea6\uff1a\u65f6\u95f4\u590d\u6742\u5ea6 O(n log n)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\u3002<br \/>\n\u4f18\u70b9\uff1a\u7a33\u5b9a\u6392\u5e8f\uff0c\u9002\u5408\u5927\u6570\u636e\u91cf\u6392\u5e8f\uff0c\u6700\u574f\u60c5\u51b5\u4e5f\u6709\u8f83\u597d\u6027\u80fd\u3002<br \/>\n\u7f3a\u70b9\uff1a\u9700\u8981\u989d\u5916\u7684\u7a7a\u95f4\u5b58\u50a8\u6570\u7ec4\uff0c\u4e0d\u9002\u5408\u5185\u5b58\u975e\u5e38\u53d7\u9650\u7684\u573a\u666f\u3002<br \/>\n\u5e94\u7528\uff1a\u9002\u5408\u6392\u5e8f\u8f83\u5927\u6570\u636e\u96c6\uff0c\u4e14\u5bf9\u7a33\u5b9a\u6027\u6709\u8981\u6c42\u7684\u573a\u666f\u3002<\/p>\n<pre><code class=\"prettyprint\" >function mergeSort($array) {\n    if (count($array) &lt;= 1) {\n        return $array; \/\/ \u57fa\u7ebf\u6761\u4ef6\n    }\n\n    $mid = floor(count($array) \/ 2);\n    $left = array_slice($array, 0, $mid);\n    $right = array_slice($array, $mid);\n\n    return merge(mergeSort($left), mergeSort($right));\n}\n\nfunction merge($left, $right) {\n    $result = [];\n    $i = $j = 0;\n\n    \/\/ \u5408\u5e76\u4e24\u4e2a\u5df2\u6392\u5e8f\u6570\u7ec4\n    while ($i &lt; count($left) &amp;&amp; $j &lt; count($right)) {\n        if ($left[$i] &lt; $right[$j]) {\n            $result[] = $left[$i];\n            $i++;\n        } else {\n            $result[] = $right[$j];\n            $j++;\n        }\n    }\n\n    \/\/ \u5408\u5e76\u5269\u4f59\u5143\u7d20\n    while ($i &lt; count($left)) {\n        $result[] = $left[$i];\n        $i++;\n    }\n\n    while ($j &lt; count($right)) {\n        $result[] = $right[$j];\n        $j++;\n    }\n\n    return $result;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u7ecf\u5178\u7684\u56db\u5927\u6392\u5e8f\u7b97\u6cd5\uff0c\u6bcf\u79cd\u7b97\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u5408\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f 1.\u5192\u6ce1\u6392\u5e8f\uff08Bubble Sort\uff09 \u63cf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/556"}],"collection":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=556"}],"version-history":[{"count":1,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/556\/revisions"}],"predecessor-version":[{"id":557,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/556\/revisions\/557"}],"wp:attachment":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}