﻿ C# example- binary search algorithm

C# example- binary search algorithm

Binary search algorithm

This is C# example code for a binary search algorithm. If your are not sure about the process of binary search, i recommend you to follow this tutorial:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
int[] arr= new int[5]{23,2,3,34,6}; //unsorted data set
int pos;
pos=binsearch(arr,23,0,4);
if(pos!=-1)
Console.WriteLine("The target item was found at location:{0}",pos);
else
Console.WriteLine("The target item was not found in the list.\n");

}

///binary search
static int binsearch(int[] dataset,int target, int l,int u){
insertsort(dataset,dataset.Length);//make sure the list sorted
while(u>=l){

int mid=(l+u)/2;

if(target==dataset[mid]) return mid;

else if(target>dataset[mid]) l=mid+1;

else if(target<dataset[mid]) u=mid-1;

}
return -1;
}

static void insertsort(int[] dataset, int n)
{

int i, j;
for (i = 1; i < n; i++)
{
int pick_item = dataset[i];
int inserted = 0;
for (j = i - 1; j >= 0 && inserted != 1; )
{
if (pick_item < dataset[j])
{
dataset[j + 1] = dataset[j];
j--;
dataset[j + 1] = pick_item;
}
else inserted = 1;
}
}

}

}
}