Nortmar
Nortmar

Reputation: 15

Why isn't linear search taking more time than binary search?

When I run my program, I load a list of integers with random numbers, the max I have tried was 500000 numbers (it took a few minutes to load), and then I sort the list with listName.Sort();.

The user inputs a number an I have a 'Linear Search' method that detects if the list contains the number, if yes I make a for loop through every list number and try to find the input number (linear/brute force). And then I print the time difference in milliseconds between the input of the number and the result of the search. But I also have another method for a Binary Search, that does the same thing but instead of a brute-force it makes a binary search.

I tried both searches one at a time expecting the binary search to be way faster, but it turns out both are taking 1 or 2 milliseconds.. What am I doing wrong that it takes so few time?

static void Main(string[] args)
{
    List<int> arr = new List<int>();
    Random rnd = new Random();
    int randomNum;
    DateTime primeira = DateTime.Now;

//here i use 50000 numbers in the array
    for (var i = 0; i < 50000; i++)
    {
        do
        {
            //150000 options that don't repeat
            randomNum = rnd.Next(1, 150000);
        } while (arr.Contains(randomNum));
        arr.Add(randomNum);

    }
    arr.Sort();
    DateTime segunda = DateTime.Now;
    TimeSpan span = segunda - primeira;
    int ms = (int)span.TotalMilliseconds;
    Console.WriteLine($"Load time: {ms}");
    //I use this to know a number to search
    Console.WriteLine($"Biggest: {arr[arr.Count - 1]}"); 
    // search(arr);
    searchBrute(arr);  
}

public static void searchBrute(List<int> arr)
{
    Console.WriteLine("Insert a number for searching:");

    var input = Console.ReadLine();
    if (int.TryParse(input, out int number))
    {
        if (!arr.Contains(number))
        {
            Console.WriteLine("The number isn't in the array!");
            searchBrute(arr);
        }
        DateTime now = DateTime.Now;

        for (int i = 0; i < arr.Count; i++)
        {
            if(number == arr[i])
                showFound(number, i);
        }
        DateTime two = DateTime.Now;
        TimeSpan test = two - now;
        int msT = (int)test.TotalMilliseconds;
        Console.WriteLine($"Time taken: {msT}");
    }
}

public static void search(List<int> arr)
{
    bool found;
    int left = 0;
    int right = arr.Count - 1;
    Console.WriteLine("Insert a number for searching:");
    var input = Console.ReadLine();
    DateTime primeira = DateTime.Now;
    DateTime segunda;
    if (int.TryParse(input, out int number))
    {
        if (!arr.Contains(number))
        {
            Console.WriteLine("The number isn't in the array!");
            search(arr);
        }
        do
        {
            found = false;
            int mid = (left + right) / 2;
            if (number == arr[mid])
            {
                found = true;
                showFound(number, mid);

                segunda = DateTime.Now;
                TimeSpan span = segunda - primeira;
                int ms = (int)span.TotalMilliseconds;
                Console.WriteLine($"Time taken: {ms}");
            }
            else
            {
                if (arr[mid] > number)
                    right = mid - 1;
                else
                    left = mid + 1;

            }
        } while (!found);
    }
}

Upvotes: 1

Views: 114

Answers (1)

Adam Brown
Adam Brown

Reputation: 1729

So. As others have said, use System.Diagnostics.Stopwatch for this timing.

That isn't why the binary search is being slow. Both your search methods contain the line:

if (!arr.Contains(number))

Which is a linear search. This is probably making the binary search seem unreasonably slow, since it is also O(n) with a linear search inside.

Upvotes: 3

Related Questions