Advertisement
Layvu

Complex1

Nov 24th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     public static class NumberFinder
  6.     {
  7.         public static bool BinaryNumberSearch(this int[] numbers, int findNumber)
  8.         {
  9.             var left = 0;
  10.             var right = numbers.Length - 1;
  11.  
  12.             while (left < right)
  13.             {
  14.                 var middle = (left + right) / 2;
  15.                 if (findNumber <= numbers[middle])
  16.                     right = middle;
  17.                 else
  18.                     left = middle + 1;
  19.             }
  20.             if (numbers[right] == findNumber)
  21.                 return true;
  22.             return false;
  23.         }
  24.     }
  25.    
  26.     class Program
  27.     {
  28.         static void Main()
  29.         {
  30.             var leftBorder = int.Parse(Console.ReadLine());
  31.             var rightBorder = int.Parse(Console.ReadLine());
  32.             var findNumber = int.Parse(Console.ReadLine());
  33.  
  34.             int[] numbers = new int[] { 1, 5, 6, 3, 2, 2, -10};
  35.             int[] intervalNumbers = new int[rightBorder - leftBorder + 1];
  36.             Array.Copy(numbers, leftBorder, intervalNumbers, 0, intervalNumbers.Length);
  37.             Array.Sort(intervalNumbers);
  38.             Console.WriteLine(intervalNumbers.BinaryNumberSearch(findNumber));
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement