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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:35:18+00:00 2026-06-08T17:35:18+00:00

% java BinarySearch 1.txt < 2.txt If I have two text files (1.txt and

  • 0
% java BinarySearch 1.txt < 2.txt

If I have two text files (1.txt and 2.txt), where 2.txt contains values not in 1.txt, how does the binary search work in giving us these values? If the arguments to BinarySearch are a key and a sorted array, I don’t see how this applies.

Here is the code for the binary search:

import java.util.Arrays;

public class BinarySearch {

    // precondition: array a[] is sorted
    public static int rank(int key, int[] a) {
        int lo = 0;
        int hi = a.length - 1;
        while (lo <= hi) {
            // Key is in a[lo..hi] or not present.
            int mid = lo + (hi - lo) / 2;
            if      (key < a[mid]) hi = mid - 1;
            else if (key > a[mid]) lo = mid + 1;
            else return mid;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] whitelist = In.readInts(args[0]);

        Arrays.sort(whitelist);

        // read key; print if not in whitelist
        while (!StdIn.isEmpty()) {
            int key = StdIn.readInt();
            if (rank(key, whitelist) == -1)
                StdOut.println(key);
        }
    }
}

According to Wikipedia, and from what I’d understood: A binary search or half-interval search algorithm finds the position of a specified value (the input “key”) within a sorted array.

So how is it working to find uncommon values in two text files?

  • 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-08T17:35:19+00:00Added an answer on June 8, 2026 at 5:35 pm

    As I understand the question, you want to know how this program works when it (correctly) determines an entry in 2.txt is NOT in 1.txt. That has a pretty simple answer.

    This algorithm sorts the array whitelist. It initializes the lo pointer to point to element 0 and the hi pointer to point to element whitelist.length-1 which is the last elment in whitelist. The array segment is the whole array for the first iteration. The array must be ordered or sorted for this to work.

    For each successive iteration, if the value is not found in the middle of the current array segment, the logic determines whether the value has to be in the half-segment above the middle or the half-segment below the middle. That half-segment, excluding the old middle element, becomes the new search segment for the next iteration. The algorithm adjusts the hi and lo pointers to close in, one half of the remaining segment of the array at a time, on where the searched for value has to be, if it is in the array.

    Eventually, for a search value not in the array, hi and lo (and therefore mid) will converge to the same single element and it will be the last segment of the array searched, a segment of just one element. If that element doesn’t have the search value, then, depending on the search value and that element’s value either hi will become mid – 1 or lo will become mid + 1. Either way, the while continuation condition will become false because lo <= hi is no longer true. The new remaining search segment now has a negative size. This can be interpreted as meaning that if a return doesn’t occur before the while terminates, then the search didn’t find the value in any previous segment and there is no remaining segment to search. Therefore, the search value can’t be in the array.

    The implementation given in this question works. I’ve tested it usng the Princeton.edu stdlib that contains the In and StdIn classes used here. I’ve compiled and run it from a command line using stdin pipe to pipe in the second text file. I don’t think I would implement this application like this except as a demonstration of binary search methods, perhaps for a class or examining some techniques.

    Here is some further background on why a binary search is used. The reason to use a binary search is to obtain a worst case 2*logBase2(n) execution complexity with an average 1.5*logBase2(n) complexity. A binary search for a value not in the array will always be the worst case of 2*logBase2(n) compares.

    A binary search is vastly superior to a linear search that just starts at one end of the array and searches every elemnt until it finds a match or comes to the end of the array. The average search may be about n/2, depending on the distribution of values in the array. A linear search for a value not in the array will always have the worst case of n compares.

    In a binary search, each pair of compares eliminates half the possibilites. An array of 1024 entries can be searched in a maximum of 20 compares. Compare that to a 1024 maximum for a linear search. Squaring the size of the searched array only doubles the number of compares for a binary search. A binary search can search an array with 1,048,576 entries with a maximum of 40 compares. Compare that to a linear search maximum of 1,048,576.

    The basic binary search algorithm given in the question can be very useful with objects that inherit from a sorted or ordered collection and where you have to implement your own compare and search method to overload the inherited methods. As long as you have a compare that determines less, greater and equal amongst objects, and the collection is ordered or sorted according to that compare, you can use this basic binary search algorithm to search the collection.

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

Sidebar

Related Questions

I am trying to convert the following java binary search routine to as3. I
I am developing a binary search tree in java. But i am facing certain
I have rewritten the Binary Search Tree code found in the following links to
I have an array of java objects. Each object stores two longs that define
I am trying to implement binary search in Java: import java.util.Scanner; public class Search
Java script has many falsy values as I started learning. I have a program
using Java. the List is sorted so I want to use binary search. I'd
So recently I have been working on phonebook project that uses Binary Search Tree.
https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html Sun does not mention any complexity for their binary-search implemention. Is this a
I would like to test if two given BSTs (Binary Search Trees) are equal

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.