Skip to main content
Spelling fix
Source Link
Toby Speight
  • 81.8k
  • 14
  • 101
  • 309

Could you adviceadvise on this binary search implementation?

template<typename Iterator, typename T>
Iterator binarySearch(Iterator begin, Iterator end, const T& v) {
    Iterator low = begin;
    Iterator high = end - 1;
    while (low <= high)
    {
        Iterator mid = low + (high - low) / 2;
        if (*mid == v) {
            return mid;
        }
        else if (*mid < v) {
            low = mid + 1;
        } else {
            if (mid == begin) { 
                return end;
            }
            high = mid-1;
        }
    }
    return end;
}

The mid==begin line is unfortunate but cannot think of a more elegant approach here, welcoming ideas.

Could you advice on this binary search implementation?

template<typename Iterator, typename T>
Iterator binarySearch(Iterator begin, Iterator end, const T& v) {
    Iterator low = begin;
    Iterator high = end - 1;
    while (low <= high)
    {
        Iterator mid = low + (high - low) / 2;
        if (*mid == v) {
            return mid;
        }
        else if (*mid < v) {
            low = mid + 1;
        } else {
            if (mid == begin) { 
                return end;
            }
            high = mid-1;
        }
    }
    return end;
}

The mid==begin line is unfortunate but cannot think of a more elegant approach here, welcoming ideas.

Could you advise on this binary search implementation?

template<typename Iterator, typename T>
Iterator binarySearch(Iterator begin, Iterator end, const T& v) {
    Iterator low = begin;
    Iterator high = end - 1;
    while (low <= high)
    {
        Iterator mid = low + (high - low) / 2;
        if (*mid == v) {
            return mid;
        }
        else if (*mid < v) {
            low = mid + 1;
        } else {
            if (mid == begin) { 
                return end;
            }
            high = mid-1;
        }
    }
    return end;
}

The mid==begin line is unfortunate but cannot think of a more elegant approach here, welcoming ideas.

Became Hot Network Question
added 3 characters in body; edited tags
Source Link
Cris Luengo
  • 6.2k
  • 1
  • 13
  • 36

Could you advice on this binarySearchbinary search implementation:?

template<typename Iterator, typename T>
Iterator binarySearch(Iterator begin, Iterator end, const T& v) {
    Iterator low = begin;
    Iterator high = end - 1;
    while (low <= high)
    {
        Iterator mid = low + (high - low) / 2;
        if (*mid == v) {
            return mid;
        }
        else if (*mid < v) {
            low = mid + 1;
        } else {
            if (mid == begin) { 
                return end;
            }
            high = mid-1;
        }
    }
    return end;
}

the mid==beginThe mid==begin line is unfortunate but cannot think of a more elegant approach here, welcoming ideas.

Could you advice on this binarySearch implementation:

template<typename Iterator, typename T>
Iterator binarySearch(Iterator begin, Iterator end, const T& v) {
    Iterator low = begin;
    Iterator high = end - 1;
    while (low <= high)
    {
        Iterator mid = low + (high - low) / 2;
        if (*mid == v) {
            return mid;
        }
        else if (*mid < v) {
            low = mid + 1;
        } else {
            if (mid == begin) { 
                return end;
            }
            high = mid-1;
        }
    }
    return end;
}

the mid==begin line is unfortunate but cannot think of a more elegant approach here, welcoming ideas.

Could you advice on this binary search implementation?

template<typename Iterator, typename T>
Iterator binarySearch(Iterator begin, Iterator end, const T& v) {
    Iterator low = begin;
    Iterator high = end - 1;
    while (low <= high)
    {
        Iterator mid = low + (high - low) / 2;
        if (*mid == v) {
            return mid;
        }
        else if (*mid < v) {
            low = mid + 1;
        } else {
            if (mid == begin) { 
                return end;
            }
            high = mid-1;
        }
    }
    return end;
}

The mid==begin line is unfortunate but cannot think of a more elegant approach here, welcoming ideas.

Source Link
acd
  • 107
  • 4

C++ Binary Search

Could you advice on this binarySearch implementation:

template<typename Iterator, typename T>
Iterator binarySearch(Iterator begin, Iterator end, const T& v) {
    Iterator low = begin;
    Iterator high = end - 1;
    while (low <= high)
    {
        Iterator mid = low + (high - low) / 2;
        if (*mid == v) {
            return mid;
        }
        else if (*mid < v) {
            low = mid + 1;
        } else {
            if (mid == begin) { 
                return end;
            }
            high = mid-1;
        }
    }
    return end;
}

the mid==begin line is unfortunate but cannot think of a more elegant approach here, welcoming ideas.