Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
Best way to calculate the midpoint of an array?

I've noticed that people use different ways to calculate the mid point of an ordered array and its sub arrays. This is often used in binary search.

The first method seems a bit better as it is simpler. Does the second way offer any advantages?

const mid = Math.round((left + right) / 2);

and

const mid = left + Math.round((right - left) / 2);

and ( per answer )

const mid =  ( left + right ) >>> 1;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First of all, I don't think that Math.round is what you will find more often, because it will round upwards any integer + 0.5. E.g., Math.round(3.5) === 4. This is not the most common way to find the integer midpoint. The main reason is that alternative ways involving integers (as opposed to floating point numbers), all round downwards:

  • (left + right) >>> 1 (>>> is the unsigned right shift)
  • ~~((left + right) * 0.5) (~~ is a way to cast to integer)
  • in other languages: integer division by 2 of left + right

These ways that involve integers are probably faster than calling Math.round(), check it on your browser. Furthermore, JavaScript is an exception among languages, in that it doesn't differentiate, except within operations, between integers and floats. Most languages will explicitly use integers for array indices, and so the right shift above is for them obviously the fastest way to divide by 2. This has also influenced the literature, so you will find ⌊(left+right)/2⌋ much more often than ⌈(left+right)/2⌉. If you insist on using the Math library, this means using Math.floor((left + right) / 2).

The only reason not to use integer operations could be that integers are signed 32 bit integers in JavaScript, while floats are 64 bit IEEE 754 floats, which have 53 significant bits (including an implied 1 bit). If your array indices can exceed 2147483637 (231-1), you cannot use integer operations. This situation is unlikely, though, also because the size of JavaScript arrays cannot exceed 4294967296 (232) anyway.

As for your question, the second form is not only a bit longer on screen, it also implies one more math operation. The second form would have an advantage over the first if indices could get close to Number.MAX_SAFE_INTEGER, which is 9007199254740991 (253-1) (because the subtraction would allow indices to be closer to that limit), but as we have seen this is not possible for JavaScript arrays.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!