Skip to main content
Changed the last BinarySearch() method to return an int.
Source Link

An alternative to using a StringBuilder is using LINQ with string.Join(). It's about as fast and is more concise and readable.

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var resultString = "";

    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var results = searchTerms.Select(x => BinarySearch(data, lowerBound, upperBound, x));
    return string.Join(" ", results);
}

If you want to parallelize the operation, you can just call AsParallel() and then AsOrdered() to preserve order:

var results = searchTerms
    .AsParallel()
    .AsOrdered()
    .Select(x => BinarySearch(data, lowerBound, upperBound, x));

If you want to cache the results, you can build a dictionary on the distinct elements in a similar fashion to the above LINQ query. AsOrdered() is not required because it's getting stored in a dictionary. The method will look like this:

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var cache = searchTerms
        .Distinct()
        .AsParallel()
        .ToDictionary(x => x, x => BinarySearch(data, lowerBound, upperBound, x));
    var results = searchTerms.Select(x => cache[x]);
    return string.Join(" ", results);
}

Edit: Regarding BinarySearch(), when I make recursive methods, I like to make a top level method with less parameters that calculates the extra parameters to pass into the recursive method. Like this:

public int BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    return BinarySearch(data, lowerBound, upperBound, key);
}

If you don't mind working with ints (which I think arrays are indexed with anyway), you can use Array.BinarySearch(). According to the documentation, a negative number is returned when an element is not found, and it's not necessarily -1, so we will have to handle that.

public longint BinarySearch(long[] data, long key)
{
    int result = Array.BinarySearch(data, key);
    return Math.Max(result, -1); 
}

An alternative to using a StringBuilder is using LINQ with string.Join(). It's about as fast and is more concise and readable.

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var resultString = "";

    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var results = searchTerms.Select(x => BinarySearch(data, lowerBound, upperBound, x));
    return string.Join(" ", results);
}

If you want to parallelize the operation, you can just call AsParallel() and then AsOrdered() to preserve order:

var results = searchTerms
    .AsParallel()
    .AsOrdered()
    .Select(x => BinarySearch(data, lowerBound, upperBound, x));

If you want to cache the results, you can build a dictionary on the distinct elements in a similar fashion to the above LINQ query. AsOrdered() is not required because it's getting stored in a dictionary. The method will look like this:

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var cache = searchTerms
        .Distinct()
        .AsParallel()
        .ToDictionary(x => x, x => BinarySearch(data, lowerBound, upperBound, x));
    var results = searchTerms.Select(x => cache[x]);
    return string.Join(" ", results);
}

Edit: Regarding BinarySearch(), when I make recursive methods, I like to make a top level method with less parameters that calculates the extra parameters to pass into the recursive method. Like this:

public int BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    return BinarySearch(data, lowerBound, upperBound, key);
}

If you don't mind working with ints (which I think arrays are indexed with anyway), you can use Array.BinarySearch(). According to the documentation, a negative number is returned when an element is not found, and it's not necessarily -1, so we will have to handle that.

public long BinarySearch(long[] data, long key)
{
    int result = Array.BinarySearch(data, key);
    return Math.Max(result, -1); 
}

An alternative to using a StringBuilder is using LINQ with string.Join(). It's about as fast and is more concise and readable.

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var resultString = "";

    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var results = searchTerms.Select(x => BinarySearch(data, lowerBound, upperBound, x));
    return string.Join(" ", results);
}

If you want to parallelize the operation, you can just call AsParallel() and then AsOrdered() to preserve order:

var results = searchTerms
    .AsParallel()
    .AsOrdered()
    .Select(x => BinarySearch(data, lowerBound, upperBound, x));

If you want to cache the results, you can build a dictionary on the distinct elements in a similar fashion to the above LINQ query. AsOrdered() is not required because it's getting stored in a dictionary. The method will look like this:

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var cache = searchTerms
        .Distinct()
        .AsParallel()
        .ToDictionary(x => x, x => BinarySearch(data, lowerBound, upperBound, x));
    var results = searchTerms.Select(x => cache[x]);
    return string.Join(" ", results);
}

Edit: Regarding BinarySearch(), when I make recursive methods, I like to make a top level method with less parameters that calculates the extra parameters to pass into the recursive method. Like this:

public int BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    return BinarySearch(data, lowerBound, upperBound, key);
}

If you don't mind working with ints (which I think arrays are indexed with anyway), you can use Array.BinarySearch(). According to the documentation, a negative number is returned when an element is not found, and it's not necessarily -1, so we will have to handle that.

public int BinarySearch(long[] data, long key)
{
    int result = Array.BinarySearch(data, key);
    return Math.Max(result, -1); 
}
removed unused variables and fixed wording
Source Link

An alternative to using a StringBuilder is using LINQ with string.Join(). It's about as fast and is more concise and readable.

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var resultString = "";

    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var results = searchTerms.Select(x => BinarySearch(data, lowerBound, upperBound, x));
    return string.Join(" ", results);
}

If you want to parallelize the operation, you can just call AsParallel() and then AsOrdered() to preserve order:

var results = searchTerms
    .AsParallel()
    .AsOrdered()
    .Select(x => BinarySearch(data, lowerBound, upperBound, x));

If you want to cache the results, you can build a dictionary on the distinct elements in a similar fashion to the above LINQ query. AsOrdered() is not required because it's getting stored in a dictionary. The method will look like this:

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var cache = searchTerms
        .Distinct()
        .AsParallel()
        .ToDictionary(x => x, x => BinarySearch(data, lowerBound, upperBound, x));
    var results = searchTerms.Select(x => cache[x]);
    return string.Join(" ", results);
}

Edit: Regarding BinarySearch(), when I make recursive methods, I like to make a top level method with less parameters that calculates the extra parameters to pass into the recursive method. Like this:

public int BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    return BinarySearch(data, lowerBound, upperBound, key);
}

If you don't mind working with ints (which I think arrays are indexed with anyway), you can use Array.BinarySearch(). According to the documentation, a negative number is returned withwhen an element is not found, and it's not necessarily -1, so we will have to handle that.

public long BinarySearch(long[] data, long key)
{
    int result = Array.BinarySearch(data, key);
    return Math.Max(result, -1); 
}

An alternative to using a StringBuilder is using LINQ with string.Join(). It's about as fast and is more concise and readable.

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var resultString = "";

    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var results = searchTerms.Select(x => BinarySearch(data, lowerBound, upperBound, x));
    return string.Join(" ", results);
}

If you want to parallelize the operation, you can just call AsParallel() and then AsOrdered() to preserve order:

var results = searchTerms
    .AsParallel()
    .AsOrdered()
    .Select(x => BinarySearch(data, lowerBound, upperBound, x));

If you want to cache the results, you can build a dictionary on the distinct elements in a similar fashion to the above LINQ query. AsOrdered() is not required because it's getting stored in a dictionary. The method will look like this:

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var cache = searchTerms
        .Distinct()
        .AsParallel()
        .ToDictionary(x => x, x => BinarySearch(data, lowerBound, upperBound, x));
    var results = searchTerms.Select(x => cache[x]);
    return string.Join(" ", results);
}

Edit: Regarding BinarySearch(), when I make recursive methods, I like to make a top level method with less parameters that calculates the extra parameters to pass into the recursive method. Like this:

public int BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    return BinarySearch(data, lowerBound, upperBound, key);
}

If you don't mind working with ints (which I think arrays are indexed with anyway), you can use Array.BinarySearch(). According to the documentation, a negative number is returned with an element is not found, and it's not necessarily -1, so we will have to handle that.

public long BinarySearch(long[] data, long key)
{
    int result = Array.BinarySearch(data, key);
    return Math.Max(result, -1); 
}

An alternative to using a StringBuilder is using LINQ with string.Join(). It's about as fast and is more concise and readable.

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var resultString = "";

    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var results = searchTerms.Select(x => BinarySearch(data, lowerBound, upperBound, x));
    return string.Join(" ", results);
}

If you want to parallelize the operation, you can just call AsParallel() and then AsOrdered() to preserve order:

var results = searchTerms
    .AsParallel()
    .AsOrdered()
    .Select(x => BinarySearch(data, lowerBound, upperBound, x));

If you want to cache the results, you can build a dictionary on the distinct elements in a similar fashion to the above LINQ query. AsOrdered() is not required because it's getting stored in a dictionary. The method will look like this:

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var cache = searchTerms
        .Distinct()
        .AsParallel()
        .ToDictionary(x => x, x => BinarySearch(data, lowerBound, upperBound, x));
    var results = searchTerms.Select(x => cache[x]);
    return string.Join(" ", results);
}

Edit: Regarding BinarySearch(), when I make recursive methods, I like to make a top level method with less parameters that calculates the extra parameters to pass into the recursive method. Like this:

public int BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    return BinarySearch(data, lowerBound, upperBound, key);
}

If you don't mind working with ints (which I think arrays are indexed with anyway), you can use Array.BinarySearch(). According to the documentation, a negative number is returned when an element is not found, and it's not necessarily -1, so we will have to handle that.

public long BinarySearch(long[] data, long key)
{
    int result = Array.BinarySearch(data, key);
    return Math.Max(result, -1); 
}

An alternative to using a StringBuilder is using LINQ with string.Join(). It's about as fast and is more concise and readable.

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var resultString = "";

    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var results = searchTerms.Select(x => BinarySearch(data, lowerBound, upperBound, x));
    return string.Join(" ", results);
}

If you want to parallelize the operation, you can just call AsParallel() and then AsOrdered() to preserve order:

var results = searchTerms
    .AsParallel()
    .AsOrdered()
    .Select(x => BinarySearch(data, lowerBound, upperBound, x));

If you want to cache the results, you can build a dictionary on the distinct elements in a similar fashion to the above LINQ query. AsOrdered() is not required because it's getting stored in a dictionary. The method will look like this:

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var cache = searchTerms
        .Distinct()
        .AsParallel()
        .ToDictionary(x => x, x => BinarySearch(data, lowerBound, upperBound, x));
    var results = searchTerms.Select(x => cache[x]);
    return string.Join(" ", results);
}

Edit: Regarding BinarySearch(), when I make recursive methods, I like to make a top level method with less parameters that calculates the extra parameters to pass into the recursive method. Like this:

public int BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    return BinarySearch(data, lowerBound, upperBound, key);
}

If you don't mind working with ints (which I think arrays are indexed with anyway), you can use Array.BinarySearch(). According to the documentation, a negative number is returned with an element is not found, and it's not necessarily -1, so we will have to handle that.

public long BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    int result = Array.BinarySearch(data, key);
    return Math.Max(result, -1); 
}

An alternative to using a StringBuilder is using LINQ with string.Join(). It's about as fast and is more concise and readable.

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var resultString = "";

    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var results = searchTerms.Select(x => BinarySearch(data, lowerBound, upperBound, x));
    return string.Join(" ", results);
}

If you want to parallelize the operation, you can just call AsParallel() and then AsOrdered() to preserve order:

var results = searchTerms
    .AsParallel()
    .AsOrdered()
    .Select(x => BinarySearch(data, lowerBound, upperBound, x));

If you want to cache the results, you can build a dictionary on the distinct elements in a similar fashion to the above LINQ query. AsOrdered() is not required because it's getting stored in a dictionary. The method will look like this:

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var cache = searchTerms
        .Distinct()
        .AsParallel()
        .ToDictionary(x => x, x => BinarySearch(data, lowerBound, upperBound, x));
    var results = searchTerms.Select(x => cache[x]);
    return string.Join(" ", results);
}

Edit: Regarding BinarySearch(), when I make recursive methods, I like to make a top level method with less parameters that calculates the extra parameters to pass into the recursive method. Like this:

public int BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    return BinarySearch(data, lowerBound, upperBound, key);
}

If you don't mind working with ints (which I think arrays are indexed with anyway), you can use Array.BinarySearch(). According to the documentation, a negative number is returned with an element is not found, and it's not necessarily -1, so we will have to handle that.

public long BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    int result = Array.BinarySearch(data, key);
    return Math.Max(result, -1); 
}

An alternative to using a StringBuilder is using LINQ with string.Join(). It's about as fast and is more concise and readable.

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var resultString = "";

    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var results = searchTerms.Select(x => BinarySearch(data, lowerBound, upperBound, x));
    return string.Join(" ", results);
}

If you want to parallelize the operation, you can just call AsParallel() and then AsOrdered() to preserve order:

var results = searchTerms
    .AsParallel()
    .AsOrdered()
    .Select(x => BinarySearch(data, lowerBound, upperBound, x));

If you want to cache the results, you can build a dictionary on the distinct elements in a similar fashion to the above LINQ query. AsOrdered() is not required because it's getting stored in a dictionary. The method will look like this:

public string BinarySearchSetup(long[] data, long[] searchTerms)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    var cache = searchTerms
        .Distinct()
        .AsParallel()
        .ToDictionary(x => x, x => BinarySearch(data, lowerBound, upperBound, x));
    var results = searchTerms.Select(x => cache[x]);
    return string.Join(" ", results);
}

Edit: Regarding BinarySearch(), when I make recursive methods, I like to make a top level method with less parameters that calculates the extra parameters to pass into the recursive method. Like this:

public int BinarySearch(long[] data, long key)
{
    var lowerBound = 0;
    var upperBound = data.Length - 1;
    return BinarySearch(data, lowerBound, upperBound, key);
}

If you don't mind working with ints (which I think arrays are indexed with anyway), you can use Array.BinarySearch(). According to the documentation, a negative number is returned with an element is not found, and it's not necessarily -1, so we will have to handle that.

public long BinarySearch(long[] data, long key)
{
    int result = Array.BinarySearch(data, key);
    return Math.Max(result, -1); 
}
Added improvements for BinarySearch()
Source Link
Loading
Got rid of Distinct() in the middle example to match the functionality of the original code.
Source Link
Loading
Source Link
Loading