A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Binary Search in C# page! Here, you'll find the source code for this program as well as a description of how the program works.
using System;
using System.Linq;
using System.Collections.Generic;
public class BinarySearch
{
public static bool Search(List<int> list, int toFind)
{
int lowerBound = 0;
int upperBound = list.Count - 1;
while (lowerBound <= upperBound)
{
int midpoint = (lowerBound + upperBound) / 2;
if (list[midpoint] == toFind)
{
return true;
}
else if (list[midpoint] < toFind)
{
lowerBound = midpoint + 1;
}
else
{
upperBound = midpoint - 1;
}
}
return false;
}
public static void ErrorAndExit()
{
Console.WriteLine("Usage: please provide a list of sorted integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")");
Environment.Exit(1);
}
public static void Main(string[] args)
{
try
{
var list = args[0].Split(',').Select(i => Int32.Parse(i.Trim())).ToList();
var toFind = Int32.Parse(args[1]);
for (int i = 0; i < list.Count - 1; i++)
{
if (list[i] > list[i + 1])
{
ErrorAndExit();
}
}
Console.WriteLine(Search(list, toFind));
}
catch
{
ErrorAndExit();
}
}
}
Binary Search in C# was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.