You are on page 1of 2

using System;

class binary_search{

static void Main(){

int[] list = new int[] {1,2,3,4,5,6,7,8,9,10,20,30,40,50,80};

Binary(list, 20);

public static void Binary(Array list, int item)

//int[] array = n

//list.AddRange(1,2,3,4,5,6,7,8,9,10,20,30,40,50,80);

int low = 0;

int high = list.Length - 1;

while (low < high){

float calculo = (low+high) / 2;

int middle = (int)Math.Round(calculo);

if(list[middle] == item ){

Console.WriteLine("o Valor é: ",item, "a posicao é: ",


middle);

}else if(list[middle] > item){

high = high - 1;
}else {

low =low + 1;

Console.WriteLine("Valor não existe no array");

You might also like