{"id":747,"date":"2011-04-22T21:46:14","date_gmt":"2011-04-22T13:46:14","guid":{"rendered":"http:\/\/www.kumouse.com\/?p=747"},"modified":"2011-04-22T21:46:14","modified_gmt":"2011-04-22T13:46:14","slug":"halfind-%e6%8a%98%e5%8d%8a%e6%9f%a5%e6%89%be","status":"publish","type":"post","link":"https:\/\/www.kumouse.com\/?p=747","title":{"rendered":"halfind &#8211; \u6298\u534a\u67e5\u627e"},"content":{"rendered":"<p>#include &lt;stdio.h&gt;<br \/>\n\t#include &lt;string.h&gt;<br \/>\n\t#include &lt;stdlib.h&gt;<br \/>\n\t#define LEN 8<\/p>\n<p>\tint halfind(int *a,int key)<br \/>\n\t{<br \/>\n\t&nbsp;&nbsp;&nbsp; int start =0,end=LEN-1,mid;<br \/>\n\t&nbsp;&nbsp;&nbsp; while(start &lt;= end){<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; mid=(start+end)\/2;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(a[mid] &gt; key){<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; end=mid -1;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else if(a[mid] &lt; key){<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; start=mid+1;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return mid;&nbsp;&nbsp;&nbsp; <\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; }<\/p>\n<p>\t&nbsp;&nbsp;&nbsp; return -1;<br \/>\n\t}<\/p>\n<p>\tint main(int argc,char **argv)<br \/>\n\t{<br \/>\n\t&nbsp;&nbsp;&nbsp; int a[]={1,2,3,4,5,6,7,8};<br \/>\n\t&nbsp;&nbsp;&nbsp; printf(&quot;%dn&quot;,halfind(a,atoi(argv[1])));<br \/>\n\t&nbsp;&nbsp;&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; return 0;<br \/>\n\t}<\/p>\n<p>&nbsp;<\/p>\n<p>==============\u9012\u5f52\u65b9\u5f0f===============<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n\t#include &lt;string.h&gt;<br \/>\n\t#include &lt;stdlib.h&gt;<br \/>\n\t#define LEN 8<br \/>\n\tint a[]={1,2,3,4,5,6,7,8};<\/p>\n<p>\tint halfind(int start,int end,int key)<br \/>\n\t{<br \/>\n\t&nbsp;&nbsp;&nbsp; int mid;<br \/>\n\t&nbsp;&nbsp;&nbsp; if(start &lt;= end){<\/p>\n<p>\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; mid=(start+end)\/2;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(a[mid] &gt; key){<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; halfind(start,mid -1,key);<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else if(a[mid] &lt; key){<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; halfind(mid+1,end,key);<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else{<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf(&quot;%dn&quot;,mid);<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return mid;&nbsp;&nbsp;&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<\/p>\n<p>\t&nbsp;&nbsp;&nbsp; }<\/p>\n<p>\t&nbsp;&nbsp;&nbsp; return -1;<br \/>\n\t}<\/p>\n<p>\tint main(int argc,char **argv)<br \/>\n\t{<br \/>\n\t&nbsp;&nbsp;&nbsp; printf(&quot;%dn&quot;,halfind(0,LEN-1,atoi(argv[1])));<br \/>\n\t&nbsp;&nbsp;&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; return 0;<br \/>\n\t}<br \/>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #inc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-747","post","type-post","status-publish","format-standard","hentry","category-linux-c"],"_links":{"self":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=747"}],"version-history":[{"count":0,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/747\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}