Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class BinarySearchAlgorithm
- {
- static int BinarySearch(int[] array, int value)
- {
- int low = 0, high = array.Length - 1, midpoint = 0;
- while (low <= high)
- {
- midpoint = low + (high - low) / 2;
- if (value == array[midpoint])
- {
- return midpoint;
- }
- else if (value < array[midpoint])
- high = midpoint - 1;
- else
- low = midpoint + 1;
- }
- return -1;
- }
- static void Main()
- {
- int[] array = { 1, 2, 3, 5, 6, 8, 1, 9, 0, 1, 4, 5, 6, 7, 8, 1, 1 };
- int n = 9;
- Array.Sort(array);
- Console.WriteLine(BinarySearch(array, n));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement