Java String Binary Search Example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Here we are going to find a specific element in a string array using Binary Search Algorithm.
Searching a string using binary search algorithm is something tricky when compared to searching a number. Because we can compare 2 numerics directly, but in the case of strings it’s not as simple as number comparison.
Here we will see how we can do this in Binary Search.
Input: String[] a = { “AAA”, “BBB”, “CCC”, “DDD”, “EEE”, “FFF”, “GGG” };
Element to find: String key = “CCC”;
public class BinarySearchString {

	static String[] a = { "AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG" };
	static int min = 0;
	static int max = a.length - 1;
	static int mid;
	static String key = "CCC";

	public static void main(String[] args) {
		System.out.println("Key Found at : " + stringBinarySearch() + " position");
	}

	public static int stringBinarySearch() {
		while (min <= max) {
			mid = (min + max) / 2;
			if (a[mid].compareTo(key) < 0) { min = mid + 1; } else if (a[mid].compareTo(key) > 0) {
				max = mid - 1;
			} else {
				return mid;
			}
		}
		return -1;
	}
}

Output:
Key Found at : 2 position
Happy Learning 🙂

Decimal To Binary Conversion Java Program
Binary Search using Java
Comparable in Java
Python – How to Read Google Search Results in Selenium
How to add elements to ArrayList in Java
Selection Sort In Java
Convert any Number to Python Binary Number
Java Insertion Sort
How to Search a file in a Directory using Java
Binary Literals Java 7 Example Tutorials
Linear Search using Java
Java 13 Text Blocks – Formatting String in Java 13
String sorting in Java
String in Switch in Java 7 Example
How to Convert Java String to Int

Decimal To Binary Conversion Java Program
Binary Search using Java
Comparable in Java
Python – How to Read Google Search Results in Selenium
How to add elements to ArrayList in Java
Selection Sort In Java
Convert any Number to Python Binary Number
Java Insertion Sort
How to Search a file in a Directory using Java
Binary Literals Java 7 Example Tutorials
Linear Search using Java
Java 13 Text Blocks – Formatting String in Java 13
String sorting in Java
String in Switch in Java 7 Example
How to Convert Java String to Int
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String