{"id":1167758,"date":"2025-01-15T15:49:21","date_gmt":"2025-01-15T07:49:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167758.html"},"modified":"2025-01-15T15:49:24","modified_gmt":"2025-01-15T07:49:24","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%ae%9e%e7%8e%b0%e9%80%89%e6%8b%a9%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1167758.html","title":{"rendered":"\u5982\u4f55\u7528python\u5b9e\u73b0\u9009\u62e9\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065206\/e19fb958-24e0-4987-ac9a-ca7b49170c6b.webp\" alt=\"\u5982\u4f55\u7528python\u5b9e\u73b0\u9009\u62e9\u6392\u5e8f\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u5b9e\u73b0\u9009\u62e9\u6392\u5e8f\uff1a<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u5b9e\u73b0\u9009\u62e9\u6392\u5e8f\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u904d\u5386\u6570\u7ec4\u3001\u627e\u5230\u5f53\u524d\u672a\u6392\u5e8f\u90e8\u5206\u4e2d\u7684\u6700\u5c0f\u5143\u7d20\u3001\u5c06\u5176\u4e0e\u672a\u6392\u5e8f\u90e8\u5206\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u4ea4\u6362\u3002<\/strong> \u9996\u5148\uff0c\u6211\u4eec\u904d\u5386\u6574\u4e2a\u6570\u7ec4\uff0c\u627e\u5230\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u7136\u540e\u5c06\u5176\u4e0e\u7b2c\u4e00\u4e2a\u5143\u7d20\u4ea4\u6362\u3002\u63a5\u7740\uff0c\u6211\u4eec\u5ffd\u7565\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u904d\u5386\u5269\u4e0b\u7684\u6570\u7ec4\uff0c\u5e76\u91cd\u590d\u76f8\u540c\u7684\u8fc7\u7a0b\u3002\u6700\u7ec8\uff0c\u6574\u4e2a\u6570\u7ec4\u5c06\u88ab\u6392\u5e8f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u9009\u62e9\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u6392\u5e8f\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u6392\u5e8f\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u89c2\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521d\u59cb\u65f6\uff0c\u672a\u6392\u5e8f\u90e8\u5206\u662f\u6574\u4e2a\u6570\u7ec4\u3002<\/li>\n<li>\u5728\u672a\u6392\u5e8f\u90e8\u5206\u4e2d\u627e\u5230\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u672a\u6392\u5e8f\u90e8\u5206\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4ea4\u6362\u3002<\/li>\n<li>\u63a5\u7740\u5c06\u672a\u6392\u5e8f\u90e8\u5206\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4f4d\u7f6e\u79fb\u5230\u5df2\u6392\u5e8f\u90e8\u5206\uff0c\u7ee7\u7eed\u5bf9\u5269\u4f59\u7684\u672a\u6392\u5e8f\u90e8\u5206\u6267\u884c\u4e0a\u8ff0\u64cd\u4f5c\u3002<\/li>\n<li>\u91cd\u590d\u4e0a\u8ff0\u8fc7\u7a0b\uff0c\u76f4\u5230\u6574\u4e2a\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002<\/li>\n<\/ol>\n<p><p>\u9009\u62e9\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u5176\u4e2dn\u662f\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u867d\u7136\u5b83\u7684\u6548\u7387\u4e0d\u5982\u4e00\u4e9b\u66f4\u590d\u6742\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f46\u7531\u4e8e\u5176\u7b80\u5355\u6027\uff0c\u9009\u62e9\u6392\u5e8f\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4ecd\u7136\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9009\u62e9\u6392\u5e8f\u7684\u5b9e\u73b0\u6b65\u9aa4<\/h3>\n<\/p>\n<p><h4>1. \u5b9a\u4e49\u9009\u62e9\u6392\u5e8f\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<code>selection_sort<\/code>\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def selection_sort(arr):<\/p>\n<p>    for i in range(len(arr)):<\/p>\n<p>        min_idx = i<\/p>\n<p>        for j in range(i + 1, len(arr)):<\/p>\n<p>            if arr[j] &lt; arr[min_idx]:<\/p>\n<p>                min_idx = j<\/p>\n<p>        arr[i], arr[min_idx] = arr[min_idx], arr[i]<\/p>\n<p>    return arr<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8be6\u7ec6\u89e3\u91ca\u6bcf\u4e00\u6b65<\/h4>\n<\/p>\n<ol>\n<li><strong>\u904d\u5386\u6570\u7ec4<\/strong>\uff1a\u4f7f\u7528\u4e00\u4e2a\u5916\u5c42\u5faa\u73af<code>for i in range(len(arr))<\/code>\u6765\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<li><strong>\u627e\u5230\u6700\u5c0f\u5143\u7d20<\/strong>\uff1a\u5728\u5185\u5c42\u5faa\u73af\u4e2d\uff0c\u5047\u8bbe\u5f53\u524d\u5143\u7d20\u662f\u6700\u5c0f\u5143\u7d20<code>min_idx = i<\/code>\uff0c\u7136\u540e\u904d\u5386\u6570\u7ec4\u7684\u5176\u4f59\u90e8\u5206<code>for j in range(i + 1, len(arr))<\/code>\uff0c\u627e\u5230\u6bd4\u5f53\u524d\u6700\u5c0f\u5143\u7d20\u66f4\u5c0f\u7684\u5143\u7d20\u3002<\/li>\n<li><strong>\u4ea4\u6362\u5143\u7d20<\/strong>\uff1a\u5982\u679c\u627e\u5230\u66f4\u5c0f\u7684\u5143\u7d20\uff0c\u5219\u66f4\u65b0\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15<code>min_idx = j<\/code>\u3002\u6700\u540e\uff0c\u5c06\u6700\u5c0f\u5143\u7d20\u4e0e\u5f53\u524d\u5143\u7d20\u4ea4\u6362<code>arr[i], arr[min_idx] = arr[min_idx], arr[i]<\/code>\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u9009\u62e9\u6392\u5e8f\u7684\u4f8b\u5b50<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u9009\u62e9\u6392\u5e8f\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def selection_sort(arr):<\/p>\n<p>    for i in range(len(arr)):<\/p>\n<p>        min_idx = i<\/p>\n<p>        for j in range(i + 1, len(arr)):<\/p>\n<p>            if arr[j] &lt; arr[min_idx]:<\/p>\n<p>                min_idx = j<\/p>\n<p>        arr[i], arr[min_idx] = arr[min_idx], arr[i]<\/p>\n<p>    return arr<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>arr = [64, 25, 12, 22, 11]<\/p>\n<p>sorted_arr = selection_sort(arr)<\/p>\n<p>print(&quot;\u6392\u5e8f\u540e\u7684\u6570\u7ec4\uff1a&quot;, sorted_arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bf9\u6570\u7ec4<code>[64, 25, 12, 22, 11]<\/code>\u8fdb\u884c\u4e86\u9009\u62e9\u6392\u5e8f\uff0c\u6700\u7ec8\u8f93\u51fa\u6392\u5e8f\u540e\u7684\u6570\u7ec4<code>[11, 12, 22, 25, 64]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9009\u62e9\u6392\u5e8f\u7684\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u9009\u62e9\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u4e00\u4e9b\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u5728\u6bcf\u6b21\u627e\u5230\u6700\u5c0f\u5143\u7d20\u540e\uff0c\u53ef\u4ee5\u68c0\u67e5\u662f\u5426\u9700\u8981\u8fdb\u884c\u4ea4\u6362\u64cd\u4f5c\u3002\u5982\u679c\u5f53\u524d\u5143\u7d20\u5df2\u7ecf\u662f\u6700\u5c0f\u5143\u7d20\uff0c\u5219\u4e0d\u9700\u8981\u8fdb\u884c\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def selection_sort(arr):<\/p>\n<p>    for i in range(len(arr)):<\/p>\n<p>        min_idx = i<\/p>\n<p>        for j in range(i + 1, len(arr)):<\/p>\n<p>            if arr[j] &lt; arr[min_idx]:<\/p>\n<p>                min_idx = j<\/p>\n<p>        if min_idx != i:<\/p>\n<p>            arr[i], arr[min_idx] = arr[min_idx], arr[i]<\/p>\n<p>    return arr<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f18\u5316\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6761\u4ef6<code>if min_idx != i<\/code>\uff0c\u53ea\u6709\u5728\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\u4e0d\u7b49\u4e8e\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\u65f6\uff0c\u624d\u8fdb\u884c\u4ea4\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u9009\u62e9\u6392\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u6392\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u56e0\u4e3a\u5b83\u53ea\u9700\u8981\u5e38\u6570\u7ea7\u522b\u7684\u989d\u5916\u7a7a\u95f4\u6765\u5b58\u50a8\u4e34\u65f6\u53d8\u91cf\u3002\u8fd9\u4e5f\u662f\u9009\u62e9\u6392\u5e8f\u7684\u4e00\u4e2a\u4f18\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u9009\u62e9\u6392\u5e8f\u7684\u7a33\u5b9a\u6027<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u6392\u5e8f\u662f\u4e00\u79cd\u4e0d\u7a33\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u56e0\u4e3a\u5728\u4ea4\u6362\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u540c\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\u53ef\u80fd\u4f1a\u88ab\u6539\u53d8\u3002\u4f8b\u5982\uff0c\u5728\u5bf9\u6570\u7ec4<code>[3, 3, 1]<\/code>\u8fdb\u884c\u9009\u62e9\u6392\u5e8f\u65f6\uff0c\u7b2c\u4e00\u6b21\u4ea4\u6362\u540e\u6570\u7ec4\u53d8\u4e3a<code>[1, 3, 3]<\/code>\uff0c\u76f8\u540c\u5143\u7d20<code>3<\/code>\u7684\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u4e86\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u9009\u62e9\u6392\u5e8f\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u6392\u5e8f\u9002\u7528\u4e8e\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u91cf\u8f83\u5c0f<\/strong>\uff1a\u9009\u62e9\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\u7684\u6392\u5e8f\u6548\u7387\u8f83\u4f4e\uff0c\u4f46\u5bf9\u4e8e\u5c0f\u6570\u636e\u91cf\u7684\u6392\u5e8f\u53ef\u4ee5\u63a5\u53d7\u3002<\/li>\n<li><strong>\u5bf9\u6392\u5e8f\u7a33\u5b9a\u6027\u8981\u6c42\u4e0d\u9ad8<\/strong>\uff1a\u9009\u62e9\u6392\u5e8f\u662f\u4e0d\u7a33\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5982\u679c\u5bf9\u6392\u5e8f\u7a33\u5b9a\u6027\u6ca1\u6709\u4e25\u683c\u8981\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u9009\u62e9\u6392\u5e8f\u3002<\/li>\n<li><strong>\u9700\u8981\u7b80\u5355\u5b9e\u73b0<\/strong>\uff1a\u9009\u62e9\u6392\u5e8f\u7684\u7b97\u6cd5\u7b80\u5355\uff0c\u5bb9\u6613\u7406\u89e3\u548c\u5b9e\u73b0\uff0c\u9002\u5408\u7528\u4e8e\u6559\u5b66\u548c\u5b66\u4e60\u76ee\u7684\u3002<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u4e0e\u5176\u4ed6\u5e38\u89c1\u7684\u6392\u5e8f\u7b97\u6cd5\u76f8\u6bd4\uff0c\u9009\u62e9\u6392\u5e8f\u7684\u6548\u7387\u8f83\u4f4e\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5192\u6ce1\u6392\u5e8f<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u4e0e\u9009\u62e9\u6392\u5e8f\u7c7b\u4f3c\uff0c\u4f46\u9009\u62e9\u6392\u5e8f\u901a\u5e38\u6bd4\u5192\u6ce1\u6392\u5e8f\u7a0d\u5feb\u3002<\/li>\n<li><strong>\u63d2\u5165\u6392\u5e8f<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u6bd4\u9009\u62e9\u6392\u5e8f\u66f4\u5feb\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u51e0\u4e4e\u6709\u5e8f\u7684\u6570\u7ec4\u3002<\/li>\n<li><strong>\u5f52\u5e76\u6392\u5e8f<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log n)\uff0c\u6bd4\u9009\u62e9\u6392\u5e8f\u66f4\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u6570\u636e\u91cf\u7684\u6392\u5e8f\u3002<\/li>\n<li><strong>\u5feb\u901f\u6392\u5e8f<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log n)\uff0c\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\u662f\u6700\u5feb\u7684\u6392\u5e8f\u7b97\u6cd5\u4e4b\u4e00\uff0c\u4f46\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u53ef\u80fd\u9000\u5316\u4e3aO(n^2)\u3002<\/li>\n<li><strong>\u5806\u6392\u5e8f<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log n)\uff0c\u6bd4\u9009\u62e9\u6392\u5e8f\u66f4\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u6570\u636e\u91cf\u7684\u6392\u5e8f\u3002<\/li>\n<\/ol>\n<p><p>\u9009\u62e9\u6392\u5e8f\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5e76\u4e0d\u5e38\u7528\uff0c\u56e0\u4e3a\u5b83\u7684\u6548\u7387\u8f83\u4f4e\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\u7684\u6392\u5e8f\uff0c\u901a\u5e38\u9009\u62e9\u66f4\u9ad8\u6548\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5982\u5f52\u5e76\u6392\u5e8f\u3001\u5feb\u901f\u6392\u5e8f\u6216\u5806\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u9009\u62e9\u6392\u5e8f\u7684\u53d8\u79cd<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u6392\u5e8f\u6709\u4e00\u4e9b\u53d8\u79cd\uff0c\u53ef\u4ee5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u63d0\u9ad8\u6548\u7387\u3002\u4f8b\u5982\uff0c\u53cc\u5411\u9009\u62e9\u6392\u5e8f\uff08\u4e5f\u79f0\u4e3a\u9e21\u5c3e\u9152\u6392\u5e8f\uff09\u5728\u6bcf\u6b21\u904d\u5386\u65f6\u540c\u65f6\u627e\u5230\u6700\u5c0f\u548c\u6700\u5927\u5143\u7d20\uff0c\u5e76\u5c06\u5b83\u4eec\u5206\u522b\u653e\u5728\u6570\u7ec4\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def double_selection_sort(arr):<\/p>\n<p>    left = 0<\/p>\n<p>    right = len(arr) - 1<\/p>\n<p>    while left &lt; right:<\/p>\n<p>        min_idx = left<\/p>\n<p>        max_idx = left<\/p>\n<p>        for i in range(left, right + 1):<\/p>\n<p>            if arr[i] &lt; arr[min_idx]:<\/p>\n<p>                min_idx = i<\/p>\n<p>            if arr[i] &gt; arr[max_idx]:<\/p>\n<p>                max_idx = i<\/p>\n<p>        arr[left], arr[min_idx] = arr[min_idx], arr[left]<\/p>\n<p>        if max_idx == left:<\/p>\n<p>            max_idx = min_idx<\/p>\n<p>        arr[right], arr[max_idx] = arr[max_idx], arr[right]<\/p>\n<p>        left += 1<\/p>\n<p>        right -= 1<\/p>\n<p>    return arr<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>arr = [64, 25, 12, 22, 11]<\/p>\n<p>sorted_arr = double_selection_sort(arr)<\/p>\n<p>print(&quot;\u6392\u5e8f\u540e\u7684\u6570\u7ec4\uff1a&quot;, sorted_arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bf9\u6570\u7ec4<code>[64, 25, 12, 22, 11]<\/code>\u8fdb\u884c\u4e86\u53cc\u5411\u9009\u62e9\u6392\u5e8f\uff0c\u6700\u7ec8\u8f93\u51fa\u6392\u5e8f\u540e\u7684\u6570\u7ec4<code>[11, 12, 22, 25, 64]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u6392\u5e8f\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u89c2\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5c3d\u7ba1\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u4f46\u7531\u4e8e\u5176\u7b80\u5355\u6027\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4ecd\u7136\u6709\u7528\u3002\u901a\u8fc7\u7406\u89e3\u9009\u62e9\u6392\u5e8f\u7684\u57fa\u672c\u6982\u5ff5\u3001\u5b9e\u73b0\u6b65\u9aa4\u548c\u4f18\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1\u8fd9\u79cd\u6392\u5e8f\u7b97\u6cd5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6392\u5e8f\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u9009\u62e9\u6392\u5e8f\u7684\u57fa\u672c\u539f\u7406\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u9009\u62e9\u6392\u5e8f\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u8def\u662f\u5c06\u5f85\u6392\u5e8f\u7684\u5e8f\u5217\u5206\u4e3a\u5df2\u6392\u5e8f\u548c\u672a\u6392\u5e8f\u4e24\u90e8\u5206\u3002\u6bcf\u6b21\u4ece\u672a\u6392\u5e8f\u90e8\u5206\u4e2d\u9009\u62e9\u6700\u5c0f\uff08\u6216\u6700\u5927\uff09\u5143\u7d20\uff0c\u5c06\u5176\u653e\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u672b\u5c3e\uff0c\u76f4\u5230\u6240\u6709\u5143\u7d20\u90fd\u88ab\u6392\u5e8f\u5b8c\u6210\u3002\u8fd9\u79cd\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n\u00b2)\uff0c\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u7684\u6392\u5e8f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9e\u73b0\u9009\u62e9\u6392\u5e8f\u7684\u4ee3\u7801\u793a\u4f8b\u662f\u600e\u6837\u7684\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u9009\u62e9\u6392\u5e8f\uff1a  <\/p>\n<pre><code class=\"language-python\">def selection_sort(arr):\n    n = len(arr)\n    for i in range(n):\n        # \u5047\u8bbe\u5f53\u524d\u7d22\u5f15\u7684\u5143\u7d20\u662f\u6700\u5c0f\u503c\n        min_index = i\n        for j in range(i+1, n):\n            if arr[j] &lt; arr[min_index]:\n                min_index = j\n        # \u4ea4\u6362\u5f53\u524d\u5143\u7d20\u4e0e\u627e\u5230\u7684\u6700\u5c0f\u503c\n        arr[i], arr[min_index] = arr[min_index], arr[i]\n    return arr\n\n# \u793a\u4f8b\u4f7f\u7528\nnumbers = [64, 25, 12, 22, 11]\nsorted_numbers = selection_sort(numbers)\nprint(sorted_numbers)  # \u8f93\u51fa: [11, 12, 22, 25, 64]\n<\/code><\/pre>\n<p><strong>\u9009\u62e9\u6392\u5e8f\u9002\u5408\u5904\u7406\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\uff1f<\/strong><br \/>\u9009\u62e9\u6392\u5e8f\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u96c6\uff0c\u56e0\u4e3a\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u968f\u7740\u6570\u636e\u91cf\u7684\u589e\u52a0\uff0c\u6548\u7387\u4f1a\u663e\u8457\u4e0b\u964d\u3002\u8fd9\u79cd\u7b97\u6cd5\u5728\u6570\u636e\u57fa\u672c\u6709\u5e8f\u65f6\u8868\u73b0\u826f\u597d\uff0c\u4f46\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u65e0\u5e8f\u6570\u636e\uff0c\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6392\u5e8f\u7b97\u6cd5\uff08\u5982\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\uff09\u4f1a\u66f4\u52a0\u5408\u9002\u3002\u6b64\u5916\uff0c\u9009\u62e9\u6392\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u56e0\u6b64\u5728\u5185\u5b58\u4f7f\u7528\u4e0a\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u5b9e\u73b0\u9009\u62e9\u6392\u5e8f\uff1a \u4f7f\u7528Python\u5b9e\u73b0\u9009\u62e9\u6392\u5e8f\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u904d\u5386\u6570\u7ec4\u3001\u627e\u5230\u5f53\u524d\u672a\u6392\u5e8f\u90e8 [&hellip;]","protected":false},"author":3,"featured_media":1167763,"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\/1167758"}],"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=1167758"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167758\/revisions"}],"predecessor-version":[{"id":1167764,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167758\/revisions\/1167764"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167763"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}