Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace Binary_Search
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<int> unsorted = new List<int>();
- Random generator = new Random();
- for (int i =0;i<100;i++)
- {
- unsorted.Add(i);
- }
- int number = generator.Next() % 200;
- Console.WriteLine("Search number is " + number);
- search(unsorted, number);
- }
- static void search(List<int> unsorted, int number)
- {
- double min = 0;
- double max = unsorted.Count()-1;
- int mid;
- while (max >min)
- {
- mid = Convert.ToInt16(Math.Ceiling((min + max)/2));
- if (unsorted[mid] == number)
- {
- Console.WriteLine("Number found at " + mid);
- Console.ReadLine();
- }
- else if (unsorted[mid] > number)
- {
- max = mid;
- }
- else if (unsorted[mid] < number)
- {
- min = mid;
- }
- }
- Console.WriteLine("Number not found");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment