{"id":709,"date":"2019-11-27T14:55:11","date_gmt":"2019-11-27T14:55:11","guid":{"rendered":"http:\/\/blog.davcloud.top\/?p=709"},"modified":"2020-06-11T16:45:03","modified_gmt":"2020-06-11T16:45:03","slug":"c%ef%bc%9a%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/blog.davcloud.top\/?p=709&lang=zh","title":{"rendered":"C++\uff1a\u5feb\u901f\u6392\u5e8f"},"content":{"rendered":"\n<h2>\u6458\u8981<\/h2>\n\n\n\n<p>\u8fd9\u7bc7\u6587\u7ae0\u5c06\u4f1a\u8bb2\u89e3\u5982\u4f55\u7528C++\u5b9e\u73b0\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u3002\u5c3d\u7ba1STL\u5e93\u4e2d\u5df2\u63d0\u4f9b\u4e86\u6392\u5e8f\u51fd\u6570\uff0c\u4f46\u662f\u7406\u89e3\u5982\u4f55\u901a\u8fc7\u7b80\u5355\u7684\u5faa\u73af\u5b9e\u73b0\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u8fd8\u662f\u6709\u5fc5\u8981\u7684\u3002<\/p>\n\n\n\n<h2>\u539f\u7406<\/h2>\n\n\n\n<p>\u5bf9\u4e8e\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u66f4\u7b80\u6d01\u7684\u7406\u89e3\uff0c\u53ef\u8bbf\u95ee\u7f51\u7ad9<a href=\"https:\/\/visualgo.net\/\">https:\/\/visualgo.net\/<\/a>\u3002<\/p>\n\n\n\n<h2>C++\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;vector>\n#include &lt;stdio.h>\n\nusing namespace std;\n\nvoid quick_sort(vector&lt;int> *serial, int left_pos, int right_pos);\n\nint main(int argc, char* argv&#91;])\n{\n    vector&lt;int> serial;\n    int temp = 0;\n\n    \/\/Hint sentence to ask user input numbers.\n    cout &lt;&lt; \"Please input numbers with space to seperate and finished with any non-number character:\" &lt;&lt; endl;\n\n    \/\/Use a vector as dynamic array to store numbers.\n    while(cin >> temp) serial.push_back(temp);\n\n    \/\/Call quick_sort function.\n    quick_sort(&amp;serial, 0, serial.size()-1);\n\n    \/\/Print the result.\n    cout &lt;&lt; endl &lt;&lt; \"From min to max:\" &lt;&lt; endl;\n    for(int i = 0; i &lt; serial.size(); i++)\n        cout &lt;&lt; serial&#91;i] &lt;&lt; \" \";\n    cout &lt;&lt; endl;\n\n    return 0;\n}\n\nvoid quick_sort(vector&lt;int> *serial, int left, int right)\n{\n    int temp = 0, base, base_pos;\n    int left_pos, right_pos;\n\n    \/\/Flag and condition for recursive finished.\n    if(left > right) return;\n\n    \/\/The first number on the left is the base number by default.\n    base = (*serial)&#91;left];\n    base_pos = left;\n\n    \/\/Specifies the numbers at both ends of this sort.\n    left_pos = left;\n    right_pos = right;\n\n    \/\/When the left and right are not search to the same number.\n    while(left_pos != right_pos)\n    {\n        \/\/Search from right one by one until the searched number &lt; base number.\n        while(((*serial)&#91;right_pos] >= base) &amp;&amp; (left_pos &lt; right_pos)) right_pos--;\n        \/\/Search from left one by one until the searched number > base number.\n        while(((*serial)&#91;left_pos] &lt;= base) &amp;&amp; (left_pos &lt; right_pos)) left_pos++;\n\n        \/\/Swap the two searched number.\n        if(left_pos != right_pos)\n        {\n            temp = (*serial)&#91;left_pos];\n            (*serial)&#91;left_pos] = (*serial)&#91;right_pos];\n            (*serial)&#91;right_pos] = temp;\n        }\n    }\n\n    \/* When it become the same number for searching from both sides, swap the current \n    number and the base number to confirm the position of base number. *\/\n    (*serial)&#91;base_pos] = (*serial)&#91;left_pos];\n    (*serial)&#91;left_pos] = base;\n\n    \/\/Recursive to continue sorting for the left and right parts.\n    quick_sort(serial, left, left_pos-1);\n    quick_sort(serial, left_pos+1, right);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981 \u8fd9\u7bc7\u6587\u7ae0\u5c06\u4f1a\u8bb2\u89e3\u5982\u4f55\u7528C++\u5b9e\u73b0\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u3002\u5c3d\u7ba1STL\u5e93\u4e2d\u5df2\u63d0\u4f9b\u4e86\u6392\u5e8f\u51fd\u6570\uff0c\u4f46\u662f\u7406\u89e3\u5982\u4f55\u901a\u8fc7\u7b80\u5355\u7684\u5faa\u73af\u5b9e\u73b0\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u8fd8\u662f\u6709\u5fc5\u8981\u7684\u3002 \u539f\u7406 \u5bf9\u4e8e\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u66f4\u7b80\u6d01\u7684\u7406\u89e3\uff0c\u53ef\u8bbf\u95ee\u7f51\u7ad9https:\/\/visualgo.net\/\u3002 C++\u4ee3\u7801<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[398],"tags":[430,554],"_links":{"self":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/709"}],"collection":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=709"}],"version-history":[{"count":2,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/709\/revisions"}],"predecessor-version":[{"id":712,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/709\/revisions\/712"}],"wp:attachment":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}