Binary Search

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //Sorting the elements of an array

            int[] array = { 10, 10, 12, 12, 20, 20, 25 };

            Console.WriteLine("Array before sorting: ");

            for (int i = 0; i < array.Length; i++)

            {

                Console.Write(array[i] + " ");

            }

            Console.WriteLine();

            for (int i = 1; i < array.Length; i++)

            {

                for (int j = 0; j < array.Length – i; j++)

                {

                    if (array[j] > array[j + 1])

                    {

                        int temp = array[j + 1];

                        array[j + 1] = array[j];

                        array[j] = temp;

                    }

                }

            }

            Console.WriteLine("Array after sorting: ");

            for (int i = 0; i < array.Length; i++)

            {

                Console.Write(array[i] + " ");

            }

            Console.WriteLine();

            Console.Write("Enter an element to search in the array: ");

            string str = Console.ReadLine();

            int searchElement = int.Parse(str);

            int start = 0; int end = array.Length – 1;

            int flag = 0;

            if (array.Length > 0)

            {

                if (searchElement < array[0])

                {

                    flag = -1;

                    start = end;

                }

                if (searchElement > array[array.Length – 1])

                {

                    flag = 1;

                    start = end;

                }

                while (start != end)

                {

                    int middle = (start + end) / 2;

                    if (searchElement < array[middle])

                    {

                        end = middle;

                    }

                    else if (searchElement > array[middle])

                    {

                        start = middle + 1;

                    }

                    else

                    {

                        start = middle;

                        end = middle;

                    }

                }

                if (searchElement == array[start])

                {

                    Console.WriteLine("Element Found.");

                }

                else

                {

                    Console.WriteLine("Element not present in the array");

                    if (flag == -1)

                    {

                        Console.WriteLine("Nearest match is " + array[0]);

                    }

                    else if (flag == 1)

                    {

                        Console.WriteLine("Nearest match is " + array[array.Length – 1]);

                    }

                    else

                    {

                        if (searchElement > array[end])

                        {

                            Console.WriteLine("Nearest match is between " + array[end] + " and " + array[end + 1]);

                        }

                        else

                        {

                            Console.WriteLine("Nearest match is between " + array[end – 1] + " and " + array[end]);

                        }

                    }

                }

            }

            else

            {

                Console.WriteLine("No elements to search from!!!");

            }

        }

    }

}
 

Array before sorting:

10 10 12 12 20 20 25

Array after sorting:

10 10 12 12 20 20 25

Enter an element to search in the array: 15

Element not present in the array

Nearest match is between 12 and 20

Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s