Refactored a few searching algorithms
Added documentation to these algorithms
This commit is contained in:
parent
cf778675df
commit
7d178c51b4
@ -1,71 +0,0 @@
|
|||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
class BinarySearch
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* This method implements the Generic Binary Search
|
|
||||||
*
|
|
||||||
* @param array The array to make the binary search
|
|
||||||
* @param key The number you are looking for
|
|
||||||
* @param lb The lower bound
|
|
||||||
* @param ub The upper bound
|
|
||||||
* @return the location of the key
|
|
||||||
**/
|
|
||||||
|
|
||||||
public static <T extends Comparable<T>> int BS(T array[], T key, int lb, int ub)
|
|
||||||
{
|
|
||||||
if ( lb > ub)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
int mid = (ub+lb) >>> 1;
|
|
||||||
int comp = key.compareTo(array[mid]);
|
|
||||||
|
|
||||||
if (comp < 0)
|
|
||||||
return (BS(array, key, lb, mid-1));
|
|
||||||
|
|
||||||
if (comp > 0)
|
|
||||||
return (BS(array, key, mid + 1, ub));
|
|
||||||
|
|
||||||
return mid;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Driver Program
|
|
||||||
public static void main(String[] args)
|
|
||||||
{
|
|
||||||
Scanner input=new Scanner(System.in);
|
|
||||||
|
|
||||||
// For INTEGER Input
|
|
||||||
Integer[] array = new Integer[10];
|
|
||||||
int key = 5;
|
|
||||||
|
|
||||||
for (int i = 0; i < 10 ; i++ )
|
|
||||||
array[i] = i+1;
|
|
||||||
|
|
||||||
int index = BS(array, key, 0, 9);
|
|
||||||
|
|
||||||
if (index != -1)
|
|
||||||
System.out.println("Number " + key + " found at index number : " + index);
|
|
||||||
else
|
|
||||||
System.out.println("Not found");
|
|
||||||
|
|
||||||
|
|
||||||
// For STRING Input
|
|
||||||
String[] array1 = {"a", "b", "c", "d", "e"};
|
|
||||||
String key1 = "d";
|
|
||||||
|
|
||||||
int index1 = BS(array1, key1, 0, array1.length-1);
|
|
||||||
|
|
||||||
if (index1 != -1)
|
|
||||||
System.out.println("String " + key1 + " found at index number : " + index1);
|
|
||||||
else
|
|
||||||
System.out.println("Not found");
|
|
||||||
|
|
||||||
input.close();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,77 +0,0 @@
|
|||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class LinearSearch{
|
|
||||||
/**
|
|
||||||
* The main method
|
|
||||||
* @param args Command line arguments
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
|
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
|
||||||
|
|
||||||
// Test for Integer inputs
|
|
||||||
Integer[] myArray;
|
|
||||||
int size = 0;
|
|
||||||
|
|
||||||
//Prompt user to create array and its elements
|
|
||||||
System.out.print("Enter the array size: ");
|
|
||||||
size = Integer.parseInt(br.readLine());
|
|
||||||
myArray = new Integer[size];
|
|
||||||
for (int i = 0; i < size; i++){
|
|
||||||
System.out.print("For index " + i + ", enter an integer: ");
|
|
||||||
myArray[i] = Integer.parseInt(br.readLine());
|
|
||||||
}
|
|
||||||
|
|
||||||
//Prompt user to search for particular element
|
|
||||||
System.out.print("Enter integer to search for: ");
|
|
||||||
Integer key = Integer.parseInt(br.readLine());
|
|
||||||
|
|
||||||
//Output array and index of target element, if found
|
|
||||||
System.out.printf("The integer %d is found in index %d\n", key, linearSearch(myArray, key));
|
|
||||||
|
|
||||||
// Test for String inputs
|
|
||||||
String[] myArray1;
|
|
||||||
int size1 = 0;
|
|
||||||
|
|
||||||
//Prompt user to create array and its elements
|
|
||||||
System.out.print("Enter the array size: ");
|
|
||||||
size1 = Integer.parseInt(br.readLine());
|
|
||||||
myArray1 = new String[size];
|
|
||||||
for (int i = 0; i < size1; i++){
|
|
||||||
System.out.print("For index " + i + ", enter a String: ");
|
|
||||||
myArray1[i] = br.readLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
//Prompt user to search for particular element
|
|
||||||
System.out.print("Enter String to search for: ");
|
|
||||||
String key1 = br.readLine();
|
|
||||||
|
|
||||||
//Output array and index of target element, if found
|
|
||||||
System.out.printf("The string %s is found in index %d\n", key1, linearSearch(myArray1, key1));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic Linear search method
|
|
||||||
*
|
|
||||||
* @param array List to be searched
|
|
||||||
* @param value Key being searched for
|
|
||||||
* @return Location of the key
|
|
||||||
*/
|
|
||||||
public static <T extends Comparable<T>> int linearSearch(T[] array, T value) {
|
|
||||||
int lo = 0;
|
|
||||||
int hi = array.length - 1;
|
|
||||||
for (int i = lo; i <= hi; i++) {
|
|
||||||
if (array[i].compareTo(value) == 0) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
87
Searches/src/search/BinarySearch.java
Normal file
87
Searches/src/search/BinarySearch.java
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
package search;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Binary search is one of the most popular algorithms
|
||||||
|
* The algorithm finds the position of a target value within a sorted array
|
||||||
|
*
|
||||||
|
* Worst-case performance O(log n)
|
||||||
|
* Best-case performance O(1)
|
||||||
|
* Average performance O(log n)
|
||||||
|
* Worst-case space complexity O(1)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||||
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||||
|
*
|
||||||
|
* @see SearchAlgorithm
|
||||||
|
* @see IterativeBinarySearch
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
class BinarySearch implements SearchAlgorithm {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param array is an array where the element should be found
|
||||||
|
* @param key is an element which should be found
|
||||||
|
* @param <T> is any comparable type
|
||||||
|
* @return index of the element
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <T extends Comparable<T>> int find(T array[], T key) {
|
||||||
|
return search(array, key, 0, array.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method implements the Generic Binary Search
|
||||||
|
*
|
||||||
|
* @param array The array to make the binary search
|
||||||
|
* @param key The number you are looking for
|
||||||
|
* @param left The lower bound
|
||||||
|
* @param right The upper bound
|
||||||
|
* @return the location of the key
|
||||||
|
**/
|
||||||
|
private <T extends Comparable<T>> int search(T array[], T key, int left, int right){
|
||||||
|
if (right < left) return -1; // this means that the key not found
|
||||||
|
|
||||||
|
// find median
|
||||||
|
int median = (left + right) >>> 1;
|
||||||
|
int comp = key.compareTo(array[median]);
|
||||||
|
|
||||||
|
if (comp < 0) {
|
||||||
|
return search(array, key, left, median - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (comp > 0) {
|
||||||
|
return search(array, key, median + 1, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
return median;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Driver Program
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
//just generate data
|
||||||
|
Random r = new Random();
|
||||||
|
int size = 200;
|
||||||
|
int maxElement = 100;
|
||||||
|
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[]::new);
|
||||||
|
|
||||||
|
|
||||||
|
//the element that should be found
|
||||||
|
Integer shouldBeFound = integers[r.nextInt(size - 1)];
|
||||||
|
|
||||||
|
BinarySearch search = new BinarySearch();
|
||||||
|
int atIndex = search.find(integers, shouldBeFound);
|
||||||
|
|
||||||
|
System.out.println(String.format("Should be found: %d. Found %d at index %d. An array length %d"
|
||||||
|
, shouldBeFound, integers[atIndex], atIndex, size));
|
||||||
|
}
|
||||||
|
}
|
@ -1,14 +1,28 @@
|
|||||||
|
package search;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Binary search is one of the most popular algorithms
|
||||||
|
* This class represents iterative version {@link BinarySearch}
|
||||||
|
* Iterative binary search is likely to have lower constant factors because it doesn't involve the overhead of manipulating the call stack.
|
||||||
|
* But in java the recursive version can be optimized by the compiler to this version.
|
||||||
|
*
|
||||||
|
* Worst-case performance O(log n)
|
||||||
|
* Best-case performance O(1)
|
||||||
|
* Average performance O(log n)
|
||||||
|
* Worst-case space complexity O(1)
|
||||||
*
|
*
|
||||||
* @author Gabriele La Greca : https://github.com/thegabriele97
|
* @author Gabriele La Greca : https://github.com/thegabriele97
|
||||||
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||||
|
*
|
||||||
|
* @see SearchAlgorithm
|
||||||
|
* @see BinarySearch
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public final class IterativeBinarySearch {
|
public final class IterativeBinarySearch implements SearchAlgorithm {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method implements an iterative version of binary search algorithm
|
* This method implements an iterative version of binary search algorithm
|
||||||
@ -18,13 +32,14 @@ public final class IterativeBinarySearch {
|
|||||||
*
|
*
|
||||||
* @return the index of key in the array or -1 if not found
|
* @return the index of key in the array or -1 if not found
|
||||||
*/
|
*/
|
||||||
public static <T extends Comparable<T>> int binarySearch(T[] array, T key) {
|
@Override
|
||||||
|
public <T extends Comparable<T>> int find(T[] array, T key) {
|
||||||
int l, r, k, cmp;
|
int l, r, k, cmp;
|
||||||
|
|
||||||
l = 0;
|
l = 0;
|
||||||
r = array.length - 1;
|
r = array.length;
|
||||||
|
|
||||||
while (l <= r) {
|
while (l < r) {
|
||||||
k = (l + r) / 2;
|
k = (l + r) / 2;
|
||||||
cmp = key.compareTo(array[k]);
|
cmp = key.compareTo(array[k]);
|
||||||
|
|
||||||
@ -32,7 +47,7 @@ public final class IterativeBinarySearch {
|
|||||||
return k;
|
return k;
|
||||||
} else if (cmp < 0) {
|
} else if (cmp < 0) {
|
||||||
r = --k;
|
r = --k;
|
||||||
} else if (cmp > 0) {
|
} else {
|
||||||
l = ++k;
|
l = ++k;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -53,7 +68,7 @@ public final class IterativeBinarySearch {
|
|||||||
//Arrays.sort(array); //if needed
|
//Arrays.sort(array); //if needed
|
||||||
Integer key = base + rand.nextInt(array.length * 2); //can generate keys that aren't in array
|
Integer key = base + rand.nextInt(array.length * 2); //can generate keys that aren't in array
|
||||||
|
|
||||||
System.out.println(binarySearch(array, key));
|
System.out.println(new IterativeBinarySearch().find(array, key));
|
||||||
System.out.println(Arrays.binarySearch(array, key));
|
System.out.println(Arrays.binarySearch(array, key));
|
||||||
}
|
}
|
||||||
}
|
}
|
63
Searches/src/search/LinearSearch.java
Normal file
63
Searches/src/search/LinearSearch.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package search;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Linear search is the easiest search algorithm
|
||||||
|
* It works with sorted and unsorted arrays (an binary search works only with sorted array)
|
||||||
|
* This algorithm just compares all elements of an array to find a value
|
||||||
|
*
|
||||||
|
* Worst-case performance O(n)
|
||||||
|
* Best-case performance O(1)
|
||||||
|
* Average performance O(n)
|
||||||
|
* Worst-case space complexity
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||||
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @see BinarySearch
|
||||||
|
* @see SearchAlgorithm
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class LinearSearch implements SearchAlgorithm {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic Linear search method
|
||||||
|
*
|
||||||
|
* @param array List to be searched
|
||||||
|
* @param value Key being searched for
|
||||||
|
* @return Location of the key
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <T extends Comparable<T>> int find(T[] array, T value) {
|
||||||
|
for (int i = 0; i < array.length ; i++) {
|
||||||
|
if (array[i].compareTo(value) == 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//just generate data
|
||||||
|
Random r = new Random();
|
||||||
|
int size = 200;
|
||||||
|
int maxElement = 100;
|
||||||
|
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).toArray(Integer[]::new);
|
||||||
|
|
||||||
|
|
||||||
|
//the element that should be found
|
||||||
|
Integer shouldBeFound = integers[r.nextInt(size - 1)];
|
||||||
|
|
||||||
|
LinearSearch search = new LinearSearch();
|
||||||
|
int atIndex = search.find(integers, shouldBeFound);
|
||||||
|
|
||||||
|
System.out.println(String.format("Should be found: %d. Found %d at index %d. An array length %d"
|
||||||
|
, shouldBeFound, integers[atIndex], atIndex, size));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
20
Searches/src/search/SearchAlgorithm.java
Normal file
20
Searches/src/search/SearchAlgorithm.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package search;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The common interface of most searching algorithms
|
||||||
|
*
|
||||||
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
public interface SearchAlgorithm {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param key is an element which should be found
|
||||||
|
* @param array is an array where the element should be found
|
||||||
|
* @param <T> Comparable type
|
||||||
|
* @return first found index of the element
|
||||||
|
*/
|
||||||
|
<T extends Comparable<T>> int find(T array[], T key);
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user