{"id":998452,"date":"2024-12-27T09:33:27","date_gmt":"2024-12-27T01:33:27","guid":{"rendered":""},"modified":"2024-12-27T09:33:29","modified_gmt":"2024-12-27T01:33:29","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%ae%9e%e7%8e%b0%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998452.html","title":{"rendered":"\u5982\u4f55\u7528python\u5b9e\u73b0\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073859\/efa86ea1-333f-4982-84a7-737502309ee3.webp\" alt=\"\u5982\u4f55\u7528python\u5b9e\u73b0\u6392\u5e8f\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5b9e\u73b0\u6392\u5e8f\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\u4ee5\u53ca\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002<strong>Python\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u3001\u6392\u5e8f\u7b97\u6cd5\u7684\u9009\u62e9\u3001\u7a33\u5b9a\u6027\u548c\u6548\u7387\u7684\u8003\u8651<\/strong>\u662f\u5b9e\u73b0\u6392\u5e8f\u65f6\u9700\u8981\u5173\u6ce8\u7684\u51e0\u4e2a\u6838\u5fc3\u70b9\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u9488\u5bf9\u5176\u4e2d\u4e00\u79cd\u5c55\u5f00\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u5982<code>sorted()<\/code>\u548c\u5217\u8868\u5bf9\u8c61\u7684<code>sort()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u6392\u5e8f\u65b9\u5f0f\u3002\u8fd9\u4e9b\u65b9\u6cd5\u57fa\u4e8eTimsort\u7b97\u6cd5\u5b9e\u73b0\uff0c\u5177\u6709\u7a33\u5b9a\u6027\u548c\u9ad8\u6548\u6027\u3002Timsort\u662f\u4e00\u79cd\u6df7\u5408\u6392\u5e8f\u7b97\u6cd5\uff0c\u7ed3\u5408\u4e86\u5f52\u5e76\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u4f18\u52bf\uff0c\u80fd\u591f\u5728\u590d\u6742\u5ea6\u4e3aO(n log n)\u7684\u65f6\u95f4\u5185\u5b8c\u6210\u6392\u5e8f\u3002\u5176\u7a33\u5b9a\u6027\u4f53\u73b0\u5728\u5f53\u4e24\u4e2a\u5143\u7d20\u76f8\u7b49\u65f6\uff0c\u5b83\u4eec\u5728\u6392\u5e8f\u540e\u7684\u76f8\u5bf9\u4f4d\u7f6e\u4e0e\u6392\u5e8f\u524d\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PYTHON\u5185\u7f6e\u6392\u5e8f\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e24\u4e2a\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\uff1a<code>sorted()<\/code>\u548c<code>list.sort()<\/code>\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u57fa\u4e8eTimsort\u7b97\u6cd5\uff0c\u5177\u6709\u9ad8\u6548\u6027\u548c\u7a33\u5b9a\u6027\u3002\u4ee5\u4e0b\u662f\u5b83\u4eec\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h4>1. <code>sorted()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\uff0c\u800c\u4e0d\u6539\u53d8\u539f\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u5bf9\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u5177\u6709\u7075\u6d3b\u7684\u81ea\u5b9a\u4e49\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [3, 1, 4, 1, 5, 9, 2]<\/p>\n<p>sorted_numbers = sorted(numbers)<\/p>\n<p>print(&quot;Original list:&quot;, numbers)<\/p>\n<p>print(&quot;Sorted list:&quot;, sorted_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u63a5\u6536\u4e24\u4e2a\u53ef\u9009\u53c2\u6570\uff1a<code>key<\/code>\u548c<code>reverse<\/code>\u3002<code>key<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5e94\u7528\u4e8e\u6bcf\u4e2a\u5143\u7d20\u4ee5\u4ea7\u751f\u7528\u4e8e\u6bd4\u8f83\u7684\u503c\uff0c\u800c<code>reverse<\/code>\u53c2\u6570\u4e3a\u5e03\u5c14\u503c\uff0c\u51b3\u5b9a\u6392\u5e8f\u987a\u5e8f\u662f\u5426\u4e3a\u53cd\u5411\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528key\u53c2\u6570<\/p>\n<p>words = [&quot;banana&quot;, &quot;apple&quot;, &quot;cherry&quot;]<\/p>\n<p>sorted_words = sorted(words, key=len)<\/p>\n<p>print(&quot;Sorted by length:&quot;, sorted_words)<\/p>\n<h2><strong>\u4f7f\u7528reverse\u53c2\u6570<\/strong><\/h2>\n<p>sorted_numbers_desc = sorted(numbers, reverse=True)<\/p>\n<p>print(&quot;Sorted in descending order:&quot;, sorted_numbers_desc)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. <code>list.sort()<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>list.sort()<\/code>\u65b9\u6cd5\u4e0e<code>sorted()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c\u4f46\u5b83\u76f4\u63a5\u5728\u5217\u8868\u5bf9\u8c61\u4e0a\u8fdb\u884c\u6392\u5e8f\uff0c\u6539\u53d8\u539f\u5217\u8868\uff0c\u4e0d\u8fd4\u56de\u65b0\u5217\u8868\u3002\u5b83\u4e5f\u652f\u6301<code>key<\/code>\u548c<code>reverse<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [3, 1, 4, 1, 5, 9, 2]<\/p>\n<p>numbers.sort()<\/p>\n<p>print(&quot;Sorted list:&quot;, numbers)<\/p>\n<h2><strong>\u4f7f\u7528key\u548creverse\u53c2\u6570<\/strong><\/h2>\n<p>words = [&quot;banana&quot;, &quot;apple&quot;, &quot;cherry&quot;]<\/p>\n<p>words.sort(key=len, reverse=True)<\/p>\n<p>print(&quot;Sorted by length in descending order:&quot;, words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5e38\u89c1\u6392\u5e8f\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u6392\u5e8f\u7b97\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5192\u6ce1\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5192\u6ce1\u6392\u5e8f\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u901a\u8fc7\u91cd\u590d\u904d\u5386\u5217\u8868\u3001\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5e76\u4ea4\u6362\u5b83\u4eec\u7684\u4f4d\u7f6e\u6765\u5b8c\u6210\u6392\u5e8f\u3002\u5c3d\u7ba1\u5b83\u7684\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u65f6\u4ecd\u7136\u6709\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def bubble_sort(arr):<\/p>\n<p>    n = len(arr)<\/p>\n<p>    for i in range(n):<\/p>\n<p>        for j in range(0, n-i-1):<\/p>\n<p>            if arr[j] &gt; arr[j+1]:<\/p>\n<p>                arr[j], arr[j+1] = arr[j+1], arr[j]<\/p>\n<p>numbers = [3, 1, 4, 1, 5, 9, 2]<\/p>\n<p>bubble_sort(numbers)<\/p>\n<p>print(&quot;Bubble sorted list:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u9009\u62e9\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u6392\u5e8f\u901a\u8fc7\u53cd\u590d\u4ece\u672a\u6392\u5e8f\u90e8\u5206\u9009\u62e9\u6700\u5c0f\u5143\u7d20\u5e76\u5c06\u5176\u653e\u5728\u5df2\u6392\u5e8f\u90e8\u5206\u672b\u5c3e\u6765\u5b8c\u6210\u6392\u5e8f\u3002\u5176\u590d\u6742\u5ea6\u540c\u6837\u4e3aO(n^2)\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def selection_sort(arr):<\/p>\n<p>    n = len(arr)<\/p>\n<p>    for i in range(n):<\/p>\n<p>        min_idx = i<\/p>\n<p>        for j in range(i+1, n):<\/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>numbers = [3, 1, 4, 1, 5, 9, 2]<\/p>\n<p>selection_sort(numbers)<\/p>\n<p>print(&quot;Selection sorted list:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u63d2\u5165\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u63d2\u5165\u6392\u5e8f\u901a\u8fc7\u9010\u6b65\u6784\u5efa\u6709\u5e8f\u5217\u8868\uff0c\u63d2\u5165\u6bcf\u4e2a\u65b0\u5143\u7d20\u5230\u9002\u5f53\u4f4d\u7f6e\u3002\u5176\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u4f46\u5bf9\u51e0\u4e4e\u6392\u5e8f\u597d\u7684\u6570\u636e\u96c6\u8868\u73b0\u826f\u597d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def insertion_sort(arr):<\/p>\n<p>    for i in range(1, len(arr)):<\/p>\n<p>        key = arr[i]<\/p>\n<p>        j = i - 1<\/p>\n<p>        while j &gt;= 0 and key &lt; arr[j]:<\/p>\n<p>            arr[j + 1] = arr[j]<\/p>\n<p>            j -= 1<\/p>\n<p>        arr[j + 1] = key<\/p>\n<p>numbers = [3, 1, 4, 1, 5, 9, 2]<\/p>\n<p>insertion_sort(numbers)<\/p>\n<p>print(&quot;Insertion sorted list:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5feb\u901f\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5feb\u901f\u6392\u5e8f\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5206\u6cbb\u6392\u5e8f\u7b97\u6cd5\uff0c\u901a\u8fc7\u9009\u62e9\u4e00\u4e2a\u201c\u57fa\u51c6\u201d\u5143\u7d20\u5e76\u5212\u5206\u6570\u7ec4\uff0c\u5c06\u5c0f\u4e8e\u57fa\u51c6\u7684\u5143\u7d20\u653e\u5728\u5de6\u8fb9\uff0c\u5927\u4e8e\u57fa\u51c6\u7684\u5143\u7d20\u653e\u5728\u53f3\u8fb9\u3002\u5176\u5e73\u5747\u590d\u6742\u5ea6\u4e3aO(n log n)\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def quick_sort(arr):<\/p>\n<p>    if len(arr) &lt;= 1:<\/p>\n<p>        return arr<\/p>\n<p>    pivot = arr[len(arr) \/\/ 2]<\/p>\n<p>    left = [x for x in arr if x &lt; pivot]<\/p>\n<p>    middle = [x for x in arr if x == pivot]<\/p>\n<p>    right = [x for x in arr if x &gt; pivot]<\/p>\n<p>    return quick_sort(left) + middle + quick_sort(right)<\/p>\n<p>numbers = [3, 1, 4, 1, 5, 9, 2]<\/p>\n<p>sorted_numbers = quick_sort(numbers)<\/p>\n<p>print(&quot;Quick sorted list:&quot;, sorted_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6392\u5e8f\u7b97\u6cd5\u9009\u62e9\u4e0e\u6548\u7387<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u65f6\uff0c\u9700\u8003\u8651\u6570\u636e\u89c4\u6a21\u3001\u6570\u636e\u5206\u5e03\u4ee5\u53ca\u7b97\u6cd5\u7684\u7a33\u5b9a\u6027\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e00\u822c\u6027\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5c0f\u89c4\u6a21\u6570\u636e<\/strong>\uff1a\u63d2\u5165\u6392\u5e8f\u548c\u9009\u62e9\u6392\u5e8f\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u65f6\u8868\u73b0\u826f\u597d\uff0c\u7b80\u5355\u6613\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u5927\u89c4\u6a21\u6570\u636e<\/strong>\uff1a\u5feb\u901f\u6392\u5e8f\u548c\u5f52\u5e76\u6392\u5e8f\u662f\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u7406\u60f3\u9009\u62e9\u3002\u5feb\u901f\u6392\u5e8f\u901a\u5e38\u6bd4\u5f52\u5e76\u6392\u5e8f\u5feb\uff0c\u4f46\u5f52\u5e76\u6392\u5e8f\u5177\u6709\u7a33\u5b9a\u6027\u3002<\/li>\n<li><strong>\u51e0\u4e4e\u6392\u5e8f\u7684\u6570\u636e<\/strong>\uff1a\u63d2\u5165\u6392\u5e8f\u5bf9\u51e0\u4e4e\u6392\u5e8f\u7684\u6570\u636e\u975e\u5e38\u9ad8\u6548\u3002<\/li>\n<li><strong>\u5185\u5b58\u6709\u9650\u7684\u73af\u5883<\/strong>\uff1a\u9009\u62e9\u6392\u5e8f\u548c\u5feb\u901f\u6392\u5e8f\u4f7f\u7528\u539f\u5730\u6392\u5e8f\uff0c\u9002\u7528\u4e8e\u5185\u5b58\u6709\u9650\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Python\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u548c\u624b\u52a8\u5b9e\u73b0\u7684\u6392\u5e8f\u7b97\u6cd5\uff0cPython\u793e\u533a\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6392\u5e8f\u3002NumPy\u548cPandas\u662f\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u4e24\u4e2a\u5e38\u7528\u5e93\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6392\u5e8f\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. NumPy\u7684\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>NumPy\u5e93\u63d0\u4f9b\u4e86<code>numpy.sort()<\/code>\u51fd\u6570\u7528\u4e8e\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u652f\u6301\u591a\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5305\u62ec\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u548c\u5806\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.array([3, 1, 4, 1, 5, 9, 2])<\/p>\n<p>sorted_numbers = np.sort(numbers)<\/p>\n<p>print(&quot;NumPy sorted array:&quot;, sorted_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Pandas\u7684\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>Pandas\u5e93\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\uff0c<code>DataFrame<\/code>\u5bf9\u8c61\u7684<code>sort_values()<\/code>\u65b9\u6cd5\u53ef\u7528\u4e8e\u6839\u636e\u67d0\u4e00\u5217\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [25, 30, 20]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>sorted_df = df.sort_values(by=&#39;Age&#39;)<\/p>\n<p>print(&quot;Pandas sorted DataFrame:\\n&quot;, sorted_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6392\u5e8f\u7b97\u6cd5\u7684\u7a33\u5b9a\u6027\u548c\u590d\u6742\u5ea6<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u65f6\uff0c\u7a33\u5b9a\u6027\u548c\u590d\u6742\u5ea6\u662f\u4e24\u4e2a\u91cd\u8981\u7684\u8003\u91cf\u56e0\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1. \u7a33\u5b9a\u6027<\/h4>\n<\/p>\n<p><p>\u6392\u5e8f\u7b97\u6cd5\u7684\u7a33\u5b9a\u6027\u6307\u5728\u6392\u5e8f\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u662f\u5426\u4fdd\u6301\u4e0d\u53d8\u3002\u7a33\u5b9a\u6027\u5bf9\u9700\u8981\u4fdd\u6301\u539f\u6709\u987a\u5e8f\u7684\u6392\u5e8f\u4efb\u52a1\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5f53\u5bf9\u5305\u542b\u591a\u4e2a\u5b57\u6bb5\u7684\u8bb0\u5f55\u8fdb\u884c\u6392\u5e8f\u65f6\uff0c\u7a33\u5b9a\u6027\u53ef\u4ee5\u786e\u4fdd\u6b21\u8981\u5b57\u6bb5\u6392\u5e8f\u7684\u7ed3\u679c\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7a33\u5b9a\u6392\u5e8f<\/strong>\uff1a\u5f52\u5e76\u6392\u5e8f\u3001\u63d2\u5165\u6392\u5e8f\u3001\u5192\u6ce1\u6392\u5e8f\u3002<\/li>\n<li><strong>\u4e0d\u7a33\u5b9a\u6392\u5e8f<\/strong>\uff1a\u5feb\u901f\u6392\u5e8f\u3001\u9009\u62e9\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<p><h4>2. \u65f6\u95f4\u590d\u6742\u5ea6<\/h4>\n<\/p>\n<p><p>\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u8861\u91cf\u7b97\u6cd5\u6548\u7387\u7684\u4e00\u4e2a\u91cd\u8981\u6307\u6807\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5192\u6ce1\u6392\u5e8f<\/strong>\uff1aO(n^2)<\/li>\n<li><strong>\u9009\u62e9\u6392\u5e8f<\/strong>\uff1aO(n^2)<\/li>\n<li><strong>\u63d2\u5165\u6392\u5e8f<\/strong>\uff1aO(n^2)<\/li>\n<li><strong>\u5f52\u5e76\u6392\u5e8f<\/strong>\uff1aO(n log n)<\/li>\n<li><strong>\u5feb\u901f\u6392\u5e8f<\/strong>\uff1a\u5e73\u5747O(n log n)\uff0c\u6700\u5deeO(n^2)<\/li>\n<li><strong>\u5806\u6392\u5e8f<\/strong>\uff1aO(n log n)<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9e\u73b0\u6392\u5e8f\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\u4ee5\u53ca\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u89c4\u6a21\u3001\u6570\u636e\u5206\u5e03\u4ee5\u53ca\u7b97\u6cd5\u7684\u7a33\u5b9a\u6027\u548c\u7a7a\u95f4\u590d\u6742\u5ea6<\/strong>\u3002\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u7b80\u5355\u7684\u63d2\u5165\u6392\u5e8f\u548c\u9009\u62e9\u6392\u5e8f\u53ef\u80fd\u662f\u6700\u76f4\u63a5\u7684\u9009\u62e9\uff1b\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u5feb\u901f\u6392\u5e8f\u548c\u5f52\u5e76\u6392\u5e8f\u5219\u662f\u66f4\u4f18\u7684\u9009\u62e9\u3002\u6b64\u5916\uff0c\u5229\u7528NumPy\u548cPandas\u7b49\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u63d0\u9ad8\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u6548\u7387\u3002\u7406\u89e3\u4e0d\u540c\u6392\u5e8f\u7b97\u6cd5\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u505a\u51fa\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u6216<code>list.sort()<\/code>\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\uff0c\u53ef\u4ee5\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u4f20\u5165\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\uff0c\u4ece\u800c\u5f71\u54cd\u6392\u5e8f\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u6309\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]\nsorted_list = sorted(my_list, key=len)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u7528\u6237\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u8fdb\u884c\u7075\u6d3b\u6392\u5e8f\u3002<\/p>\n<p><strong>Python\u4e2d\u7684\u6392\u5e8f\u7b97\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>Python\u5185\u7f6e\u7684<code>sorted()<\/code>\u548c<code>list.sort()<\/code>\u65b9\u6cd5\u4f7f\u7528\u7684\u662fTimsort\u7b97\u6cd5\uff0c\u7ed3\u5408\u4e86\u5f52\u5e76\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u4f18\u70b9\u3002\u9664\u6b64\u4e4b\u5916\uff0cPython\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u548c\u5192\u6ce1\u6392\u5e8f\u3002\u901a\u8fc7\u5b9a\u4e49\u81ea\u5df1\u7684\u6392\u5e8f\u51fd\u6570\uff0c\u7528\u6237\u53ef\u4ee5\u4f53\u9a8c\u548c\u5b66\u4e60\u4e0d\u540c\u7684\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5b57\u5178\u5728Python\u4e2d\u662f\u65e0\u5e8f\u7684\uff0c\u4f46\u53ef\u4ee5\u6839\u636e\u952e\u6216\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u5178\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6309\u952e\u6216\u503c\u6392\u5e8f\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u8981\u6309\u5b57\u5178\u7684\u503c\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 3, &#39;b&#39;: 1, &#39;c&#39;: 2}\nsorted_dict = dict(sorted(my_dict.items(), key=lambda item: item[1]))\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u6709\u5e8f\u5b57\u5178\uff0c\u6839\u636e\u503c\u7684\u5927\u5c0f\u8fdb\u884c\u6392\u5217\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u6392\u5e8f\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\u4ee5\u53ca\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002Python [&hellip;]","protected":false},"author":3,"featured_media":998466,"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\/998452"}],"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=998452"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998452\/revisions"}],"predecessor-version":[{"id":998467,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998452\/revisions\/998467"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998466"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}