Go: // binary search

package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			a[i], _ = strconv.Atoi(scanner.Text())
		}
		scanner.Scan()
		k, _ := strconv.Atoi(scanner.Text())
		cnt := 0
		low, high := 0, n-1
		for low <= high {
			cnt++
			mid := (low+high)>>1
			if k == a[mid] {
				break
			}
			if k < a[mid] {
				high = mid-1
			} else {
				low = mid+1
			}
		}
		fmt.Println(cnt)
	}
}

C++: #include <cstdio>
using namespace std;

int main()
{
	int array[1000];
	int n;
	
	while (scanf("%d", &n), n){
		for (int i = 0; i < n; i++){
			scanf("%d", &array[i]);
		}
		
		int q;
		scanf("%d", &q);
		
		int l = 0;
		int r = n - 1;
		int cnt = 0;
		while (1){
			int m = (l + r) / 2;
			
			cnt++;
			
			if (q == array[m]){
				break;
			}
			
			else if (l == r){
				break;
			}
			
			else if (q < array[m]){
				r = m - 1;
			}
			
			else if (q > array[m]){
				l = m + 1;
			}
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

C++: #include<iostream>
using namespace std;

int main(){
    while(1){
        int n;
        cin >> n;

        if(n == 0)  break;

        int A[n];
        for(int i = 0; i < n; i++)  cin >> A[i];

        int x;
        cin >> x;
        int ans = 0;
        int r = n-1, l = 0;

        while(l != r){
            ans++;
            int mid = (r+l)/2;
            if(A[mid] == x) break;
            else if(A[mid] < x) l = mid+1;
            else if(A[mid] > x) r = mid-1;
        }

        cout << ans+1 << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n,n){
        int num[n],ans;
        for(int i=0;i<n;i++)cin>>num[i];
        cin>>ans;
        int serch,kawa[2]={0,n-1};
        for(int i=1;;i++){
            serch=(kawa[0]+kawa[1])/2;
            if(num[serch]==ans||kawa[0]==kawa[1]){
                cout<<i<<endl;
                break;
            }
            if(num[serch]>ans)kawa[1]=serch;
            else kawa[0]=serch+1;
        }
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
int n;
while(cin>>n,n){
  int a[n];
  for (int i=0;i<n;++i){
    cin>>a[i];
  }
  
  int goal;
  cin>>goal;
  int left,right;
  
  left=0;
  right=n;
  if(a[n/2]==goal){
    cout<<1<<endl;
    continue;
  }
  int sum=1;
  while(left<right){
    if( a[(left+right)/2]==goal ){
break;
}
    if(a[(left+right)/2]>goal){right=(left+right)/2-1;}
    if(a[(left+right)/2]<goal){left=(left+right)/2+1;}
    sum++;
  }
  if(left==right){
    cout<<sum<<endl;
   }
  
  
 }
 return 0;
}

C++: #include <iostream>
using namespace std;

int find( int* a, int s, int e, int d )
{
	int m = ( s + e ) / 2;
	int r = 0;
	if( a[ m ] == d || s == e ){
		return 1;
	}
	if( d > a[ m ] ){
		s = m + 1;
	}
	if( d < a[ m ] ){
		e = m - 1;
	}
	return 1 + find( a, s, e, d );
}

int main()
{
	int n;
	while( cin >> n && n ){
		int a[ 100 ] = {0};
		for( int i = 0; i < n; ++i ){
			cin >> a[ i ];
		}
		int d;
		cin >> d;
		cout << find( a, 0, n - 1, d ) << endl;
	}

	return 0;
}

C++: #include <iostream>

int binSearch(int* a, int n, int k)
{
	int res = 0;
	int lb = 0;
	int ub = n-1;
	while (lb <= ub) {
		int mid = (lb + ub) / 2;
		res++;
		if (a[mid] == k) return res;
		else if (a[mid] < k) lb = mid + 1;
		else ub = mid - 1;
	}
	return res;
}

int main()
{
	int n;
	while (true) {
		std::cin >> n;
		if (n == 0) break;
		int* a = new int[n];
		for (int i=0; i<n; i++) std::cin >> a[i];
		int k; std::cin >> k;
		std::cout << binSearch(a, n, k) << std::endl;
	}
	return 0;
}

C++: import java.util.Arrays;
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner scan =new Scanner(System.in);
		while(true){
		int n=scan.nextInt();
		if(n==0)break;
		int []a=new int[101];
		for(int i=0;i<n;i++){
			a[i]=scan.nextInt();
		}
		int m=scan.nextInt();
		int r=0,l=n-1,cnt=0;
		while(r<=l){
			++cnt;
			int h =(r+l)/2;
			if(a[h]==m)break;
			if(a[h]>m){
				r=h+1;
			}
			else{
				l=h;
			}
		}
		System.out.println(cnt);
	}
	}
}

C++: #include <iostream>
using namespace std;

int find( int* a, int b, int e, int t )
{
	if( b > e ){
		return 1;
	}
	int m = ( b + e ) / 2;
	if( t < a[ m ] ){
		return 1 + find( a, b, m - 1, t );
	}
	if( t > a[ m ] ){
		return 1 + find( a, m + 1, e, t );
	}
	return 1;
}


int main()
{
	int n;
	while( cin >> n && n ){
		int a[ 100 ] = {0};
		for( int i = 0; i < n; ++i ){
			cin >> a[ i ];
		}
		int d;
		cin >> d;
		cout << find( a, 0, n - 1, d ) << endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <cmath>
#define buf 1000001
using namespace std;
int cnt,b[buf]={0};
void binary_search(int d,int a);

int main(void){
  int i,j,a,n;
  char str[200];

  for( ; ; ){
    fgets(str,sizeof(str),stdin);
    sscanf(str,"%d",&a);
    if(a==0) break;
    for(i=0;i<a;i++){
      fgets(str,sizeof(str),stdin);
      sscanf(str,"%d",&n);
      b[i]=n;
    }
    cnt=0;
    fgets(str,sizeof(str),stdin);
    sscanf(str,"%d",&n);
    binary_search(n,a);
    printf("%d\n",cnt);
  }
  return 0;
}

void binary_search(int n,int a){
  int left,right,mid;
  double tmp;
  left = 0; right = a-1;
  while(left < right){
    tmp = floor((left + right)/2);
    mid=tmp;
    cnt++;
    if(b[mid] == n)
      break;
    else if(n < b[mid])
      right = mid;
    else if(n > b[mid])
      left = mid+1;
  }
}

C++: #include<iostream>
using namespace std;
int n,a[200],x;
int solve(){
	int l=0,r=n-1,mid,ans=0;
	while(l<=r){
		mid=(l+r)/2;
		if(a[mid]==x)
			return ans+1;
		else
		if(a[mid]<x)
		l=mid+1;
		else r=mid-1;
		ans++;

	}
	return ans;
}
int main(){
	while(cin>>n,n){
		for(int i=0;i<n;i++)
		cin>>a[i];
		cin>>x;
		cout<<solve()<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int SortedData[100];

int main() {
	int n, k, cnt, left, right, mid;

	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			cin >> SortedData[i];
		}
		cin >> k;

		left = 0;
		right = n - 1;
		cnt = 0;
		while (1) {
			++cnt;
			mid = (left + right) / 2;
			if (SortedData[mid] == k) {
				break;
			}
			if (SortedData[mid] < k) {
				left = mid + 1;
			}
			else if (SortedData[mid] > k) {
				right = mid - 1;
			}
			if (left > right) {
				break;
			}
		}
		cout << cnt << endl;
	}

	return 0;
}

C++: #include<iostream>

int search(int *n, int key, int p, int l, int count){
	int c = (p+l)/2;
	int cnt = count+1;
	if(n[c] == key || p>l)
		return count;
	else if(n[c] > key)
		return search(n, key, c+1, l, cnt);
	else
		return search(n, key, p, c-1, cnt);
}

int main(){
	int *data, n, key, count, p, l, c;
	while(true){
		std::cin >> n;
		if(n==0)
			return 0;
		data = new int[n];
		for(int i=0; i<n; i++)
			std::cin >> data[i];
		std::cin >> key;
		std::cout << search(data, key, 0, n-1, 0) << std::endl;
		delete[] data;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(void){
    int n;
    while(cin >> n, n){
        int data[10000];
        int need;
        for(int i=0; i < n; i++)
            cin >> data[i];
        cin >> need;

        int before = 0, after = n-1;
        int cnt = 1;
        while(before < after){
            int mid = (after + before) / 2;
            //cout << "mid 
            if(need == data[mid])
                break;
            cnt++;
            if(data[mid] > need)
                after = mid-1;
            else
                before = mid+1;
        }
        cout << cnt << endl;
    }
    return 0;
}