{"id":296988,"date":"2024-05-20T16:16:05","date_gmt":"2024-05-20T08:16:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/296988.html"},"modified":"2024-05-20T16:16:26","modified_gmt":"2024-05-20T08:16:26","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87-c-%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0%e5%86%92%e6%b3%a1%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/296988.html","title":{"rendered":"\u5982\u4f55\u901a\u8fc7 C \u8bed\u8a00\u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27144653\/a502dbec-31cc-4f33-bb41-c0fe3c5d6c86.webp\" alt=\"\u5982\u4f55\u901a\u8fc7 C \u8bed\u8a00\u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f\" \/><\/p>\n<p><p>\u5192\u6ce1\u6392\u5e8f\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u901a\u8fc7<strong>\u91cd\u590d\u904d\u5386\u5f85\u6392\u5e8f\u7684\u6570\u5217\u3001\u6bd4\u8f83\u4e24\u4e2a\u76f8\u90bb\u5143\u7d20\u3001\u4ea4\u6362\u987a\u5e8f\u9519\u8bef\u7684\u5143\u7d20<\/strong>\uff0c\u5728\u904d\u5386\u4e2d\u4e0d\u65ad\u5c06\u8f83\u5927\u7684\u5143\u7d20\u50cf\u201c\u5192\u6ce1\u201d\u4e00\u822c\u9010\u6b65\u79fb\u81f3\u6570\u5217\u7684\u672b\u7aef\uff0c\u76f4\u81f3\u6574\u4e2a\u6570\u5217\u6392\u5e8f\u5b8c\u6210\u3002\u5728C\u8bed\u8a00\u4e2d\u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f\u4e3b\u8981\u5305\u62ec<strong>\u8bbe\u7f6e\u5faa\u73af\u8fdb\u884c\u5143\u7d20\u6bd4\u8f83\u3001\u4ea4\u6362\u4e0d\u5408\u987a\u5e8f\u7684\u5143\u7d20\u3001\u4f18\u5316\u7b97\u6cd5\u9632\u6b62\u4e0d\u5fc5\u8981\u7684\u904d\u5386<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u672c\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u8fdb\u884c\u5192\u6ce1\u6392\u5e8f\u7684\u9996\u6b65\u662f\u7f16\u5199\u6bd4\u8f83\u53ca\u4ea4\u6362\u76f8\u90bb\u4e24\u4e2a\u5143\u7d20\u7684\u529f\u80fd\u3002\u6574\u4e2a\u6392\u5e8f\u8fc7\u7a0b\u4e2d\uff0c\u91cd\u590d\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\uff0c\u5e76\u5728\u5fc5\u8981\u65f6\u4ea4\u6362\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>void bubbleSort(int arr[], int n) {<\/p>\n<p>    int i, j, temp;<\/p>\n<p>    for (i = 0; i &lt; n-1; i++) {<\/p>\n<p>        for (j = 0; j &lt; n-i-1; j++) {<\/p>\n<p>            if (arr[j] &gt; arr[j+1]) {<\/p>\n<p>                \/\/ \u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e<\/p>\n<p>                temp = arr[j];<\/p>\n<p>                arr[j] = arr[j+1];<\/p>\n<p>                arr[j+1] = temp;<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>void printArray(int arr[], int size) {<\/p>\n<p>    int i;<\/p>\n<p>    for (i = 0; i &lt; size; i++)<\/p>\n<p>        printf(&quot;%d &quot;, arr[i]);<\/p>\n<p>    printf(&quot;\\n&quot;);<\/p>\n<p>}<\/p>\n<p>int m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    int arr[] = {64, 34, 25, 12, 22, 11, 90};<\/p>\n<p>    int n = sizeof(arr)\/sizeof(arr[0]);<\/p>\n<p>    bubbleSort(arr, n);<\/p>\n<p>    printf(&quot;Sorted array: \\n&quot;);<\/p>\n<p>    printArray(arr, n);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f18\u5316\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u57fa\u672c\u7684\u5192\u6ce1\u6392\u5e8f\u5df2\u7ecf\u80fd\u591f\u6b63\u786e\u5b8c\u6210\u6392\u5e8f\u4efb\u52a1\uff0c\u4f46\u662f\u5b83\u6ca1\u6709\u8003\u8651\u5230\u6570\u5217\u5df2\u7ecf\u6392\u5e8f\u5b8c\u6210\u7684\u60c5\u51b5\u3002\u5bf9\u6b64\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d\uff0c\u5982\u679c\u5728\u4e00\u6b21\u904d\u5386\u4e2d\u5143\u7d20\u6ca1\u6709\u53d1\u751f\u4ea4\u6362\uff0c\u5373\u53ef\u63d0\u65e9\u7ed3\u675f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">void optimizedBubbleSort(int arr[], int n) {<\/p>\n<p>    int i, j, temp;<\/p>\n<p>    int swapped; \/\/ \u521d\u59cb\u5316\u4e00\u4e2a\u4ea4\u6362\u6807\u5fd7\u4f4d<\/p>\n<p>    for (i = 0; i &lt; n-1; i++) {<\/p>\n<p>        swapped = 0; \/\/ \u5728\u6bcf\u8f6e\u904d\u5386\u5f00\u59cb\u65f6\u91cd\u7f6e\u4ea4\u6362\u6807\u5fd7\u4f4d<\/p>\n<p>        for (j = 0; j &lt; n-i-1; j++) {<\/p>\n<p>            if (arr[j] &gt; arr[j+1]) {<\/p>\n<p>                temp = arr[j];<\/p>\n<p>                arr[j] = arr[j+1];<\/p>\n<p>                arr[j+1] = temp;<\/p>\n<p>                swapped = 1; \/\/ \u53d1\u751f\u4e86\u4ea4\u6362<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>        \/\/ \u82e5\u5728\u8fd9\u8f6e\u904d\u5386\u4e2d\u6ca1\u6709\u53d1\u751f\u4ea4\u6362\uff0c\u5219\u6570\u7ec4\u5df2\u7ecf\u6392\u597d\u5e8f<\/p>\n<p>        if (swapped == 0)<\/p>\n<p>            break;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5\u7684\u6027\u80fd\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5192\u6ce1\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff08\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u9006\u5e8f\uff09\u4e3a<strong>O(n^2)<\/strong>\uff0c\u5176\u4e2dn\u662f\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u3002\u5728\u6700\u597d\u7684\u60c5\u51b5\u4e0b\uff08\u8f93\u5165\u6570\u7ec4\u5df2\u7ecf\u662f\u6392\u5e8f\u597d\u7684\uff09\uff0c\u4f18\u5316\u8fc7\u7684\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5\u53ef\u4ee5\u8fbe\u5230<strong>O(n)<\/strong> \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u5c3d\u7ba1\u4f18\u5316\u540e\u7684\u5192\u6ce1\u6392\u5e8f\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff0c\u4f46\u901a\u5e38\u7531\u4e8e\u5176O(n^2)\u7684\u590d\u6742\u5ea6\uff0c\u5b83\u5e76\u4e0d\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5192\u6ce1\u6392\u5e8f\u7684\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u5192\u6ce1\u6392\u5e8f\u7684\u6548\u7387\u76f8\u5bf9\u8f83\u4f4e\uff0c\u56e0\u6b64\u5b83\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u7684\u6392\u5e8f\uff0c\u6216\u8005\u662f\u51e0\u4e4e\u5df2\u7ecf\u6392\u597d\u5e8f\u7684\u6570\u636e\u3002\u5728\u5de5\u4e1a\u5b9e\u8df5\u548c\u5927\u89c4\u6a21\u5e94\u7528\u4e2d\uff0c\u66f4\u5feb\u7684\u6392\u5e8f\u7b97\u6cd5\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u7b49\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5192\u6ce1\u6392\u5e8f\u662f\u6392\u5e8f\u7b97\u6cd5\u4e2d\u6700\u7b80\u5355\u7684\u4e00\u79cd\uff0c\u9002\u5408\u7528\u4e8e\u6559\u5b66\u548c\u7406\u89e3\u6392\u5e8f\u7b97\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u3002\u5b83\u901a\u8fc7\u4e0d\u65ad\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u6765\u5b9e\u73b0\u6392\u5e8f\uff0c\u5c3d\u7ba1\u4e0d\u591f\u9ad8\u6548\uff0c\u4f46\u5176\u5b9e\u73b0\u7b80\u5355\u3001\u76f4\u89c2\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5bf9\u4e8e\u6548\u7387\u6709\u66f4\u9ad8\u9700\u6c42\u7684\u573a\u5408\uff0c\u5e94\u8be5\u9009\u62e9\u66f4\u4e3a\u5148\u8fdb\u7684\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5192\u6ce1\u6392\u5e8f\u662f\u4ec0\u4e48\uff0c\u4e3a\u4ec0\u4e48\u8981\u7528 C \u8bed\u8a00\u6765\u5b9e\u73b0\uff1f<\/strong><\/p>\n<p>\u5192\u6ce1\u6392\u5e8f\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u91cd\u590d\u5730\u4ece\u5f85\u6392\u5e8f\u7684\u5e8f\u5217\u4e2d\u6bd4\u8f83\u76f8\u90bb\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u8f83\u5927\uff08\u6216\u8f83\u5c0f\uff09\u7684\u5143\u7d20\u4ea4\u6362\u5230\u53f3\u4fa7\uff0c\u76f4\u5230\u6574\u4e2a\u5e8f\u5217\u6392\u597d\u5e8f\u3002C \u8bed\u8a00\u662f\u4e00\u79cd\u9ad8\u6548\u4e14\u7075\u6d3b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u975e\u5e38\u9002\u5408\u7528\u6765\u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<p><strong>2. \u5192\u6ce1\u6392\u5e8f\u7684\u5b9e\u73b0\u6b65\u9aa4\u662f\u600e\u6837\u7684\uff1f<\/strong><\/p>\n<p>\u5192\u6ce1\u6392\u5e8f\u7684\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u904d\u5386\u5f85\u6392\u5e8f\u7684\u5e8f\u5217\uff0c\u6bd4\u8f83\u76f8\u90bb\u7684\u5143\u7d20\u3002<\/li>\n<li>\u5982\u679c\u987a\u5e8f\u4e0d\u5bf9\uff0c\u5c31\u4ea4\u6362\u5b83\u4eec\u7684\u4f4d\u7f6e\uff0c\u5c06\u8f83\u5927\uff08\u6216\u8f83\u5c0f\uff09\u7684\u5143\u7d20\u5f80\u53f3\u79fb\u52a8\u3002<\/li>\n<li>\u7ee7\u7eed\u8fdb\u884c\u4e0b\u4e00\u8f6e\u7684\u6bd4\u8f83\uff0c\u76f4\u5230\u6574\u4e2a\u5e8f\u5217\u6392\u5e8f\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<p><strong>3. \u5982\u4f55\u7528 C \u8bed\u8a00\u6765\u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 C \u8bed\u8a00\u7684\u6570\u7ec4\u548c\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684 C \u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nvoid bubbleSort(int arr[], int n) {\n    for (int i = 0; i &lt; n - 1; i++) {\n        for (int j = 0; j &lt; n - i - 1; j++) {\n            if (arr[j] &gt; arr[j + 1]) {\n                int temp = arr[j];\n                arr[j] = arr[j + 1];\n                arr[j + 1] = temp;\n            }\n        }\n    }\n}\n\nint main() {\n    int arr[] = {5, 2, 8, 12, 1};\n    int n = sizeof(arr) \/ sizeof(arr[0]);\n\n    bubbleSort(arr, n);\n\n    printf(&quot;\u6392\u5e8f\u540e\u7684\u6570\u7ec4\uff1a&quot;);\n    for (int i = 0; i &lt; n; i++) {\n        printf(&quot;%d &quot;, arr[i]);\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>bubbleSort<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f\u3002\u901a\u8fc7\u4e24\u4e2a\u5d4c\u5957\u7684\u5faa\u73af\uff0c\u904d\u5386\u5f85\u6392\u5e8f\u7684\u6570\u7ec4\uff0c\u5e76\u8fdb\u884c\u5143\u7d20\u7684\u6bd4\u8f83\u548c\u4ea4\u6362\u3002\u6700\u540e\uff0c\u5728 <code>main<\/code> \u51fd\u6570\u4e2d\u8c03\u7528 <code>bubbleSort<\/code> \u51fd\u6570\uff0c\u5e76\u6253\u5370\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u4ee5\u4e0a\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5192\u6ce1\u6392\u5e8f\u5b9e\u73b0\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u6392\u5e8f\u53ef\u80fd\u6548\u7387\u8f83\u4f4e\uff0c\u53ef\u4ee5\u8003\u8651\u4f18\u5316\u7b97\u6cd5\u6216\u4f7f\u7528\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5192\u6ce1\u6392\u5e8f\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u901a\u8fc7\u91cd\u590d\u904d\u5386\u5f85\u6392\u5e8f\u7684\u6570\u5217\u3001\u6bd4\u8f83\u4e24\u4e2a\u76f8\u90bb\u5143\u7d20\u3001\u4ea4\u6362\u987a\u5e8f\u9519\u8bef\u7684\u5143\u7d20\uff0c\u5728\u904d\u5386\u4e2d\u4e0d\u65ad\u5c06 [&hellip;]","protected":false},"author":3,"featured_media":297020,"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\/296988"}],"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=296988"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/296988\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/297020"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=296988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=296988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=296988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}