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 1077363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:33:46+00:00 2026-05-16T21:33:46+00:00

In Programming Pearls (second edition) Column 5, problem 5 the question is about implementing

  • 0

In Programming Pearls (second edition) Column 5, problem 5 the question is about implementing binary search on an unsorted array.

How can you add partial checking to
the function at significantly less
than O(n-1) cost?

I know you can check with every iteration and achieve O(log n), but the hint in the back suggests there is an O(1) solution.

What is that solution?

  • 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-05-16T21:33:46+00:00Added an answer on May 16, 2026 at 9:33 pm

    Summary

    Partially checking if the array is sorted so that the binary search is applicable can be done in O(log n), as the OP said, and in O(1). The O(log n) method is to check each of the probes against the previous probe to make sure that it compares properly (less than, greater than). The O(1) method is to just check the final element found by binary search and one next to it so that if the sought-after element wasn’t found, at least the correct place for insertion was found. If the sought-after element was found, then that is a good O(1) partial check.

    Longer explanation

    The part of the problem before the code block says that a common problem is using binary search on an unsorted array. Basically, how can use partial checking to check if an array is sorted in less than O(n-1) cost?

    The O(log n) solution is to check each of the binary search probes meshes with respect to the previous probe (is less than or greater than it is expected to be). This doesn’t guarantee that the array is sorted, but it is a good partial check.

    The only O(1) check that I can think of is to check the final place that is searched such that its value and the neighboring values mesh with the idea of where the searched for element should be, even if the element wasn’t found. It’s a pretty good partial check: if the element is found then things are probably working correctly. If it wasn’t, then check the elements around where it should be that such that there is one less than the searched for element and then one that is greater than the searched for element. However, I do realize that checking in this manner means that the binary search, which is O(log n), has already been done so I don’t know if this is truly O(1). However, it only adds O(1) to the overall search so I think that it is applicable.

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

Sidebar

Related Questions

Programming Pearls in the column 2 (AHA! Algorithm) talks about how binary search aids
This is the problem described in Programming pearls . I can not understand binary
Here is a problem from Programming Pearls 2nd edition (Chapter 8.7): Considering a real
In Programming Pearls I have met the following problem. The question is this: print
i have question from programming pearls problem is following show how to use Lomuto's
Question from Programming Pearls, 2nd edition: Given a sequential file containing 4,300,000,000 32-bit integers,
From Programming Pearls : Column 12: A Sample Problem : The input consists of
I am trying to recreate the program in Column 15 of programming pearls using
From programming pearls, it is known that array[1...n] has heap property if for all
Page 120 of Programming Pearls 1st edition presents this algorithm for selecting M equally

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.