ÎÒÃǼÙÉèÖ»ÓÐ 10 ¸ö¶©µ¥£¬¶©µ¥½ð¶î·Ö±ðÊÇ£º8£¬11£¬19£¬23£¬27£¬33£¬45£¬55£¬67£¬98¡£
ÀûÓöþ·Ö˼Ï룬ÿ´Î¶¼ÓëÇø¼äµÄÖмäÊý¾Ý±È¶Ô´óС£¬ËõС²éÕÒÇø¼äµÄ·¶Î§¡£ÎªÁ˸ü¼ÓÖ±¹Û£¬ÎÒ»ÁËÒ»ÕŲéÕÒ¹ý³ÌµÄͼ¡£ÆäÖУ¬low ºÍ high ±íʾ´ý²éÕÒÇø¼äµÄϱ꣬mid ±íʾ´ý²éÕÒÇø¼äµÄÖмäÔªËØϱꡣ
¶þ·Ö²éÕÒÕë¶ÔµÄÊÇÒ»¸öÓÐÐòµÄÊý¾Ý¼¯ºÏ£¬²éÕÒ˼ÏëÓеãÀàËÆ·ÖÖÎ˼Ïë¡£
ÿ´Î¶¼Í¨¹ý¸úÇø¼äµÄÖмäÔªËضԱȣ¬½«´ý²éÕÒµÄÇø¼äËõСΪ֮ǰµÄÒ»°ë£¬
Ö±µ½ÕÒµ½Òª²éÕÒµÄÔªËØ£¬»òÕßÇø¼ä±»ËõСΪ 0¡£
¿ÉÒÔ¿´³öÀ´£¬ÕâÊÇÒ»¸öµÈ±ÈÊýÁС£ÆäÖÐ n/2k=1 ʱ£¬k µÄÖµ¾ÍÊÇ×ܹ²ËõСµÄ´ÎÊý¡£
¶øÿһ´ÎËõС²Ù×÷Ö»Éæ¼°Á½¸öÊý¾ÝµÄ´óС±È½Ï£¬ËùÒÔ£¬¾¹ýÁË k ´ÎÇø¼äËõС²Ù×÷£¬
ʱ¼ä¸´ÔӶȾÍÊÇ O(k)¡£
ͨ¹ý n/2k=1£¬ÎÒÃÇ¿ÉÒÔÇóµÃ k=log2n£¬ËùÒÔʱ¼ä¸´ÔӶȾÍÊÇ O(logn)¡£
O(logn) ÕâÖÖ¶ÔÊýʱ¼ä¸´ÔӶȡ£ÕâÊÇÒ»ÖÖ¼«Æä¸ßЧµÄʱ¼ä¸´ÔӶȣ¬ÓеÄʱºòÉõÖÁ±Èʱ¼ä¸´ÔÓ¶ÈÊdz£Á¿¼¶ O(1) µÄËã·¨»¹Òª¸ßЧ¡£
ÒòΪ logn ÊÇÒ»¸ö·Ç³£¡°¿Ö²À¡±µÄÊýÁ¿¼¶£¬¼´±ã n ·Ç³£·Ç³£´ó£¬¶ÔÓ¦µÄ logn Ò²ºÜС¡£
ÒòΪ logn ÊÇÒ»¸ö·Ç³£¡°¿Ö²À¡±µÄÊýÁ¿¼¶£¬¼´±ã n ·Ç³£·Ç³£´ó£¬¶ÔÓ¦µÄ logn Ò²ºÜС¡£
±ÈÈç n µÈÓÚ 2 µÄ 32 ´Î·½£¬Õâ¸öÊýºÜ´óÁË°É£¿´óÔ¼ÊÇ 42 ÒÚ¡£
Ò²¾ÍÊÇ˵£¬Èç¹ûÎÒÃÇÔÚ 42 ÒÚ¸öÊý¾ÝÖÐÓöþ·Ö²éÕÒÒ»¸öÊý¾Ý£¬×î¶àÐèÒª±È½Ï 32 ´Î¡£
10 ÒڵĻ°,´ó¸ÅÊÇ 30 ´Î. ³ýÒÔ 2, Ê®¼¸´Î×óÓÒ°É.
¶þ·Ö²éÕҵĵݹéÓë·ÇµÝ¹éʵÏÖ
×î¼òµ¥µÄÇé¿ö¾ÍÊÇÓÐÐòÊý×éÖв»´æÔÚÖظ´ÔªËØ£¬ÎÒÃÇÔÚÆäÖÐÓöþ·Ö²éÕÒÖµµÈÓÚ¸ø¶¨ÖµµÄÊý¾Ý¡£
ÎÒÓôúÂëʵÏÖÁËÒ»¸ö×î¼òµ¥µÄ¶þ·Ö²éÕÒËã·¨¡£
int bsearch(int *a, int n, int value) {
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid] == value) {
return mid;
} else if (a[mid] < value) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
×ÅÖØÇ¿µ÷Ò»ÏÂÈÝÒ׳ö´íµÄ 3 ¸öµØ·½
1. Ñ»·Í˳öÌõ¼þ
×¢ÒâÊÇ low<=high£¬¶ø²»ÊÇ low<high¡£
2.mid µÄÈ¡Öµ
ʵ¼ÊÉÏ£¬mid=(low+high)/2 ÕâÖÖд·¨ÊÇÓÐÎÊÌâµÄ¡£ÒòΪÈç¹û low ºÍ high ±È½Ï´óµÄ»°£¬Á½ÕßÖ®ºÍ¾ÍÓпÉÄÜ»áÒç³ö¡£¸Ä½øµÄ·½·¨Êǽ« mid µÄ¼ÆË㷽ʽд³É low+(high-low)/2¡£¸ü½øÒ»²½£¬Èç¹ûÒª½«ÐÔÄÜÓÅ»¯µ½¼«ÖµĻ°£¬ÎÒÃÇ¿ÉÒÔ½«ÕâÀïµÄ³ýÒÔ 2 ²Ù×÷ת»¯³ÉλÔËËã low+((high-low)>>1)¡£ÒòΪÏà±È³ý·¨ÔËËãÀ´Ëµ£¬¼ÆËã»ú´¦ÀíλÔËËãÒª¿ìµÃ¶à¡£
3.low ºÍ high µÄ¸üÐÂ
low=mid+1£¬high=mid-1¡£×¢ÒâÕâÀïµÄ +1 ºÍ -1£¬Èç¹ûÖ±½Óд³É low=mid »òÕß high=mid£¬¾Í¿ÉÄܻᷢÉúËÀÑ»·¡£
±ÈÈ磬µ± high=3£¬low=3 ʱ£¬Èç¹û a[3] ²»µÈÓÚ value£¬¾Í»áµ¼ÖÂһֱѻ·²»Í˳ö¡£
¶þ·Ö²éÕÒÓ¦Óó¡¾°µÄ¾ÖÏÞÐÔ
Ê×ÏÈ£¬¶þ·Ö²éÕÒÒÀÀµµÄÊÇ˳Ðò±í½á¹¹£¬¼òµ¥µã˵¾ÍÊÇÊý×é¡£
Æä´Î£¬¶þ·Ö²éÕÒÕë¶ÔµÄÊÇÓÐÐòÊý¾Ý¡£
¶þ·Ö²éÕÒÖ»ÄÜÓÃÔÚ²åÈ롢ɾ³ý²Ù×÷²»Æµ·±£¬Ò»´ÎÅÅÐò¶à´Î²éÕҵij¡¾°ÖС£Õë¶Ô¶¯Ì¬±ä»¯µÄÊý¾Ý¼¯ºÏ£¬¶þ·Ö²éÕÒ½«²»ÔÙÊÊÓá£
ÔٴΣ¬Êý¾ÝÁ¿Ì«Ð¡²»Êʺ϶þ·Ö²éÕÒ¡£
×îºó£¬Êý¾ÝÁ¿Ì«´óÒ²²»Êʺ϶þ·Ö²éÕÒ¡£
¶þ·Ö²éÕҵĵײãÐèÒªÒÀÀµÊý×éÕâÖÖÊý¾Ý½á¹¹£¬¶øÊý×éΪÁËÖ§³ÖËæ»ú·ÃÎʵÄÌØÐÔ£¬ÒªÇóÄÚ´æ¿Õ¼äÁ¬Ðø£¬¶ÔÄÚ´æµÄÒªÇó±È½Ï¿Á¿Ì¡£
±ÈÈ磬ÎÒÃÇÓÐ 1GB ´óСµÄÊý¾Ý£¬Èç¹ûÏ£ÍûÓÃÊý×éÀ´´æ´¢£¬ÄǾÍÐèÒª 1GB µÄÁ¬ÐøÄÚ´æ¿Õ¼ä¡£
ÄÚÈÝС½á
½ñÌìÎÒÃÇѧϰÁËÒ»ÖÖÕë¶ÔÓÐÐòÊý¾ÝµÄ¸ßЧ²éÕÒËã·¨£¬¶þ·Ö²éÕÒ£¬ËüµÄʱ¼ä¸´ÔÓ¶ÈÊÇ O(logn)¡£
¶þ·Ö²éÕҵĺËÐÄ˼ÏëÀí½âÆðÀ´·Ç³£¼òµ¥£¬ÓеãÀàËÆ·ÖÖÎ˼Ïë¡£¼´Ã¿´Î¶¼Í¨¹ý¸úÇø¼äÖеÄÖмäÔªËضԱȣ¬½«´ý²éÕÒµÄÇø¼äËõСΪһ°ë£¬Ö±µ½ÕÒµ½Òª²éÕÒµÄÔªËØ£¬»òÕßÇø¼ä±»ËõСΪ 0¡£µ«ÊǶþ·Ö²éÕҵĴúÂëʵÏֱȽÏÈÝÒ×д´í¡£ÄãÐèÒª×ÅÖØÕÆÎÕËüµÄÈý¸öÈÝÒ׳ö´íµÄµØ·½£ºÑ»·Í˳öÌõ¼þ¡¢mid µÄÈ¡Öµ£¬low ºÍ high µÄ¸üС£
¶þ·Ö²éÕÒËäÈ»ÐÔÄܱȽÏÓÅÐ㣬µ«Ó¦Óó¡¾°Ò²±È½ÏÓÐÏÞ¡£µ×²ã±ØÐëÒÀÀµÊý×飬²¢ÇÒ»¹ÒªÇóÊý¾ÝÊÇÓÐÐòµÄ¡£¶ÔÓÚ½ÏС¹æÄ£µÄÊý¾Ý²éÕÒ£¬ÎÒÃÇÖ±½ÓʹÓÃ˳Ðò±éÀú¾Í¿ÉÒÔÁË£¬¶þ·Ö²éÕÒµÄÓÅÊƲ¢²»Ã÷ÏÔ¡£¶þ·Ö²éÕÒ¸üÊʺϴ¦Àí¾²Ì¬Êý¾Ý£¬Ò²¾ÍÊÇûÓÐƵ·±µÄÊý¾Ý²åÈ롢ɾ³ý²Ù×÷¡£
---------------------
À´Ô´£ºCSDN
ÔÎÄ£ºhttps://blog.csdn.net/zhanglong_4444/article/details/90449372