docs: update the whole repository

* fix some bugs
* delete duplicate files
* format code
This commit is contained in:
yanglbme
2019-05-09 19:32:54 +08:00
parent 163db8521a
commit 29948363da
368 changed files with 4372 additions and 30841 deletions

View File

@ -2,7 +2,7 @@ package Searches;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
import static java.lang.String.format;
@ -38,7 +38,7 @@ class BinarySearch implements SearchAlgorithm {
* @return index of the element
*/
@Override
public <T extends Comparable<T>> int find(T array[], T key) {
public <T extends Comparable<T>> int find(T[] array, T key) {
return search(array, key, 0, array.length);
}
@ -77,7 +77,8 @@ class BinarySearch implements SearchAlgorithm {
int size = 100;
int maxElement = 100000;
int[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray();
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[]::new);
// The element that should be found
int shouldBeFound = integers[r.nextInt(size - 1)];

View File

@ -7,73 +7,69 @@ import java.util.stream.IntStream;
import static java.lang.String.format;
/**
*
* Interpolation search algorithm implementation
*
* <p>
* Worst-case performance O(n)
* Best-case performance O(1)
* Average performance O(log(log(n))) if the elements are uniformly distributed if not O(n)
* Worst-case space complexity O(1)
*
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
*/
class InterpolationSearch {
/**
* @param array is a sorted array
* @param key is a value what shoulb be found in the array
* @return an index if the array contains the key unless -1
*/
public int find(int array[], int key) {
// Find indexes of two corners
int start = 0, end = (array.length - 1);
// Since array is sorted, an element present
// in array must be in range defined by corner
while (start <= end && key >= array[start] && key <= array[end])
{
// Probing the position with keeping
// uniform distribution in mind.
int pos = start + (((end-start) / (array[end]-array[start]))*(key - array[start]));
// Condition of target found
if (array[pos] == key)
return pos;
// If key is larger, key is in upper part
if (array[pos] < key)
start = pos + 1;
// If key is smaller, x is in lower part
else
end = pos - 1;
}
return -1;
}
/**
* @param array is a sorted array
* @param key is a value what shoulb be found in the array
* @return an index if the array contains the key unless -1
*/
public int find(int array[], int key) {
// Find indexes of two corners
int start = 0, end = (array.length - 1);
// Driver method
public static void main(String[] args) {
Random r = new Random();
int size = 100;
int maxElement = 100000;
int[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray();
// Since array is sorted, an element present
// in array must be in range defined by corner
while (start <= end && key >= array[start] && key <= array[end]) {
// Probing the position with keeping
// uniform distribution in mind.
int pos = start + (((end - start) / (array[end] - array[start])) * (key - array[start]));
// Condition of target found
if (array[pos] == key)
return pos;
// If key is larger, key is in upper part
if (array[pos] < key)
start = pos + 1;
// If key is smaller, x is in lower part
else
end = pos - 1;
}
return -1;
}
// Driver method
public static void main(String[] args) {
Random r = new Random();
int size = 100;
int maxElement = 100000;
int[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray();
//the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];
//the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];
InterpolationSearch search = new InterpolationSearch();
int atIndex = search.find(integers, shouldBeFound);
InterpolationSearch search = new InterpolationSearch();
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));
System.out.println(String.format("Should be found: %d. Found %d at index %d. An array length %d"
, shouldBeFound, integers[atIndex], atIndex, size));
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.println(format("Found by system method at an index: %d. Is equal: %b", toCheck, toCheck == atIndex));
}
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.println(format("Found by system method at an index: %d. Is equal: %b", toCheck, toCheck == atIndex));
}
}

View File

@ -11,7 +11,7 @@ import static java.lang.String.format;
* 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.
*
* <p>
* Worst-case performance O(log n)
* Best-case performance O(1)
* Average performance O(log n)
@ -19,24 +19,21 @@ import static java.lang.String.format;
*
* @author Gabriele La Greca : https://github.com/thegabriele97
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
* @see SearchAlgorithm
* @see BinarySearch
*
*/
public final class IterativeBinarySearch implements SearchAlgorithm {
/**
* This method implements an iterative version of binary search algorithm
*
*
* @param array a sorted array
* @param key the key to search in array
*
* @param key the key to search in array
* @return the index of key in the array or -1 if not found
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T key) {
public <T extends Comparable<T>> int find(T[] array, T key) {
int l, r, k, cmp;
l = 0;

View File

@ -7,24 +7,20 @@ import java.util.stream.Stream;
import static java.lang.String.format;
/**
*
* A iterative version of a ternary search algorithm
* This is better way to implement the ternary search, because a recursive version adds some overhead to a stack.
* But in java the compile can transform the recursive version to iterative implicitly,
* so there are no much differences between these two algorithms
*
* <p>
* Worst-case performance Θ(log3(N))
* Best-case performance O(1)
* Average performance Θ(log3(N))
* Worst-case space complexity O(1)
*
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
* @since 2018-04-13
*
* @see SearchAlgorithm
* @see TernarySearch
*
* @since 2018-04-13
*/
public class IterativeTernarySearch implements SearchAlgorithm {
@ -35,10 +31,10 @@ public class IterativeTernarySearch implements SearchAlgorithm {
int left = 0;
int right = array.length - 1;
while (right > left) {
while (right > left) {
int leftCmp = array[left].compareTo(key);
int rightCmp = array[right].compareTo(key);
int leftCmp = array[left].compareTo(key);
int rightCmp = array[right].compareTo(key);
if (leftCmp == 0) return left;
if (rightCmp == 0) return right;

View File

@ -7,17 +7,14 @@ 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
*
* <p>
* 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
*/
@ -33,7 +30,7 @@ public class LinearSearch implements SearchAlgorithm {
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T value) {
for (int i = 0; i < array.length ; i++) {
for (int i = 0; i < array.length; i++) {
if (array[i].compareTo(value) == 0) {
return i;
}

View File

@ -27,10 +27,10 @@ public class SaddlebackSearch {
/**
* This method performs Saddleback Search
*
* @param arr The **Sorted** array in which we will search the element.
* @param arr The **Sorted** array in which we will search the element.
* @param row the current row.
* @param col the current column.
* @param key the element that we want to search for.
* @param key the element that we want to search for.
* @return The index(row and column) of the element if found.
* Else returns -1 -1.
*/

View File

@ -1,18 +1,16 @@
package Searches;
/**
* The common interface of most searching algorithms
* 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 key is an element which should be found
* @param array is an array where the element should be found
* @param <T> Comparable type
* @param <T> Comparable type
* @return first found index of the element
*/
<T extends Comparable<T>> int find(T array[], T key);

View File

@ -8,49 +8,43 @@ import java.util.stream.Stream;
import static java.lang.String.format;
/**
*
*
*
* A ternary search algorithm is a technique in computer science for finding the minimum or maximum of a unimodal function
* The algorithm determines either that the minimum or maximum cannot be in the first third of the domain
* or that it cannot be in the last third of the domain, then repeats on the remaining third.
*
* <p>
* Worst-case performance Θ(log3(N))
* Best-case performance O(1)
* Average performance Θ(log3(N))
* Worst-case space complexity O(1)
*
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
* @see SearchAlgorithm
* @see IterativeBinarySearch
*
*/
public class TernarySearch implements SearchAlgorithm{
public class TernarySearch implements SearchAlgorithm {
/**
* @param arr The **Sorted** array in which we will search the element.
* @param arr The **Sorted** array in which we will search the element.
* @param value The value that we want to search for.
* @return The index of the element if found.
* Else returns -1.
*/
@Override
public <T extends Comparable<T>> int find(T[] arr, T value){
public <T extends Comparable<T>> int find(T[] arr, T value) {
return ternarySearch(arr, value, 0, arr.length - 1);
}
/**
* @param arr The **Sorted** array in which we will search the element.
* @param key The value that we want to search for.
* @param arr The **Sorted** array in which we will search the element.
* @param key The value that we want to search for.
* @param start The starting index from which we will start Searching.
* @param end The ending index till which we will Search.
* @param end The ending index till which we will Search.
* @return Returns the index of the Element if found.
* Else returns -1.
*/
private <T extends Comparable<T>> int ternarySearch(T[] arr, T key, int start, int end) {
if (start > end){
if (start > end) {
return -1;
}
/* First boundary: add 1/3 of length to start */
@ -60,8 +54,7 @@ public class TernarySearch implements SearchAlgorithm{
if (key.compareTo(arr[mid1]) == 0) {
return mid1;
}
else if (key.compareTo(arr[mid2]) == 0) {
} else if (key.compareTo(arr[mid2]) == 0) {
return mid2;
}