How to implement binary search for array of strings?

CommonCoreTawan

I did some research and wasn't able to find anything but if this is a duplicate please let me know.

I have this code for binary search of integers

package thepac;

public class CustomBS{

public static void main(String[] args){
    int[] listOfNumbers = new int[]{2,45,65,76,89,100,342,455};
    int numberToSearch = 100;
    int lowestIndex = 0;
    int highestIndex = listOfNumbers.length-1;
    int middleIndex;

    while(lowestIndex<=highestIndex){
        middleIndex = (lowestIndex+highestIndex)/2;

        if(numberToSearch > listOfNumbers[middleIndex]){
            lowestIndex = middleIndex+1;
        }else if(numberToSearch < listOfNumbers[middleIndex]){
            highestIndex = middleIndex-1;
        }else{
            break;
        }
    }//end of while
    if(lowestIndex > highestIndex){
        System.out.println("not found");
    }else{
        System.out.println("found");
    }
}
}

This works just find and I understand the concept. Is it possible to implement a version of this that searches for Strings as in an array of Strings?

I can't think of how to implement this since the current algorithm checks if the number being searched is higher or lower than the middle index, but if it's a String, how do I check if it's higher or lower than another string?

I know I can use the String.compareTo() method to check if a string is larger or shorter than another one but I'm not sure if this would be the correct way of implementing this.

Keiwan

If you have two strings s1 and s2 then

s1.compareTo(s2) 

returns a negative result if s1 is lexicographically smaller than s2, a positive result if s1 is lexicographically bigger than s2 and 0 if they are equal.

So you can write your function like this:

public static void main(String[] args){
    String[] listOfStrings = new String[]{"a","b","c","d","g","h","k","z"};
    String stringToFind = "d";
    int lowestIndex = 0;
    int highestIndex = listOfStrings.length-1;
    int middleIndex = 0;

    while(lowestIndex<=highestIndex){
        middleIndex = (lowestIndex+highestIndex)/2;

        if(stringToFind.compareTo(listOfStrings[middleIndex]) > 0){
            lowestIndex = middleIndex+1;
        }else if(stringToFind.compareTo(listOfStrings[middleIndex]) < 0){
            highestIndex = middleIndex - 1;
        }else{
            break;
        }
    }//end of while
    if(lowestIndex > highestIndex){
        System.out.println("not found");
    }else{
        System.out.println("found at " + middleIndex);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to implement binary search for array of strings?

From Dev

Python: how to implement binary search in Double Dimensional Array

From Dev

How to implement binary search in JavaScript

From Dev

How to implement binary search in JavaScript

From Dev

Binary search of strings in an array in C#

From Dev

Binary search of strings in an array in C#

From Dev

How to implement a Binary Search Tree in Julia?

From Dev

How to implement keySet method in Binary Search Tree

From Dev

How to search a string in an array of strings

From Dev

Binary search for strings

From Dev

Agda: How to infer proof of _≤_ (or, how to implement a binary search tree)

From Dev

How to write array of strings into binary file?

From Dev

How to implement add operation in Binary Search Tree in F#?

From Dev

How do I implement binary search using MySQL?

From Dev

Trying to implement binary search in haskell

From Dev

How to Modify a Suffix Array to search multiple strings?

From Dev

How to Modify a Suffix Array to search multiple strings?

From Dev

how to search a big array of names using binary search

From Dev

Search for a Word in an Array of strings & give its position using Binary Search Technique

From Dev

Binary search in unsorted array

From Dev

Binary search in an array

From Dev

How to search Javascript Array, and return all strings in array that start with X

From Dev

How to build a binary-array from hex strings

From Dev

How to implement a custom search-bar when the array type is JSON

From Dev

Why implement a Hashtable with a Binary Search Tree?

From Dev

Can binary search tree implement a Map?

From Dev

How to use Binary Search to find duplicates in sorted array?

From Dev

How can build a Binary Search Tree from an array of integers in C?

From Dev

How to sort an array of structs with std::binary_search or std::sort

Related Related

  1. 1

    How to implement binary search for array of strings?

  2. 2

    Python: how to implement binary search in Double Dimensional Array

  3. 3

    How to implement binary search in JavaScript

  4. 4

    How to implement binary search in JavaScript

  5. 5

    Binary search of strings in an array in C#

  6. 6

    Binary search of strings in an array in C#

  7. 7

    How to implement a Binary Search Tree in Julia?

  8. 8

    How to implement keySet method in Binary Search Tree

  9. 9

    How to search a string in an array of strings

  10. 10

    Binary search for strings

  11. 11

    Agda: How to infer proof of _≤_ (or, how to implement a binary search tree)

  12. 12

    How to write array of strings into binary file?

  13. 13

    How to implement add operation in Binary Search Tree in F#?

  14. 14

    How do I implement binary search using MySQL?

  15. 15

    Trying to implement binary search in haskell

  16. 16

    How to Modify a Suffix Array to search multiple strings?

  17. 17

    How to Modify a Suffix Array to search multiple strings?

  18. 18

    how to search a big array of names using binary search

  19. 19

    Search for a Word in an Array of strings & give its position using Binary Search Technique

  20. 20

    Binary search in unsorted array

  21. 21

    Binary search in an array

  22. 22

    How to search Javascript Array, and return all strings in array that start with X

  23. 23

    How to build a binary-array from hex strings

  24. 24

    How to implement a custom search-bar when the array type is JSON

  25. 25

    Why implement a Hashtable with a Binary Search Tree?

  26. 26

    Can binary search tree implement a Map?

  27. 27

    How to use Binary Search to find duplicates in sorted array?

  28. 28

    How can build a Binary Search Tree from an array of integers in C?

  29. 29

    How to sort an array of structs with std::binary_search or std::sort

HotTag

Archive