{"id":1007282,"date":"2024-12-27T10:52:03","date_gmt":"2024-12-27T02:52:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007282.html"},"modified":"2024-12-27T10:52:07","modified_gmt":"2024-12-27T02:52:07","slug":"python%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e6%8e%92%e5%ba%8f%e6%9c%80%e5%bf%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1007282.html","title":{"rendered":"python\u6570\u7ec4\u5982\u4f55\u6392\u5e8f\u6700\u5feb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083053\/30659d9c-9718-4885-bbfe-3e15006c065c.webp\" alt=\"python\u6570\u7ec4\u5982\u4f55\u6392\u5e8f\u6700\u5feb\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6700\u5feb\u7684\u6570\u7ec4\u6392\u5e8f\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u6216<code>list.sort()<\/code>\u65b9\u6cd5\u3001\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u5982Timsort\u3001\u6839\u636e\u6570\u636e\u7279\u5f81\u9009\u62e9\u4f18\u5316\u7b56\u7565\u3002<\/strong> Python\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570<code>sorted()<\/code>\u548c\u65b9\u6cd5<code>list.sort()<\/code>\u90fd\u4f7f\u7528\u4e86Timsort\u7b97\u6cd5\uff0c\u5b83\u662f\u5f52\u5e76\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u7ed3\u5408\uff0c\u5177\u6709O(n log n)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u4f18\u5316\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYTHON\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u4e0e\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e24\u79cd\u5185\u7f6e\u7684\u6392\u5e8f\u65b9\u5f0f\uff1a<code>sorted()<\/code>\u51fd\u6570\u548c<code>list.sort()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e24\u8005\u5728\u5e95\u5c42\u5747\u4f7f\u7528Timsort\u7b97\u6cd5\uff0c\u4f46\u5728\u4f7f\u7528\u4e0a\u7565\u6709\u4e0d\u540c\u3002<\/p>\n<\/p>\n<ol>\n<li><code>sorted()<\/code>\u51fd\u6570<\/li>\n<\/ol>\n<p><p><code>sorted()<\/code>\u662f\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\uff0c\u5b83\u9002\u7528\u4e8e\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/p>\n<p>sorted_array = sorted(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><code>list.sort()<\/code>\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p><code>list.sort()<\/code>\u662f\u5217\u8868\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u5b83\u5c31\u5730\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u5373\u5728\u539f\u5217\u8868\u4e0a\u8fdb\u884c\u4fee\u6539\uff0c\u4e0d\u8fd4\u56de\u4efb\u4f55\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/p>\n<p>array.sort()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5efa\u8bae\u5728\u5904\u7406\u5217\u8868\u65f6\u4f7f\u7528<code>list.sort()<\/code>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u989d\u5916\u7684\u5185\u5b58\u6765\u5b58\u50a8\u65b0\u5217\u8868\uff0c\u5c24\u5176\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u663e\u5f97\u66f4\u52a0\u9ad8\u6548\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001TIMSORT\u7b97\u6cd5<\/p>\n<\/p>\n<p><p>Timsort\u662f\u4e00\u79cd\u6df7\u5408\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5\uff0c\u7531Tim Peters\u57282002\u5e74\u4e3aPython\u5f00\u53d1\u3002\u5b83\u7ed3\u5408\u4e86\u5f52\u5e76\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u4f18\u70b9\uff0c\u7279\u522b\u9002\u5408\u4e8e\u5b9e\u9645\u6570\u636e\u4e2d\u5e38\u89c1\u7684\u90e8\u5206\u6709\u5e8f\u5e8f\u5217\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5f52\u5e76\u6392\u5e8f\u4e0e\u63d2\u5165\u6392\u5e8f\u7ed3\u5408<\/li>\n<\/ol>\n<p><p>Timsort\u901a\u8fc7\u8bc6\u522b\u548c\u5229\u7528\u8f93\u5165\u6570\u636e\u4e2d\u7684\u81ea\u7136\u6709\u5e8f\u5e8f\u5217\uff08\u79f0\u4e3a\u8fd0\u884c\uff09\u6765\u4f18\u5316\u6392\u5e8f\u8fc7\u7a0b\u3002\u5bf9\u4e8e\u5c0f\u7684\u65e0\u5e8f\u90e8\u5206\uff0c\u4f7f\u7528\u63d2\u5165\u6392\u5e8f\u5904\u7406\uff0c\u800c\u5bf9\u4e8e\u5927\u7684\u90e8\u5206\u5219\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\uff0c\u63d2\u5165\u6392\u5e8f\u5728\u5c0f\u89c4\u6a21\u6570\u636e\u4e0a\u975e\u5e38\u9ad8\u6548\uff0c\u800c\u5f52\u5e76\u6392\u5e8f\u5219\u80fd\u5f88\u597d\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u65f6\u95f4\u590d\u6742\u5ea6<\/li>\n<\/ol>\n<p><p>Timsort\u7684\u6700\u574f\u60c5\u51b5\u548c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662fO(n log n)\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5176\u4ed6\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5feb\u901f\u6392\u5e8f<\/li>\n<\/ol>\n<p><p>\u5feb\u901f\u6392\u5e8f\uff08Quicksort\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5177\u6709O(n log n)\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u4f46\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u53ef\u80fd\u9000\u5316\u4e3aO(n^2)\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u67a2\u8f74\uff0c\u53ef\u4ee5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f18\u5316\u5176\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5806\u6392\u5e8f<\/li>\n<\/ol>\n<p><p>\u5806\u6392\u5e8f\uff08Heapsort\uff09\u4e5f\u662f\u4e00\u79cd\u5177\u6709O(n log n)\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u5b83\u7279\u522b\u9002\u5408\u9700\u8981\u6700\u5c0f\u7a7a\u95f4\u5f00\u9500\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f18\u5316\u7b56\u7565<\/p>\n<\/p>\n<p><p>\u6839\u636e\u6570\u636e\u7279\u5f81\u548c\u5e94\u7528\u573a\u666f\uff0c\u91c7\u53d6\u5408\u9002\u7684\u4f18\u5316\u7b56\u7565\u4e5f\u80fd\u63d0\u5347\u6392\u5e8f\u6548\u7387\u3002<\/p>\n<\/p>\n<ol>\n<li>\u9488\u5bf9\u90e8\u5206\u6709\u5e8f\u6570\u636e<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u6570\u636e\u96c6\u5df2\u7ecf\u90e8\u5206\u6709\u5e8f\uff0cTimsort\u4f1a\u81ea\u52a8\u5229\u7528\u8fd9\u4e00\u7279\u6027\u8fdb\u884c\u4f18\u5316\u3002\u4f46\u5982\u679c\u4f7f\u7528\u5176\u4ed6\u7b97\u6cd5\uff0c\u53ef\u4ee5\u5148\u68c0\u67e5\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u9009\u62e9\u66f4\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u5e76\u884c\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5e76\u884c\u5904\u7406\u6765\u52a0\u901f\u6392\u5e8f\u8fc7\u7a0b\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u6216\u7b2c\u4e09\u65b9\u5e93\u5982<code>joblib<\/code>\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5916\u90e8\u6392\u5e8f<\/li>\n<\/ol>\n<p><p>\u5f53\u6570\u636e\u96c6\u5927\u5230\u65e0\u6cd5\u5728\u5185\u5b58\u4e2d\u5b8c\u5168\u8f7d\u5165\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5916\u90e8\u6392\u5e8f\u6280\u672f\uff0c\u5c06\u6570\u636e\u5206\u6210\u591a\u4e2a\u5c0f\u5757\uff0c\u5206\u522b\u6392\u5e8f\u540e\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5185\u7f6e\u7684\u6392\u5e8f\u65b9\u6cd5\u5df2\u7ecf\u8db3\u591f\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u4e00\u822c\u7528\u9014\u7684\u6570\u636e\u5904\u7406\u3002\u7136\u800c\uff0c\u901a\u8fc7\u4e86\u89e3\u4e0d\u540c\u7684\u6392\u5e8f\u7b97\u6cd5\u53ca\u5176\u4f18\u5316\u7b56\u7565\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u8fdb\u4e00\u6b65\u63d0\u5347\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6392\u5e8f\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f18\u5148\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u548c<code>list.sort()<\/code>\uff0c\u56e0\u4e3a\u5b83\u4eec\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\uff0c\u80fd\u591f\u5904\u7406\u5927\u591a\u6570\u6392\u5e8f\u9700\u6c42\u3002<\/strong><\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728\u5904\u7406\u90e8\u5206\u6709\u5e8f\u6570\u636e\u6216\u7279\u5b9a\u7ed3\u6784\u7684\u6570\u636e\u65f6\uff0c\u8003\u8651\u4f7f\u7528\u4e13\u95e8\u7684\u6392\u5e8f\u7b97\u6cd5\u6216\u4f18\u5316\u7b56\u7565\u3002<\/strong><\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bf9\u4e8e\u8d85\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u8003\u8651\u4f7f\u7528\u5e76\u884c\u5904\u7406\u6216\u5916\u90e8\u6392\u5e8f\u6280\u672f\u3002<\/strong><\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u548c\u7b56\u7565\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u9009\u62e9\u6700\u4f73\u7684\u6392\u5e8f\u65b9\u6cd5\uff0c\u63d0\u5347\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u7684\u6392\u5e8f\u7b97\u6cd5\u4ee5\u63d0\u9ad8Python\u6570\u7ec4\u7684\u6392\u5e8f\u901f\u5ea6\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u5bf9\u4e8e\u63d0\u9ad8\u6570\u7ec4\u7684\u6392\u5e8f\u901f\u5ea6\u81f3\u5173\u91cd\u8981\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>sort()<\/code>\u65b9\u6cd5\u6216<code>sorted()<\/code>\u51fd\u6570\u901a\u5e38\u662f\u6700\u4f18\u9009\u62e9\uff0c\u56e0\u4e3aPython\u5728\u8fd9\u4e9b\u51fd\u6570\u4e2d\u5b9e\u73b0\u4e86Timsort\u7b97\u6cd5\uff0c\u8be5\u7b97\u6cd5\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u8868\u73b0\u51fa\u8272\u3002\u5bf9\u4e8e\u5c0f\u89c4\u6a21\u6570\u7ec4\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u63d2\u5165\u6392\u5e8f\u6216\u9009\u62e9\u6392\u5e8f\uff0c\u800c\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\u53ef\u80fd\u66f4\u6709\u6548\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f18\u5316\u6392\u5e8f\u6027\u80fd\u4ee5\u5904\u7406\u5927\u578b\u6570\u7ec4\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f18\u5316\u5927\u578b\u6570\u7ec4\u7684\u6392\u5e8f\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u91c7\u7528\u5e76\u884c\u8ba1\u7b97\u548c\u591a\u7ebf\u7a0b\u6280\u672f\u3002\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u53ef\u4ee5\u5c06\u6570\u7ec4\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\u5e76\u884c\u6392\u5e8f\uff0c\u6700\u540e\u518d\u8fdb\u884c\u5408\u5e76\u3002\u6b64\u5916\uff0c\u8c03\u6574\u6392\u5e8f\u7b97\u6cd5\u7684\u53c2\u6570\uff0c\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982NumPy\u6570\u7ec4\uff09\u4e5f\u80fd\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7684NumPy\u5e93\u8fdb\u884c\u6570\u7ec4\u6392\u5e8f\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>NumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002\u5176\u5185\u7f6e\u7684<code>numpy.sort()<\/code>\u51fd\u6570\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002NumPy\u8fd8\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u7684\u6392\u5e8f\uff0c\u80fd\u591f\u5bf9\u6570\u7ec4\u7684\u7279\u5b9a\u7ef4\u5ea6\u8fdb\u884c\u6392\u5e8f\uff0c\u4f7f\u5f97\u6570\u636e\u5904\u7406\u66f4\u52a0\u7075\u6d3b\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6700\u5feb\u7684\u6570\u7ec4\u6392\u5e8f\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u6216list.sort()\u65b9\u6cd5\u3001\u9009\u62e9\u5408\u9002\u7684\u6392 [&hellip;]","protected":false},"author":3,"featured_media":1007292,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007282"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1007282"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007282\/revisions"}],"predecessor-version":[{"id":1007294,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007282\/revisions\/1007294"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1007292"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1007282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1007282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1007282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}