c# - Binary search algorithm random generated array items not working

Dazzler

I have implemented binary search algorithm in a console window application in C#. I am generating random values to the array and sorting them using Random() and Array.Sort() functions respectively.

The Problem - No matter what Key(item to be searched in the array) I give, the program is returning Key not found when the array items are generated using Random function

This does not happen if I enter the array elements manually using Console.ReadLine().

TLDR: Binary Search algorithm works fine when array items are entered manually, but does not work when array items are generated using Random function.

Can anyone point out what is the mistake I am doing?

My code - Random Generated array items.

namespace BSA
{
  class Program
  {
    static void Main(string[] args)
    {
        var arr = new int[10];

        Random rnd = new Random();

        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = rnd.Next(1, 1000);
        }

        Array.Sort(arr);

        for (int i = 0; i < arr.Length; i++)
        {
            Console.Write("{0}\n", i);
        }

        while (true)
        {
            Console.WriteLine("Enter the number to be searched in the array.");

            var searchItem = Convert.ToInt32(Console.ReadLine());

            var foundPos = Search(arr, searchItem);

            if (foundPos > 0)
            {
                Console.WriteLine("Key {0} found at position {1}", searchItem, foundPos);
            }
            else
            {
                Console.WriteLine("Key {0} not found", searchItem);
            }
        }
    }

    public static int Search(int[] arr, int item)
    {
        var min = 0;
        var N = arr.Length;
        var max = N - 1;
        int basicOperations = 0;

        basicOperations++;
        do
        {
            var mid = (min + max)/2;

            if (arr[mid] == item)
                return mid;

            if (item < arr[mid])
                max = mid - 1; 
            else
                min = mid + 1;

            basicOperations++;
        } while (min <= max);

        return basicOperations;
    }
  }
}

Please let me know if I am doing any silly mistake or I am committing a blunder in the above code. Any help would be really helpful.

John D

Your search code works fine as far as I can see. However when you list the contents of the random array, you should write arr[i] rather than i to see what's in the array so you can pick a search value in it. Alternatively, pass arr[x] as the search item. It should return x.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

select a random link from generated search results in C#

From Dev

Recursive unsorted array search algorithm in C?

From Dev

Binary Search Tree Algorithm Sequence

From Dev

creating canvas lines with random/array generated co-ordinates. Not working, why?

From Dev

Binary search of strings in an array in C#

From Dev

Making use of binary search algorithm

From Dev

Binary search for sorted multiple items

From Dev

Randomized binary search algorithm

From Dev

Binary search not working - NON array.

From Dev

Seemingly working algorithm for storing a binary search tree in an array without wasting space

From Dev

Execution time for successful search Binary Search algorithm

From Dev

Binary Search Like Algorithm to Find Change in Value in Sorted Array

From Dev

Binary Search algorithm implementations

From Dev

Binary search in unsorted array

From Dev

Problem with implementing the binary search algorithm

From Dev

Binary search in an array

From Dev

Segmentation fault in recursive Binary Search Algorithm in C

From Dev

C++ binary search with char array

From Dev

Binary search the answer algorithm

From Dev

algorithm to search for an element in array

From Dev

Binary search of strings in an array in C#

From Dev

C++ Binary Search - Array Position Variables

From Dev

Does this mean binary search algorithm?

From Dev

Random math generation algorithm not working

From Dev

Binary search Algorithm - Python

From Dev

Binary Search Tree? Algorithm

From Dev

C++ Binary Search Not Working Correctly - Finds Element Not in Array

From Dev

Binary Search algorithm implementations

From Dev

Binary Search Algorithm is not working

Related Related

HotTag

Archive