{"id":204961,"date":"2025-05-29T15:40:30","date_gmt":"2025-05-29T07:40:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204961\/"},"modified":"2025-05-29T15:40:30","modified_gmt":"2025-05-29T07:40:30","slug":"python-%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e6%af%8f%e6%ac%a1%e6%8e%92%e5%ba%8f%e9%9a%8f%e6%9c%ba%e9%80%89%e5%8f%96%e5%9f%ba%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204961\/","title":{"rendered":"Python \u5feb\u901f\u6392\u5e8f\u5982\u4f55\u5b9e\u73b0\u6bcf\u6b21\u6392\u5e8f\u968f\u673a\u9009\u53d6\u57fa\u503c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5feb\u901f\u6392\u5e8f\u5982\u4f55\u5b9e\u73b0\u6bcf\u6b21\u6392\u5e8f\u968f\u673a\u9009\u53d6\u57fa\u503c\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPython \u5feb\u901f\u6392\u5e8f\u5982\u4f55\u5b9e\u73b0\u6bcf\u6b21\u6392\u5e8f\u968f\u673a\u9009\u53d6\u57fa\u503c\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241120\/1732084952673d84d82dbb4.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u5feb\u901f\u6392\u5e8f\u4e2d\u968f\u673a\u9009\u53d6\u57fa\u503c\u7684\u5b9e\u73b0<\/strong><\/p>\n<p>\u5728\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u57fa\u503c\u7684\u9009\u62e9\u5bf9\u7b97\u6cd5\u7684\u6027\u80fd\u6709\u5f88\u5927\u5f71\u54cd\u3002\u4f20\u7edf\u7684\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u901a\u5e38\u9009\u62e9\u7b2c\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u503c\uff0c\u4f46\u8fd9\u79cd\u9009\u62e9\u53ef\u80fd\u5bfc\u81f4\u7b97\u6cd5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u8868\u73b0\u4e0d\u4f73\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u63a2\u7d22\u5982\u4f55\u5b9e\u73b0\u6bcf\u6b21\u6392\u5e8f\u65f6\u968f\u673a\u9009\u53d6\u57fa\u503c\u3002<\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u5b9e\u73b0 python \u5feb\u901f\u6392\u5e8f\u4e2d\u6bcf\u6b21\u6392\u5e8f\u968f\u673a\u9009\u53d6\u57fa\u503c\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 python \u7684 random \u6a21\u5757\u53ef\u4ee5\u5b9e\u73b0\u968f\u673a\u9009\u53d6\u57fa\u503c\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5bfc\u5165 random \u6a21\u5757\uff1aimport random<\/li>\n<li>\u5c06\u57fa\u503c\u9009\u62e9\u4ee3\u7801\u4ece piviot = array[0] \u66f4\u6539\u4e3a piviot = array[random.randint(0,len(array)-1)]<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4f7f\u7528 random.randint(0, len(array) &#8211; 1)\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u4ecb\u4e8e 0 \u548c\u6570\u7ec4\u957f\u5ea6 &#8211; 1 \u4e4b\u95f4\u7684\u968f\u673a\u7d22\u5f15\uff0c\u4ece\u800c\u4ece\u6570\u7ec4\u4e2d\u968f\u673a\u9009\u53d6\u57fa\u503c\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import random\n\ndef quick_sort(array):\n    if len(array) &lt; 2:\n        return array\n    else:\n        pivot = array[random.randint(0, len(array) - 1)]\n        less = [i for i in array if i &lt; pivot]\n        middle = [i for i in array if i == pivot]\n        greater = [i for i in array if i &gt; pivot]\n        return quick_sort(less) + middle + quick_sort(greater)<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5feb\u901f\u6392\u5e8f\u5982\u4f55\u5b9e\u73b0\u6bcf&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204961","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204961"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204961\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}