using System;
using System.Windows.Forms;

namespace _7_20
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void BinarySearch(int[] a, int x)
        {
            int low = 0;
            int high = (a.Length) - 1;
            int mid = 0;
            bool flag = false;
            
            while (low <= high && !flag)
            {
                mid = (low + high) / 2;
                if (a[mid] == x)
                    flag = true;
                else if (a[mid] < x)
                    low = mid + 1;
                else if ((a[mid] > x))
                    high = mid - 1;
            }

            if (flag == true)
                MessageBox.Show("the number " + x + " exist in array");
            else
                MessageBox.Show("the number " + x +
                                " not exist in array");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int[] a = { 9, 2, 3, 4, 5, 6, 1, 8, 7, 10 };
            Array.Sort(a);
            BinarySearch(a, 2);
        }
    }
}