Five kinds of realization of dichotomy method

Source: Internet
Author: User

The implementation of the while

#include <iostream>using namespacestd;voidWhilee1 (int*a,intShangintXiaintNum//{cout<<"shang\tzhong\txia\t"<<Endl;  while(Shang >=xia)//If the number is large as above with Shang > Xia, the number is small as above with Xia<shang    {        intZhong = (shang + xia)/2; cout<< Shang <<"\ t"<< Zhong <<"\ t"<< Xia <<Endl; if(num = =A[zhong]) {cout<<"Whilee1 Find"<<Endl;  Break; }        Else if(a[zhong]<num) {Xia= Zhong +1; }        Else  //(Num<zhong){Shang= Zhong-1; }    }    if(Xia >Shang) {cout<<"While1 not find"<<Endl; }}voidWhilee2 (int*a,intXiaintShangintnum) {cout<<"shang\tzhong\txia\t"<<Endl;  while(Shang<=xia)//If the number is large as above with Shang >= Xia, the number is small as above with Xia<shang    {        intZhong = (shang + xia)/2; cout<< Shang <<"\ t"<< Zhong <<"\ t"<< Xia <<Endl; if(num = =A[zhong]) {cout<<"Whilee2 Find"<<Endl;  Break; }        Else if(a[zhong]<num) {Shang= Zhong +1; }        Else  //(Num<zhong){Xia= Zhong-1; }    }    if(shang>Xia) {cout<<"While2 not find"<<Endl; }}



The realization of Dowhile
#include <iostream>
Using namespace std;void dowhile1 (int *a,int shang,int xia,int num) {int zhong;cout << "shang\tzhong\txia\t" <&lt ; Endl;do{zhong = (shang + xia)/2;cout << shang<< "\ t" <<zhong<< "\ T" <<xia<< "\ T" <& Lt Endl;if (num = A[zhong]) {cout << "dowhile1 find" << endl;break;} else if (num > A[zhong]) {xia = zhong + 1;} else {Shang = zhong-1;}} while (Shang >= xia), if (Xia >shang) {cout << "Dowhile1 not FID" << Endl;}}  void Dowhile2 (int *a, int xia, int shang, int num) {int zhong;cout << "shang\tzhong\txia\t" << Endl;do{zhong = (Shang + xia)/2;cout << shang << "\ t" << zhong << "\ t" << xia << "\ t" << end L;if (num = A[zhong]) {cout << "dowhile2 find" << endl;break;} else if (num > A[zhong]) {shang = Zhong + 1;} else {xia = zhong-1;}} while (Xia>=shang), if (Shang>xia) {cout << "Dowhile2 not FID" << Endl;}}

Implementation of For

#include <iostream>using namespace std;void forr (int *a, int shang, int xia, int num) {int zhong;cout << "shang\ Tzhong\txia\t "<< endl;for (zhong = (shang + xia)/2; Shang >= xia;zhong= (Shang+xia)/2) {cout << shang<& lt; " \ t "<<zhong<<" \ T "<<xia<<" \ T "<< endl;if (Num==a[zhong]) {cout <<" for Find "< < Endl;break;} else if (num > A[zhong]) {xia = zhong + 1;} else {Shang = zhong-1;}} if (Xia > Shang) {cout << "for not find" << Endl;}}

The implementation of Goto

#include <iostream>using namespace std;void gotoo (int *a, int shang, int xia, int num) {if (Shang >= xia) {cout < < "shang\tzhong\txia\t" << Endl;  A:int Zhong = (shang + xia)/2;cout << shang << "\ t" << zhong << "\ t" << xia << "\ T" << endl;if (num = = A[zhong]) {cout << "goto find" << Endl;} else if (num > A[zhong]) {if (Shang > Xia) {xia = zhong + 1;goto A;} Goto B;} else/(num < A[zhong]) {if (Shang > Xia) {shang = Zhong-1;goto A;}}} if (shang<xia) {b:cout << "goto not find" << Endl;}}

Implementation of recursion

#include <iostream>using namespace std;void recurrence (int *a, int shang, int xia, int num) {if (Shang >=xia) {int Z  Hong = (shang + xia)/2;cout << shang << "\ t" << zhong << "\ t" << xia << endl;if (num = = A[zhong]) {cout << "recurrence find" << Endl;} else if (num > A[zhong]) {Recurrence (A, Shang, Zhong + 1, num);} else {recurrence (A, zhong-1, Xia, num);}} Else{cout << "Recurrence not find" << Endl;}}

  

Five kinds of realization of dichotomy method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.