Advertisement
Martichka

Array/ Task 11 - Binary Search

Jan 13th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using System;
  2. class BinarySearch
  3. {
  4.     static void Main()
  5.     {
  6.         int[] array = { 1, 3, 5, 12, 88, 4, 22, 16};
  7.         foreach (int num in array)
  8.         {
  9.             Console.Write(num + " ");
  10.         }
  11.         Console.WriteLine();
  12.         Console.WriteLine("Sorted Array");
  13.         Array.Sort(array);
  14.         foreach (int num in array)
  15.         {
  16.             Console.Write(num + " ");
  17.         }
  18.         Console.Write("Binary Search for(enter number): ");
  19.         int number = int.Parse(Console.ReadLine());
  20.         for (int i = 0; i < array.Length; i++)
  21.         {
  22.             if (number == array[i])
  23.             {
  24.                 Console.WriteLine("Your number is at position {0}", i);
  25.             }
  26.             else if (number != array[i] && (i == array.Length - 1))
  27.             {
  28.                 Console.WriteLine("There is no such number!");
  29.             }
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement