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

  • SEARCH
  • Home
  • 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 9265393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:03:41+00:00 2026-06-18T14:03:41+00:00

I have been trying to implement a binary search on a sorted array of

  • 0

I have been trying to implement a binary search on a sorted array of data with an application for cubic spline interpolation. I am unable to get it to function on all the range. For instance, search number 4.4 should return the lower bound 4.35 of range [4.35-4.57). 4.57 lives in both [4.35-4.57] and [4.57, 4.76) etc.

Sub Binary_Search_of_Array()
Dim SplineRanges() As Variant
Dim Right As Integer
Dim Middle As Integer
Dim Left As Integer
Dim SearchNumber As Variant


SplineRanges = Array(4, 4.35, 4.57, 4.76, 5.26, 5.88)
SearchNumber = 4.4


Right = UBound(SplineRanges)
Left = LBound(SplineRanges)
Do While Left < Right
  Middle = Left + (Right - Left) / 2

  If SplineRanges(Middle) < SearchNumber Then
     Left = Middle + 1
  ElseIf SplineRanges(Middle) > SearchNumber Then
     Right = Middle - 1
  Else
    Left = Middle
    Exit Do
  End If
Loop


  Debug.Print SplineRanges(Left - 1); SearchNumber; SplineRanges(Left) ' Tried many different statements to return the correct bounds.



End Sub
  • 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-18T14:03:42+00:00Added an answer on June 18, 2026 at 2:03 pm

    I was too quick pointing out (only) one thing that was making your code have trouble. I took another look at the over all logic, and came up with the following snippet – which is easier to follow, and correct:

    Do While Right - Left > 1
      Middle = Left + (Right - Left) / 2
    
      If SplineRanges(Middle) < SearchNumber Then
        Left = Middle
      Else
        Right = Middle
      End If
    Loop
    
    Debug.Print SplineRanges(Left); SearchNumber; SplineRanges(Right)
    

    I ran this over the full range of values from 4.01 to 5.88 in steps of 0.01, and it seems to behave itself throughout. The logic is a bit easier to grasp: if your midpoint value is too high, then you adjust your lower boundary to that point; otherwise you adjust your upper boundary. Once the upper and lower boundaries are only one apart, your target is “in the bracket”. Note that floating point types don’t really every reach equality, except for small integer values. So the point that you made about “4.57 lives in both [4.35-4.57] and [4.57-4.76]” is tricky. At any rate, when you are making splines the approximation doesn’t break down when you are “just the wrong side of a point” since splines have all those lovely continuous derivatives…

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

Sidebar

Related Questions

I have been trying to implement Cascading in my MVC application. Seems like everything
I've been trying to implement a binary search tree in C++ for fun. My
I have been trying to implement the camera into my application following this tutorial
I have been trying to implement Bubble Sort using simple integer array in java.
I have been trying to implement a BackgroundWorker into my application, and so far,
I have been trying to implement a search page for the admin of my
I have been trying to implement proximity search using Lucene 3.6 . I went
Good Evening, I have been trying to implement a simple Android(tm) application that utilizes
Alright, so I've been trying to implement a simple binary search tree that uses
I have been trying to implement a sample application in order to study the

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.