二分查找也稱折半查找(Binary Search),它是一種效率較高的查找方法。但是,折半查找要求線性表必須采用順序存儲(chǔ)結(jié)構(gòu),而且表中元素按關(guān)鍵字有序排列。
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),常山企業(yè)網(wǎng)站建設(shè),常山品牌網(wǎng)站建設(shè),網(wǎng)站定制,常山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,常山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
通常在查找數(shù)組的時(shí)候,會(huì)發(fā)現(xiàn)一個(gè)一個(gè)查找很浪費(fèi)資源,因此通過二分查找法實(shí)現(xiàn) 。
算法要求:
1.必須采用順序存儲(chǔ)結(jié)構(gòu)。
2.必須按關(guān)鍵字大小有序排列。
首先,假設(shè)表中元素是按升序排列,將表中間位置記錄的關(guān)鍵字與查找關(guān)鍵字比較,如果兩者相等,則查找成功;否則利用中間位置記錄將表分成前、后兩個(gè)子表,如果中間位置記錄的關(guān)鍵字大于查找關(guān)鍵字,則進(jìn)一步查找前一子表,否則進(jìn)一步查找后一子表。重復(fù)以上過程,直到找到滿足條件的記錄,使查找成功,或直到子表不存在為止,此時(shí)查找不成功。
(上面是思維導(dǎo)圖)
#include
#include
//while、do while 、goto、for、遞歸
void digui(int shang,int xia,int zhong,int num)
{
zhong = (shang + xia) / 2;
if (shang <= xia)
{
if (num == zhong)
{
printf("遞歸:find.%d\n", zhong);
return;
}
else if (num < zhong)
{
xia = zhong - 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
else
{
shang = zhong + 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
}
return;
}
int main()
{
int a[1024];
for (int i = 0; i<1024; i++) {
a[i] = i;
//printf("%d\n",a[i]);
}
int shang = 0;
int xia = 1023;
int zhong = (shang + xia) / 2;
int num = 102;
while (shang <= xia)//一、while
{
zhong = (shang + xia) / 2;
if (num == zhong)
{
printf("while:find.%d\n", zhong);
break;
}
else if (num < zhong)
{
xia = zhong - 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
else
{
shang = zhong + 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
}
printf("==================\n");
printf("==================\n");
printf("==================\n");
shang = 0;
xia = 1023;
zhong = (shang + xia) / 2;
num = 102;
do {//二、do while
zhong = (shang + xia) / 2;
if (num == zhong)
{
printf("do while:find.%d\n", zhong);
break;
}
else if (num < zhong)
{
xia = zhong - 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
else
{
shang = zhong + 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
} while (xia>=shang);
printf("==================\n");
printf("==================\n");
printf("==================\n");
shang = 0;
xia = 1023;
zhong = (shang + xia) / 2;
num = 988;
AAA://三、goto
zhong = (xia + shang) / 2;
if (num == zhong)
{
printf("goto:find.%d\n", zhong);
goto BBB;
}
else if (num > zhong)
{
shang = zhong + 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
else
{
xia = zhong - 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
if (shang<=xia)
{
goto AAA;
}
BBB:
printf("==================\n");
printf("==================\n");
printf("==================\n");
shang = 0;
xia = 1023;
zhong = (shang + xia) / 2;
num = 288;
for (int i; shang <= xia ;zhong = (shang + xia) / 2)
{
if (num == zhong)
{
printf("for:find.%d\n", zhong);
break;
}
else if (num < zhong)
{
xia = zhong - 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
else
{
shang = zhong + 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
}
printf("==================\n");
printf("==================\n");
printf("==================\n");
shang = 0;
xia = 1023;
zhong = (shang + xia) / 2;
num = 288;
digui(shang,xia,zhong,num); //遞歸
getchar();
getchar();
Sleep(5000);
return 0;
}