{"id":7635,"date":"2024-07-12T19:01:01","date_gmt":"2024-07-12T11:01:01","guid":{"rendered":""},"modified":"2024-07-12T19:01:01","modified_gmt":"2024-07-12T11:01:01","slug":"\u5199\u51fa\u5feb\u901f\u6392\u5e8f\u6c42\u7b2ck\u5927\u7684\u6570\u7684\u8fc7\u7a0b_\u4efb\u4f55\u4e00\u4e2a\u57fa\u4e8e\u6bd4\u8f83\u7684\u5185\u90e8\u6392\u5e8f\u7b97\u6cd5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7635.html","title":{"rendered":"\u5199\u51fa\u5feb\u901f\u6392\u5e8f\u6c42\u7b2ck\u5927\u7684\u6570\u7684\u8fc7\u7a0b_\u4efb\u4f55\u4e00\u4e2a\u57fa\u4e8e\u6bd4\u8f83\u7684\u5185\u90e8\u6392\u5e8f\u7b97\u6cd5"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u5feb\u901f\u6392\u5e8f\u7b80\u4ecb:<\/p>\n

\u5feb\u901f\u6392\u5e8f\u6c42\u7b2ck\u5c0f\u7684\u6570\uff0c\u601d\u60f3\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u662f\u5982\u679c\u8981\u67e5\u627e\u7684k\u6bd4\u5f53\u524d\u4e0b\u6807i\u5c0f\uff0c\u5219\u53ea\u9012\u5f52\u5de6\u90e8\u5206\uff0c\u5927\u6216\u76f8\u7b49\u5219\u9012\u5f52\u53f3\u90e8\u5206\u3002\u5f53\u7136\u7531\u4e8e\u6570\u7ec4\u4e0b\u6807\u4ece0\u5f00\u59cb\uff0c\u6240\u4ee5\u5e94\u8be5\u662fk-1\uff08\u6bd4\u5982\u7b2c\u4e00\u5927\u7684\u6570\u6570\u7ec4\u4e0b\u6807\u4e3a0)\u3002\u539f\u7406\u5c31\u662f\u5feb\u901f\u6392\u5e8f\u662f\u4ee5\u4e00\u4e2a\u5143\u7d20\u4e3a\u5206\u9694\u7684\uff0c\u5982\u679c\u6c42\u7b2ck\u5927\u7684\u5143\u7d20\uff0c\u4e5f\u5c31\u662f\u6c42\u7b2cn-k+1\u5c0f\u7684\u5143\u7d20\u3002<\/em><\/p>\n

\u539f\u7406\uff1a<\/p>\n

\u5feb\u901f\u6392\u5e8f\u6c42\u7b2ck\u5c0f\u7684\u6570\uff0c\u601d\u60f3\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u662f\u5982\u679c\u8981\u67e5\u627e\u7684k\u6bd4\u5f53\u524d\u4e0b\u6807low\u5c0f\uff0c\u5219\u53ea\u9012\u5f52\u5de6\u90e8\u5206\uff0c\u5927\u5219\u9012\u5f52\u53f3\u90e8\u5206\uff0c\u76f8\u7b49\u5219\u9012\u5f52\u53f3\u90e8\u5206\uff0c\u5f53\u7136\u7531\u4e8e\u6570\u7ec4\u4e0b\u6807\u4ece0\u5f00\u59cb\uff0c\u6240\u4ee5\u5e94\u8be5\u662fk-1,\uff08\u6bd4\u5982\u7b2c\u4e00\u5927\u7684\u6570\u6570\u7ec4\u4e0b\u6807\u4e3a0)\uff0c\u539f\u7406\u5c31\u662f\u5feb\u901f\u6392\u5e8f\u662f\u4ee5\u4e00\u4e2a\u5143\u7d20\u4e3a\u5206\u9694\u7684\uff0c\u5982\u679c\u6c42\u7b2ck\u5927\u7684\u5143\u7d20\uff0c\u5c31\u662f\u6c42\u7b2cn-k+1\u5c0f\u7684\u5143\u7d20.<\/p>\n

\u7a0b\u5e8f\uff1a<\/p>\n

#<\/span>include<\/span> <stdio.h><\/span><\/span> #<\/span>include<\/span> <stdlib.h><\/span><\/span> int<\/span> quick_select<\/span>(<\/span>int<\/span> a[<\/span>]<\/span>,<\/span> int<\/span> l,<\/span> int<\/span> r,<\/span> int<\/span> k)<\/span> { \n   <\/span> \/\/r\u603b\u6570 \u7b2ck\u5c0f<\/span> int<\/span> p =<\/span> rand<\/span>(<\/span>)<\/span> %<\/span> (<\/span>r -<\/span> l +<\/span> 1<\/span>)<\/span> +<\/span> l;<\/span> int<\/span> x =<\/span> a[<\/span>p]<\/span>;<\/span> { \n   <\/span>int<\/span> t =<\/span> a[<\/span>p]<\/span>;<\/span> a[<\/span>p]<\/span> =<\/span> a[<\/span>r]<\/span>;<\/span> a[<\/span>r]<\/span> =<\/span> t;<\/span>}<\/span> \/\/p+1\u4e2a\u4e0e\u6700\u540e\u4e00\u4e2a\u4ea4\u6362\uff1b<\/span> int<\/span> i =<\/span> l,<\/span> j =<\/span> r;<\/span> while<\/span>(<\/span>i <<\/span> j)<\/span> { \n   <\/span> while<\/span>(<\/span>i <<\/span> j &&<\/span> a[<\/span>i]<\/span> <<\/span> x)<\/span> i++<\/span>;<\/span> \/\/\u6b63\u5e8f\u904d\u5386\u6570\u7ec4\uff0c\u67e5\u627e\u5c0f\u4e8e\u6700\u540e\u4e00\u4e2a\u6570\u636e\u7684\u4e2a\u6570<\/span> if<\/span>(<\/span>i <<\/span> j)<\/span> { \n   <\/span> \/\/\u6761\u4ef6\u662f\u5e76\u975e\u6240\u6709\u7684\u6570\u90fd\u5c0f\u4e8e\u521a\u521a\u53d6\u51fa\u7684\u968f\u673a\u6570<\/span> a[<\/span>j]<\/span> =<\/span> a[<\/span>i]<\/span>;<\/span> \/\/\u5c06\u6570\u7ec4\u7684i+1\u4f4d\u8d4b\u7ed9\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4f4d\uff1b<\/span> j--<\/span>;<\/span> }<\/span> while<\/span>(<\/span>i <<\/span> j &&<\/span> a[<\/span>j]<\/span> ><\/span> x)<\/span> j--<\/span>;<\/span> \/\/\u9006\u5e8f\u904d\u5386\u6570\u7ec4\uff0c\u67e5\u627e\u5927\u4e8e\u6700\u540e\u4e00\u4e2a\u6570\u636e\u7684\u6240\u6709\u6570\u636e\u4e2a\u6570\uff1b<\/span> if<\/span>(<\/span>i <<\/span> j)<\/span> { \n   <\/span> a[<\/span>i]<\/span> =<\/span> a[<\/span>j]<\/span>;<\/span> \/\/\u5c06\u6570\u7ec4\u7684j+1\u4f4d\u8d4b\u7ed9\u6570\u7ec4\u7684\u7b2ci+1\u4f4d\uff1b<\/span> i++<\/span>;<\/span> }<\/span> }<\/span> a[<\/span>i]<\/span> =<\/span> x;<\/span> p =<\/span> i;<\/span> if<\/span>(<\/span>i -<\/span> l +<\/span> 1<\/span> ==<\/span> k)<\/span> return<\/span> a[<\/span>i]<\/span>;<\/span> if<\/span>(<\/span>i -<\/span> l +<\/span> 1<\/span> <<\/span> k)<\/span> return<\/span> quick_select<\/span>(<\/span>a,<\/span>i+<\/span>1<\/span>,<\/span>r,<\/span>k-<\/span>i+<\/span>l-<\/span>1<\/span>)<\/span>;<\/span> else<\/span> return<\/span> quick_select<\/span>(<\/span>a,<\/span> l,<\/span> i -<\/span> 1<\/span>,<\/span> k)<\/span>;<\/span> }<\/span> int<\/span> main<\/span>(<\/span>)<\/span> { \n   <\/span> printf<\/span>(<\/span>\"\u59d3\u540d\uff1a\u5f20\u4fca \u5b66\u53f7\uff1a \\n\u5bf9\u6570\u5217\u67e5\u627e\u5b8c\u6bd5\uff1a\\n\"<\/span>)<\/span>;<\/span> int<\/span> a[<\/span>]<\/span> =<\/span> { \n   <\/span>1<\/span>,<\/span> 4<\/span>,<\/span> 7<\/span>,<\/span> 10<\/span>,<\/span> 5<\/span>,<\/span> 17<\/span>,<\/span> 33<\/span>,<\/span> 58<\/span>,<\/span> 16<\/span>,<\/span> 25<\/span>,<\/span> 55<\/span>,<\/span> 13<\/span>,<\/span> 29<\/span>,<\/span> 24<\/span>,<\/span> 32<\/span>}<\/span>;<\/span> printf<\/span>(<\/span>\"%d\\n\"<\/span>,<\/span> quick_select<\/span>(<\/span>a,<\/span> 0<\/span>,<\/span> 14<\/span>,<\/span> 13<\/span>)<\/span>)<\/span>;<\/span> return<\/span> 0<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

\"\u5199\u51fa\u5feb\u901f\u6392\u5e8f\u6c42\u7b2ck\u5927\u7684\u6570\u7684\u8fc7\u7a0b_\u4efb\u4f55\u4e00\u4e2a\u57fa\u4e8e\u6bd4\u8f83\u7684\u5185\u90e8\u6392\u5e8f\u7b97\u6cd5
\u6d4b\u8bd5\uff1a
\u4f7f\u7528\u6570\u5217\uff1a{1, 4, 7, 10, 5, 17, 33, 58, 16, 25, 55, 13, 29, 24, 32}
\"\u5199\u51fa\u5feb\u901f\u6392\u5e8f\u6c42\u7b2ck\u5927\u7684\u6570\u7684\u8fc7\u7a0b_\u4efb\u4f55\u4e00\u4e2a\u57fa\u4e8e\u6bd4\u8f83\u7684\u5185\u90e8\u6392\u5e8f\u7b97\u6cd5
\u590d\u6742\u5ea6\uff1a<\/p>\n

\u4f7f\u7528\u4e86\u7c7b\u4f3c\u5feb\u901f\u6392\u5e8f\u4e2d\u7684\u5206\u6cbb\u7b97\u6cd5\uff0c\u671f\u671b\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(N)\uff0c\u5229\u7528\u4e2d\u4f4d\u6570\u7684\u4e2d\u4f4d\u6570\u7b97\u6cd5\u5f97\u5230\u7684\u6570\u4f5c\u4e3a\u5212\u5206\u53ef\u4ee5\u5b9e\u73b0\u6700\u4f18\u5212\u5206\uff0c\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\u80fd\u5b9e\u73b0O\uff08n\uff09\u590d\u6742\u5ea6\u3002\u63a5\u4e0b\u6765\u8003\u8651\u53ef\u80fd\u51fa\u73b0\u8bb8\u591a\u91cd\u590d\u7684\u6570\uff0c\u5047\u8bbe\u6570\u7ec4\u4e2d\u6240\u6709\u7684\u6570\u5168\u90e8\u76f8\u540c\uff0c\u6bcf\u6b21\u5212\u5206\u4e4b\u540e\u90fd\u662f\u5f53\u524d\u533a\u95f4\u7684\u53f3\u7aef\u70b9\uff0c\u5373\u4f1a\u9000\u5316\u5230O\uff08n^2\uff09\u590d\u6742\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5199\u51fa\u5feb\u901f\u6392\u5e8f\u6c42\u7b2ck\u5927\u7684\u6570\u7684\u8fc7\u7a0b_\u4efb\u4f55\u4e00\u4e2a\u57fa\u4e8e\u6bd4\u8f83\u7684\u5185\u90e8\u6392\u5e8f\u7b97\u6cd5\u5feb\u901f\u6392\u5e8f\u7b80\u4ecb:\u5feb\u901f\u6392\u5e8f\u6c42\u7b2ck\u5c0f\u7684\u6570\uff0c\u601d\u60f3\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u662f\u5982\u679c...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7635"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7635"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7635\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}