Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:36:51+00:00 2026-06-18T08:36:51+00:00

My binary search is for finding a position for ordered insertion in a std::vector

  • 0

My binary search is for finding a position for ordered insertion in a std::vector. However, it seems to me that it is performing one (or more?) too many comparisons to find that position. Particularly the ternary at the end. Have I designed this wrong? To be clear, I haven’t noticed any errors in output.

template<typename T>
size_t find(std::vector<T> data, T value) //returns position value should be inserted at
{
 size_t start = 0;
 size_t end = data.size();

 if (!end) return 0;

 size_t diff;

 while (diff = (end - start) / 2)
 {
  size_t mid = diff + start;

  if (data[mid].value <= value)
  {
   start = mid;
  }
  else
  {
   end = mid;
  }
 }

 return data[start].value <= value ? end : start;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:36:52+00:00Added an answer on June 18, 2026 at 8:36 am

    In a binary search, you fundamentally have a three-way branch:
    >, < and =. There’s no way of writing this in C++ without
    two comparison operators, but I would expect any decent compiler
    to optimize them into a single machine instruction for the
    comparison, followed by two conditional branches on the results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary search function that will search for a word in an
I am having trouble finding the smallest element of a binary search tree. I
I have implemented a binary search tree and I want to add more functionality
I'm coding a binary search tree and I'm having a little trouble finding a
I was reading binary search tree and was thinking that why do we need
There are many algorithms for finding optimal binary search trees given a set of
I need a binary search algorithm that is compatible with the C++ STL containers,
using a binary search tree I need to add to a vector all int
I have a Binary Search Tree, I use template to add any class object
Given a binary search tree (BST). Find the maximum depth of the binary search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.