Skip to content

Commit

Permalink
[BAEL-19135] - Move search articles to a new module
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-burcea committed Nov 19, 2019
1 parent dd4aae6 commit 5e7d875
Show file tree
Hide file tree
Showing 31 changed files with 744 additions and 327 deletions.
2 changes: 0 additions & 2 deletions algorithms-miscellaneous-1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.

- [Validating Input With Finite Automata in Java](https://www.baeldung.com/java-finite-automata)
- [Example of Hill Climbing Algorithm](https://www.baeldung.com/java-hill-climbing-algorithm)
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](https://www.baeldung.com/java-monte-carlo-tree-search)
- [Binary Search Algorithm in Java](https://www.baeldung.com/java-binary-search)
- [Introduction to Minimax Algorithm](https://www.baeldung.com/java-minimax-algorithm)
- [How to Calculate Levenshtein Distance in Java?](https://www.baeldung.com/java-levenshtein-distance)
- [How to Find the Kth Largest Element in Java](https://www.baeldung.com/java-kth-largest-element)
Expand Down
2 changes: 0 additions & 2 deletions algorithms-miscellaneous-3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
- [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique)
- [Creating a Triangle with for Loops in Java](https://www.baeldung.com/java-print-triangle)
- [Efficient Word Frequency Calculator in Java](https://www.baeldung.com/java-word-frequency)
- [Interpolation Search in Java](https://www.baeldung.com/java-interpolation-search)
- [The K-Means Clustering Algorithm in Java](https://www.baeldung.com/java-k-means-clustering-algorithm)
- [Creating a Custom Annotation in Java](https://www.baeldung.com/java-custom-annotation)
- [Breadth-First Search Algorithm in Java](https://www.baeldung.com/java-breadth-first-search)
- More articles: [[<-- prev]](/algorithms-miscellaneous-2) [[next -->]](/algorithms-miscellaneous-4)
1 change: 0 additions & 1 deletion algorithms-miscellaneous-4/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
### Relevant articles:

- [Multi-Swarm Optimization Algorithm in Java](https://www.baeldung.com/java-multi-swarm-algorithm)
- [String Search Algorithms for Large Texts](https://www.baeldung.com/java-full-text-search-algorithms)
- [Check If a String Contains All The Letters of The Alphabet](https://www.baeldung.com/java-string-contains-all-letters)
- [Find the Middle Element of a Linked List](https://www.baeldung.com/java-linked-list-middle-element)
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
Expand Down

This file was deleted.

11 changes: 11 additions & 0 deletions algorithms-searching/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Algorithms - Searching

This module contains articles about searching algorithms.

### Relevant articles:
- [Binary Search Algorithm in Java](https://www.baeldung.com/java-binary-search)
- [Depth First Search in Java](https://www.baeldung.com/java-depth-first-search)
- [Interpolation Search in Java](https://www.baeldung.com/java-interpolation-search)
- [Breadth-First Search Algorithm in Java](https://www.baeldung.com/java-breadth-first-search)
- [String Search Algorithms for Large Texts](https://www.baeldung.com/java-full-text-search-algorithms)
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](https://www.baeldung.com/java-monte-carlo-tree-search)
37 changes: 37 additions & 0 deletions algorithms-searching/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>algorithms-searching</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>algorithms-searching</name>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${org.assertj.core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>algorithms-searching</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<properties>
<org.assertj.core.version>3.9.0</org.assertj.core.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
package com.baeldung.algorithms.binarysearch;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BinarySearch {

public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {

int index = Integer.MAX_VALUE;

while (low <= high) {

int mid = (low + high) / 2;

if (sortedArray[mid] < key) {
low = mid + 1;
} else if (sortedArray[mid] > key) {
high = mid - 1;
} else if (sortedArray[mid] == key) {
index = mid;
break;
}
}
return index;
}

public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {

int middle = (low + high) / 2;
if (high < low) {
return -1;
}

if (key == sortedArray[middle]) {
return middle;
} else if (key < sortedArray[middle]) {
return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
} else {
return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
}
}

public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
int index = Arrays.binarySearch(sortedArray, key);
return index;
}

public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
int index = Collections.binarySearch(sortedList, key);
return index;
}

}
package com.baeldung.algorithms.binarysearch;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BinarySearch {

public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {

int index = Integer.MAX_VALUE;

while (low <= high) {

int mid = (low + high) / 2;

if (sortedArray[mid] < key) {
low = mid + 1;
} else if (sortedArray[mid] > key) {
high = mid - 1;
} else if (sortedArray[mid] == key) {
index = mid;
break;
}
}
return index;
}

public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {

int middle = (low + high) / 2;
if (high < low) {
return -1;
}

if (key == sortedArray[middle]) {
return middle;
} else if (key < sortedArray[middle]) {
return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
} else {
return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
}
}

public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
int index = Arrays.binarySearch(sortedArray, key);
return index;
}

public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
int index = Collections.binarySearch(sortedList, key);
return index;
}

}
Loading

0 comments on commit 5e7d875

Please sign in to comment.