Remove unnecessary code (#4141)
This commit is contained in:
parent
805f09850c
commit
ad72c28d91
@ -1079,7 +1079,7 @@ public class Blowfish {
|
|||||||
*/
|
*/
|
||||||
private String hexToBin(String hex) {
|
private String hexToBin(String hex) {
|
||||||
String binary = "";
|
String binary = "";
|
||||||
Long num;
|
long num;
|
||||||
String binary4B;
|
String binary4B;
|
||||||
int n = hex.length();
|
int n = hex.length();
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
|
@ -160,7 +160,6 @@ public class HillCipher {
|
|||||||
System.out.println(
|
System.out.println(
|
||||||
"Invalid key, as determinant = 0. Program Terminated"
|
"Invalid key, as determinant = 0. Program Terminated"
|
||||||
);
|
);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,7 +175,7 @@ public class AnyBaseToAnyBase {
|
|||||||
// If the remainder is a digit < 10, simply add it to
|
// If the remainder is a digit < 10, simply add it to
|
||||||
// the left side of the new number.
|
// the left side of the new number.
|
||||||
if (decimalValue % b2 < 10) {
|
if (decimalValue % b2 < 10) {
|
||||||
output = Integer.toString(decimalValue % b2) + output;
|
output = decimalValue % b2 + output;
|
||||||
} // If the remainder is >= 10, add a character with the
|
} // If the remainder is >= 10, add a character with the
|
||||||
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
|
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
|
||||||
else {
|
else {
|
||||||
|
@ -34,8 +34,7 @@ public class OctalToDecimal {
|
|||||||
public static int convertOctalToDecimal(String inputOctal) {
|
public static int convertOctalToDecimal(String inputOctal) {
|
||||||
try {
|
try {
|
||||||
// Actual conversion of Octal to Decimal:
|
// Actual conversion of Octal to Decimal:
|
||||||
Integer outputDecimal = Integer.parseInt(inputOctal, 8);
|
return Integer.parseInt(inputOctal, 8);
|
||||||
return outputDecimal;
|
|
||||||
} catch (NumberFormatException ne) {
|
} catch (NumberFormatException ne) {
|
||||||
// Printing a warning message if the input is not a valid octal
|
// Printing a warning message if the input is not a valid octal
|
||||||
// number:
|
// number:
|
||||||
|
@ -74,7 +74,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
|
|||||||
for (i = 0; i < v - 1; i++) {
|
for (i = 0; i < v - 1; i++) {
|
||||||
for (j = 0; j < e; j++) {
|
for (j = 0; j < e; j++) {
|
||||||
if (
|
if (
|
||||||
(int) dist[arr[j].u] != Integer.MAX_VALUE &&
|
dist[arr[j].u] != Integer.MAX_VALUE &&
|
||||||
dist[arr[j].v] > dist[arr[j].u] + arr[j].w
|
dist[arr[j].v] > dist[arr[j].u] + arr[j].w
|
||||||
) {
|
) {
|
||||||
dist[arr[j].v] = dist[arr[j].u] + arr[j].w; // Update
|
dist[arr[j].v] = dist[arr[j].u] + arr[j].w; // Update
|
||||||
@ -85,7 +85,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
|
|||||||
// Final cycle for negative checking
|
// Final cycle for negative checking
|
||||||
for (j = 0; j < e; j++) {
|
for (j = 0; j < e; j++) {
|
||||||
if (
|
if (
|
||||||
(int) dist[arr[j].u] != Integer.MAX_VALUE &&
|
dist[arr[j].u] != Integer.MAX_VALUE &&
|
||||||
dist[arr[j].v] > dist[arr[j].u] + arr[j].w
|
dist[arr[j].v] > dist[arr[j].u] + arr[j].w
|
||||||
) {
|
) {
|
||||||
neg = 1;
|
neg = 1;
|
||||||
|
@ -28,7 +28,7 @@ public class BipartiteGrapfDFS {
|
|||||||
for (Integer it : adj.get(node)) {
|
for (Integer it : adj.get(node)) {
|
||||||
if (color[it] == -1) {
|
if (color[it] == -1) {
|
||||||
color[it] = 1 - color[node];
|
color[it] = 1 - color[node];
|
||||||
if (bipartite(V, adj, color, it) == false) {
|
if (!bipartite(V, adj, color, it)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (color[it] == color[node]) {
|
} else if (color[it] == color[node]) {
|
||||||
|
@ -12,7 +12,7 @@ class dijkstras {
|
|||||||
int min = Integer.MAX_VALUE, min_index = -1;
|
int min = Integer.MAX_VALUE, min_index = -1;
|
||||||
|
|
||||||
for (int r = 0; r < k; r++) {
|
for (int r = 0; r < k; r++) {
|
||||||
if (Set[r] == false && dist[r] <= min) {
|
if (!Set[r] && dist[r] <= min) {
|
||||||
min = dist[r];
|
min = dist[r];
|
||||||
min_index = r;
|
min_index = r;
|
||||||
}
|
}
|
||||||
|
@ -154,11 +154,7 @@ class AdjacencyMatrixGraph {
|
|||||||
* @return whether or not the vertex exists
|
* @return whether or not the vertex exists
|
||||||
*/
|
*/
|
||||||
public boolean vertexDoesExist(int aVertex) {
|
public boolean vertexDoesExist(int aVertex) {
|
||||||
if (aVertex >= 0 && aVertex < this.numberOfVertices()) {
|
return aVertex >= 0 && aVertex < this.numberOfVertices();
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -343,14 +339,14 @@ class AdjacencyMatrixGraph {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
String s = " ";
|
String s = " ";
|
||||||
for (int i = 0; i < this.numberOfVertices(); i++) {
|
for (int i = 0; i < this.numberOfVertices(); i++) {
|
||||||
s = s + String.valueOf(i) + " ";
|
s = s + i + " ";
|
||||||
}
|
}
|
||||||
s = s + " \n";
|
s = s + " \n";
|
||||||
|
|
||||||
for (int i = 0; i < this.numberOfVertices(); i++) {
|
for (int i = 0; i < this.numberOfVertices(); i++) {
|
||||||
s = s + String.valueOf(i) + " : ";
|
s = s + i + " : ";
|
||||||
for (int j = 0; j < this.numberOfVertices(); j++) {
|
for (int j = 0; j < this.numberOfVertices(); j++) {
|
||||||
s = s + String.valueOf(this._adjacency[i][j]) + " ";
|
s = s + this._adjacency[i][j] + " ";
|
||||||
}
|
}
|
||||||
s = s + "\n";
|
s = s + "\n";
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ class PrimMST {
|
|||||||
int min = Integer.MAX_VALUE, min_index = -1;
|
int min = Integer.MAX_VALUE, min_index = -1;
|
||||||
|
|
||||||
for (int v = 0; v < V; v++) {
|
for (int v = 0; v < V; v++) {
|
||||||
if (mstSet[v] == false && key[v] < min) {
|
if (!mstSet[v] && key[v] < min) {
|
||||||
min = key[v];
|
min = key[v];
|
||||||
min_index = v;
|
min_index = v;
|
||||||
}
|
}
|
||||||
@ -80,7 +80,7 @@ class PrimMST {
|
|||||||
{
|
{
|
||||||
if (
|
if (
|
||||||
graph[u][v] != 0 &&
|
graph[u][v] != 0 &&
|
||||||
mstSet[v] == false &&
|
!mstSet[v] &&
|
||||||
graph[u][v] < key[v]
|
graph[u][v] < key[v]
|
||||||
) {
|
) {
|
||||||
parent[v] = u;
|
parent[v] = u;
|
||||||
|
@ -114,7 +114,7 @@ public class TarjansAlgorithm {
|
|||||||
stronglyConnCompsUtil(n, lowTime, insertionTime, isInStack, st, graph);
|
stronglyConnCompsUtil(n, lowTime, insertionTime, isInStack, st, graph);
|
||||||
//update lowTime for the current node comparing lowtime of adj node
|
//update lowTime for the current node comparing lowtime of adj node
|
||||||
lowTime[u] = Math.min(lowTime[u], lowTime[n]);
|
lowTime[u] = Math.min(lowTime[u], lowTime[n]);
|
||||||
} else if (isInStack[n] == true) {
|
} else if (isInStack[n]) {
|
||||||
//If adj node is in stack, update low
|
//If adj node is in stack, update low
|
||||||
lowTime[u] = Math.min(lowTime[u], insertionTime[n]);
|
lowTime[u] = Math.min(lowTime[u], insertionTime[n]);
|
||||||
}
|
}
|
||||||
|
@ -234,7 +234,6 @@ public class FibonacciHeap {
|
|||||||
if (!curr.isMarked()) { //stop the recursion
|
if (!curr.isMarked()) { //stop the recursion
|
||||||
curr.mark();
|
curr.mark();
|
||||||
if (!curr.isRoot()) this.markedHeapNoodesCounter++;
|
if (!curr.isRoot()) this.markedHeapNoodesCounter++;
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
if (curr.isRoot()) {
|
if (curr.isRoot()) {
|
||||||
return;
|
return;
|
||||||
|
@ -51,18 +51,12 @@ public class MinPriorityQueue {
|
|||||||
|
|
||||||
// returns boolean value whether the heap is empty or not
|
// returns boolean value whether the heap is empty or not
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
if (0 == this.size) {
|
return 0 == this.size;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns boolean value whether the heap is full or not
|
// returns boolean value whether the heap is full or not
|
||||||
public boolean isFull() {
|
public boolean isFull() {
|
||||||
if (this.size == this.capacity) {
|
return this.size == this.capacity;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// prints the heap
|
// prints the heap
|
||||||
|
@ -174,8 +174,7 @@ public class CursorLinkedList<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2- make the os point to the next of the @var{availableNodeIndex}
|
// 2- make the os point to the next of the @var{availableNodeIndex}
|
||||||
int availableNext = cursorSpace[availableNodeIndex].next;
|
cursorSpace[os].next = cursorSpace[availableNodeIndex].next;
|
||||||
cursorSpace[os].next = availableNext;
|
|
||||||
|
|
||||||
// this to indicate an end of the list , helpful at testing since any err
|
// this to indicate an end of the list , helpful at testing since any err
|
||||||
// would throw an outOfBoundException
|
// would throw an outOfBoundException
|
||||||
|
@ -407,7 +407,7 @@ public class SinglyLinkedList extends Node {
|
|||||||
list.insert(3);
|
list.insert(3);
|
||||||
list.insertNth(1, 4);
|
list.insertNth(1, 4);
|
||||||
assert list.toString().equals("10->7->5->3->1");
|
assert list.toString().equals("10->7->5->3->1");
|
||||||
System.out.println(list.toString());
|
System.out.println(list);
|
||||||
/* Test search function */
|
/* Test search function */
|
||||||
assert list.search(10) &&
|
assert list.search(10) &&
|
||||||
list.search(5) &&
|
list.search(5) &&
|
||||||
@ -424,7 +424,7 @@ public class SinglyLinkedList extends Node {
|
|||||||
list.deleteNth(1);
|
list.deleteNth(1);
|
||||||
list.delete();
|
list.delete();
|
||||||
assert list.toString().equals("7->3");
|
assert list.toString().equals("7->3");
|
||||||
System.out.println(list.toString());
|
System.out.println(list);
|
||||||
assert list.size == 2 && list.size() == list.count();
|
assert list.size == 2 && list.size() == list.count();
|
||||||
|
|
||||||
list.clear();
|
list.clear();
|
||||||
|
@ -17,21 +17,13 @@ public class CircularQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
if (beginningOfQueue == -1) {
|
return beginningOfQueue == -1;
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFull() {
|
public boolean isFull() {
|
||||||
if (topOfQueue + 1 == beginningOfQueue) {
|
if (topOfQueue + 1 == beginningOfQueue) {
|
||||||
return true;
|
return true;
|
||||||
} else if (topOfQueue == size - 1 && beginningOfQueue == 0) {
|
} else return topOfQueue == size - 1 && beginningOfQueue == 0;
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enQueue(int value) {
|
public void enQueue(int value) {
|
||||||
|
@ -91,13 +91,12 @@ public class Deques<T> {
|
|||||||
if (tail == null) {
|
if (tail == null) {
|
||||||
// If the deque is empty, add the node as the head and tail
|
// If the deque is empty, add the node as the head and tail
|
||||||
head = newNode;
|
head = newNode;
|
||||||
tail = newNode;
|
|
||||||
} else {
|
} else {
|
||||||
// If the deque is not empty, insert the node as the new tail
|
// If the deque is not empty, insert the node as the new tail
|
||||||
newNode.prev = tail;
|
newNode.prev = tail;
|
||||||
tail.next = newNode;
|
tail.next = newNode;
|
||||||
tail = newNode;
|
|
||||||
}
|
}
|
||||||
|
tail = newNode;
|
||||||
|
|
||||||
size++;
|
size++;
|
||||||
}
|
}
|
||||||
|
@ -177,6 +177,6 @@ public class Queues {
|
|||||||
|
|
||||||
System.out.println(myQueue.peekFront()); // Will print 2
|
System.out.println(myQueue.peekFront()); // Will print 2
|
||||||
System.out.println(myQueue.peekRear()); // Will print 7
|
System.out.println(myQueue.peekRear()); // Will print 7
|
||||||
System.out.println(myQueue.toString()); // Will print [2, 5, 3, 7]
|
System.out.println(myQueue); // Will print [2, 5, 3, 7]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,8 +49,7 @@ public class AVLSimple {
|
|||||||
|
|
||||||
private Node insert(Node node, int item) {
|
private Node insert(Node node, int item) {
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
Node add = new Node(item);
|
return new Node(item);
|
||||||
return add;
|
|
||||||
}
|
}
|
||||||
if (node.data > item) {
|
if (node.data > item) {
|
||||||
node.left = insert(node.left, item);
|
node.left = insert(node.left, item);
|
||||||
|
@ -62,22 +62,21 @@ public class AVLTree {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Node child;
|
||||||
if (node.left != null) {
|
if (node.left != null) {
|
||||||
Node child = node.left;
|
child = node.left;
|
||||||
while (child.right != null) {
|
while (child.right != null) {
|
||||||
child = child.right;
|
child = child.right;
|
||||||
}
|
}
|
||||||
node.key = child.key;
|
|
||||||
delete(child);
|
|
||||||
} else {
|
} else {
|
||||||
Node child = node.right;
|
child = node.right;
|
||||||
while (child.left != null) {
|
while (child.left != null) {
|
||||||
child = child.left;
|
child = child.left;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
node.key = child.key;
|
node.key = child.key;
|
||||||
delete(child);
|
delete(child);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void delete(int delKey) {
|
public void delete(int delKey) {
|
||||||
if (root == null) {
|
if (root == null) {
|
||||||
@ -216,11 +215,7 @@ public class AVLTree {
|
|||||||
|
|
||||||
public boolean search(int key) {
|
public boolean search(int key) {
|
||||||
Node result = searchHelper(this.root, key);
|
Node result = searchHelper(this.root, key);
|
||||||
if (result != null) {
|
return result != null;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node searchHelper(Node root, int key) {
|
private Node searchHelper(Node root, int key) {
|
||||||
|
@ -117,11 +117,9 @@ public class BinaryTree {
|
|||||||
if (value < parent.data) {
|
if (value < parent.data) {
|
||||||
parent.left = newNode;
|
parent.left = newNode;
|
||||||
parent.left.parent = parent;
|
parent.left.parent = parent;
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
parent.right = newNode;
|
parent.right = newNode;
|
||||||
parent.right.parent = parent;
|
parent.right.parent = parent;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -177,7 +175,6 @@ public class BinaryTree {
|
|||||||
if (temp == root) {
|
if (temp == root) {
|
||||||
successor.parent = null;
|
successor.parent = null;
|
||||||
root = successor;
|
root = successor;
|
||||||
return true;
|
|
||||||
} // If you're not deleting the root
|
} // If you're not deleting the root
|
||||||
else {
|
else {
|
||||||
successor.parent = temp.parent;
|
successor.parent = temp.parent;
|
||||||
@ -188,8 +185,8 @@ public class BinaryTree {
|
|||||||
} else {
|
} else {
|
||||||
temp.parent.left = successor;
|
temp.parent.left = successor;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
} // One child
|
} // One child
|
||||||
else {
|
else {
|
||||||
// If it has a right child
|
// If it has a right child
|
||||||
@ -207,7 +204,6 @@ public class BinaryTree {
|
|||||||
} else {
|
} else {
|
||||||
temp.parent.right = temp.right;
|
temp.parent.right = temp.right;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
} // If it has a left child
|
} // If it has a left child
|
||||||
else {
|
else {
|
||||||
if (temp == root) {
|
if (temp == root) {
|
||||||
@ -223,8 +219,8 @@ public class BinaryTree {
|
|||||||
} else {
|
} else {
|
||||||
temp.parent.right = temp.left;
|
temp.parent.right = temp.left;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +168,6 @@ public class GenericTree {
|
|||||||
for (int i = 0; i < node.child.size(); i++) {
|
for (int i = 0; i < node.child.size(); i++) {
|
||||||
depth(node.child.get(i), dep - 1);
|
depth(node.child.get(i), dep - 1);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -309,7 +309,6 @@ public class RedBlackBST {
|
|||||||
|
|
||||||
public void insertDemo() {
|
public void insertDemo() {
|
||||||
Scanner scan = new Scanner(System.in);
|
Scanner scan = new Scanner(System.in);
|
||||||
while (true) {
|
|
||||||
System.out.println("Add items");
|
System.out.println("Add items");
|
||||||
|
|
||||||
int item;
|
int item;
|
||||||
@ -324,8 +323,6 @@ public class RedBlackBST {
|
|||||||
printTree(root);
|
printTree(root);
|
||||||
System.out.println("Pre order");
|
System.out.println("Pre order");
|
||||||
printTreepre(root);
|
printTreepre(root);
|
||||||
break;
|
|
||||||
}
|
|
||||||
scan.close();
|
scan.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ public class TrieImp {
|
|||||||
}
|
}
|
||||||
currentNode = node;
|
currentNode = node;
|
||||||
}
|
}
|
||||||
if (currentNode.end == true) {
|
if (currentNode.end) {
|
||||||
currentNode.end = false;
|
currentNode.end = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,6 @@ package com.thealgorithms.dynamicprogramming;
|
|||||||
/* A Naive recursive implementation
|
/* A Naive recursive implementation
|
||||||
of 0-1 Knapsack problem */
|
of 0-1 Knapsack problem */
|
||||||
public class BruteForceKnapsack {
|
public class BruteForceKnapsack {
|
||||||
|
|
||||||
// A utility function that returns
|
|
||||||
// maximum of two integers
|
|
||||||
static int max(int a, int b) {
|
|
||||||
return (a > b) ? a : b;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the maximum value that
|
// Returns the maximum value that
|
||||||
// can be put in a knapsack of
|
// can be put in a knapsack of
|
||||||
// capacity W
|
// capacity W
|
||||||
@ -29,8 +22,7 @@ public class BruteForceKnapsack {
|
|||||||
// (1) nth item included
|
// (1) nth item included
|
||||||
// (2) not included
|
// (2) not included
|
||||||
else {
|
else {
|
||||||
return max(
|
return Math.max(val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1),
|
||||||
val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1),
|
|
||||||
knapSack(W, wt, val, n - 1)
|
knapSack(W, wt, val, n - 1)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,6 @@ package com.thealgorithms.dynamicprogramming;
|
|||||||
// A Dynamic Programming based solution
|
// A Dynamic Programming based solution
|
||||||
// for 0-1 Knapsack problem
|
// for 0-1 Knapsack problem
|
||||||
public class DyanamicProgrammingKnapsack {
|
public class DyanamicProgrammingKnapsack {
|
||||||
|
|
||||||
static int max(int a, int b) {
|
|
||||||
return (a > b) ? a : b;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the maximum value that can
|
// Returns the maximum value that can
|
||||||
// be put in a knapsack of capacity W
|
// be put in a knapsack of capacity W
|
||||||
static int knapSack(int W, int wt[], int val[], int n) {
|
static int knapSack(int W, int wt[], int val[], int n) {
|
||||||
@ -20,8 +15,7 @@ public class DyanamicProgrammingKnapsack {
|
|||||||
if (i == 0 || w == 0) {
|
if (i == 0 || w == 0) {
|
||||||
K[i][w] = 0;
|
K[i][w] = 0;
|
||||||
} else if (wt[i - 1] <= w) {
|
} else if (wt[i - 1] <= w) {
|
||||||
K[i][w] =
|
K[i][w] = Math.max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
|
||||||
max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
|
|
||||||
} else {
|
} else {
|
||||||
K[i][w] = K[i - 1][w];
|
K[i][w] = K[i - 1][w];
|
||||||
}
|
}
|
||||||
|
@ -57,8 +57,8 @@ public class EditDistance {
|
|||||||
int insert = dp[i][j + 1] + 1;
|
int insert = dp[i][j + 1] + 1;
|
||||||
int delete = dp[i + 1][j] + 1;
|
int delete = dp[i + 1][j] + 1;
|
||||||
|
|
||||||
int min = replace > insert ? insert : replace;
|
int min = Math.min(replace, insert);
|
||||||
min = delete > min ? min : delete;
|
min = Math.min(delete, min);
|
||||||
dp[i + 1][j + 1] = min;
|
dp[i + 1][j + 1] = min;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,13 +110,12 @@ public class EditDistance {
|
|||||||
if (s1.charAt(0) == s2.charAt(0)) {
|
if (s1.charAt(0) == s2.charAt(0)) {
|
||||||
storage[m][n] =
|
storage[m][n] =
|
||||||
editDistance(s1.substring(1), s2.substring(1), storage);
|
editDistance(s1.substring(1), s2.substring(1), storage);
|
||||||
return storage[m][n];
|
|
||||||
} else {
|
} else {
|
||||||
int op1 = editDistance(s1, s2.substring(1), storage);
|
int op1 = editDistance(s1, s2.substring(1), storage);
|
||||||
int op2 = editDistance(s1.substring(1), s2, storage);
|
int op2 = editDistance(s1.substring(1), s2, storage);
|
||||||
int op3 = editDistance(s1.substring(1), s2.substring(1), storage);
|
int op3 = editDistance(s1.substring(1), s2.substring(1), storage);
|
||||||
storage[m][n] = 1 + Math.min(op1, Math.min(op2, op3));
|
storage[m][n] = 1 + Math.min(op1, Math.min(op2, op3));
|
||||||
|
}
|
||||||
return storage[m][n];
|
return storage[m][n];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@ -30,10 +30,7 @@ class LongestCommonSubsequence {
|
|||||||
if (arr1[i - 1].equals(arr2[j - 1])) {
|
if (arr1[i - 1].equals(arr2[j - 1])) {
|
||||||
lcsMatrix[i][j] = lcsMatrix[i - 1][j - 1] + 1;
|
lcsMatrix[i][j] = lcsMatrix[i - 1][j - 1] + 1;
|
||||||
} else {
|
} else {
|
||||||
lcsMatrix[i][j] =
|
lcsMatrix[i][j] = Math.max(lcsMatrix[i - 1][j], lcsMatrix[i][j - 1]);
|
||||||
lcsMatrix[i - 1][j] > lcsMatrix[i][j - 1]
|
|
||||||
? lcsMatrix[i - 1][j]
|
|
||||||
: lcsMatrix[i][j - 1];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,19 +27,9 @@ public class LongestPalindromicSubstring {
|
|||||||
if (g == 0) {
|
if (g == 0) {
|
||||||
arr[i][j] = true;
|
arr[i][j] = true;
|
||||||
} else if (g == 1) {
|
} else if (g == 1) {
|
||||||
if (input.charAt(i) == input.charAt(j)) {
|
arr[i][j] = input.charAt(i) == input.charAt(j);
|
||||||
arr[i][j] = true;
|
|
||||||
} else {
|
} else {
|
||||||
arr[i][j] = false;
|
arr[i][j] = input.charAt(i) == input.charAt(j) && arr[i + 1][j - 1];
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (
|
|
||||||
input.charAt(i) == input.charAt(j) && arr[i + 1][j - 1]
|
|
||||||
) {
|
|
||||||
arr[i][j] = true;
|
|
||||||
} else {
|
|
||||||
arr[i][j] = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arr[i][j]) {
|
if (arr[i][j]) {
|
||||||
|
@ -87,7 +87,7 @@ public class MatrixChainMultiplication {
|
|||||||
private static void printArray(int[][] array) {
|
private static void printArray(int[][] array) {
|
||||||
for (int i = 1; i < size + 1; i++) {
|
for (int i = 1; i < size + 1; i++) {
|
||||||
for (int j = 1; j < size + 1; j++) {
|
for (int j = 1; j < size + 1; j++) {
|
||||||
System.out.print(String.format("%7d", array[i][j]));
|
System.out.printf("%7d", array[i][j]);
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
@ -48,27 +48,21 @@ public class PalindromicPartitioning {
|
|||||||
if (L == 2) {
|
if (L == 2) {
|
||||||
isPalindrome[i][j] = (word.charAt(i) == word.charAt(j));
|
isPalindrome[i][j] = (word.charAt(i) == word.charAt(j));
|
||||||
} else {
|
} else {
|
||||||
if (
|
isPalindrome[i][j] = (word.charAt(i) == word.charAt(j)) &&
|
||||||
(word.charAt(i) == word.charAt(j)) &&
|
isPalindrome[i + 1][j - 1];
|
||||||
isPalindrome[i + 1][j - 1]
|
|
||||||
) {
|
|
||||||
isPalindrome[i][j] = true;
|
|
||||||
} else {
|
|
||||||
isPalindrome[i][j] = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//We find the minimum for each index
|
//We find the minimum for each index
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
if (isPalindrome[0][i] == true) {
|
if (isPalindrome[0][i]) {
|
||||||
minCuts[i] = 0;
|
minCuts[i] = 0;
|
||||||
} else {
|
} else {
|
||||||
minCuts[i] = Integer.MAX_VALUE;
|
minCuts[i] = Integer.MAX_VALUE;
|
||||||
for (j = 0; j < i; j++) {
|
for (j = 0; j < i; j++) {
|
||||||
if (
|
if (
|
||||||
isPalindrome[j + 1][i] == true &&
|
isPalindrome[j + 1][i] &&
|
||||||
1 + minCuts[j] < minCuts[i]
|
1 + minCuts[j] < minCuts[i]
|
||||||
) {
|
) {
|
||||||
minCuts[i] = 1 + minCuts[j];
|
minCuts[i] = 1 + minCuts[j];
|
||||||
|
@ -112,7 +112,7 @@ public class RegexMatching {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (strg[svidx][pvidx] != 0) {
|
if (strg[svidx][pvidx] != 0) {
|
||||||
return strg[svidx][pvidx] == 1 ? false : true;
|
return strg[svidx][pvidx] != 1;
|
||||||
}
|
}
|
||||||
char chs = src.charAt(svidx);
|
char chs = src.charAt(svidx);
|
||||||
char chp = pat.charAt(pvidx);
|
char chp = pat.charAt(pvidx);
|
||||||
@ -127,7 +127,7 @@ public class RegexMatching {
|
|||||||
} else {
|
} else {
|
||||||
ans = false;
|
ans = false;
|
||||||
}
|
}
|
||||||
strg[svidx][pvidx] = ans == false ? 1 : 2;
|
strg[svidx][pvidx] = ans ? 2 : 1;
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,9 +24,7 @@ public class WineProblem {
|
|||||||
int start = WPRecursion(arr, si + 1, ei) + arr[si] * year;
|
int start = WPRecursion(arr, si + 1, ei) + arr[si] * year;
|
||||||
int end = WPRecursion(arr, si, ei - 1) + arr[ei] * year;
|
int end = WPRecursion(arr, si, ei - 1) + arr[ei] * year;
|
||||||
|
|
||||||
int ans = Math.max(start, end);
|
return Math.max(start, end);
|
||||||
|
|
||||||
return ans;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method 2: Top-Down DP(Memoization)
|
// Method 2: Top-Down DP(Memoization)
|
||||||
|
@ -58,7 +58,7 @@ public class AmicableNumber {
|
|||||||
countofRes +
|
countofRes +
|
||||||
" Amicable_numbers.These are \n "
|
" Amicable_numbers.These are \n "
|
||||||
);
|
);
|
||||||
System.out.println(res.toString());
|
System.out.println(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -52,8 +52,7 @@ public class Combinations {
|
|||||||
// nC0 is always 1
|
// nC0 is always 1
|
||||||
long solution = 1;
|
long solution = 1;
|
||||||
for (int i = 0; i < k; i++) {
|
for (int i = 0; i < k; i++) {
|
||||||
long next = (n - i) * solution / (i + 1);
|
solution = (n - i) * solution / (i + 1);
|
||||||
solution = next;
|
|
||||||
}
|
}
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,7 @@ public class DistanceFormula {
|
|||||||
) {
|
) {
|
||||||
double dX = Math.pow(x2 - x1, 2);
|
double dX = Math.pow(x2 - x1, 2);
|
||||||
double dY = Math.pow(y2 - x1, 2);
|
double dY = Math.pow(y2 - x1, 2);
|
||||||
double d = Math.sqrt(dX + dY);
|
return Math.sqrt(dX + dY);
|
||||||
return d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double manhattanDistance(
|
public static double manhattanDistance(
|
||||||
@ -20,8 +19,7 @@ public class DistanceFormula {
|
|||||||
double x2,
|
double x2,
|
||||||
double y2
|
double y2
|
||||||
) {
|
) {
|
||||||
double d = Math.abs(x1 - x2) + Math.abs(y1 - y2);
|
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
|
||||||
return d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int hammingDistance(int[] b1, int[] b2) {
|
public static int hammingDistance(int[] b1, int[] b2) {
|
||||||
|
@ -26,22 +26,14 @@ public class EulerMethod {
|
|||||||
BiFunction<Double, Double, Double> exampleEquation1 = (x, y) -> x;
|
BiFunction<Double, Double, Double> exampleEquation1 = (x, y) -> x;
|
||||||
ArrayList<double[]> points1 = eulerFull(0, 4, 0.1, 0, exampleEquation1);
|
ArrayList<double[]> points1 = eulerFull(0, 4, 0.1, 0, exampleEquation1);
|
||||||
assert points1.get(points1.size() - 1)[1] == 7.800000000000003;
|
assert points1.get(points1.size() - 1)[1] == 7.800000000000003;
|
||||||
points1.forEach(point ->
|
points1.forEach(point -> System.out.printf("x: %1$f; y: %2$f%n", point[0], point[1]));
|
||||||
System.out.println(
|
|
||||||
String.format("x: %1$f; y: %2$f", point[0], point[1])
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// example from https://en.wikipedia.org/wiki/Euler_method
|
// example from https://en.wikipedia.org/wiki/Euler_method
|
||||||
System.out.println("\n\nexample 2:");
|
System.out.println("\n\nexample 2:");
|
||||||
BiFunction<Double, Double, Double> exampleEquation2 = (x, y) -> y;
|
BiFunction<Double, Double, Double> exampleEquation2 = (x, y) -> y;
|
||||||
ArrayList<double[]> points2 = eulerFull(0, 4, 0.1, 1, exampleEquation2);
|
ArrayList<double[]> points2 = eulerFull(0, 4, 0.1, 1, exampleEquation2);
|
||||||
assert points2.get(points2.size() - 1)[1] == 45.25925556817596;
|
assert points2.get(points2.size() - 1)[1] == 45.25925556817596;
|
||||||
points2.forEach(point ->
|
points2.forEach(point -> System.out.printf("x: %1$f; y: %2$f%n", point[0], point[1]));
|
||||||
System.out.println(
|
|
||||||
String.format("x: %1$f; y: %2$f", point[0], point[1])
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
|
// example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
|
||||||
System.out.println("\n\nexample 3:");
|
System.out.println("\n\nexample 3:");
|
||||||
@ -55,11 +47,7 @@ public class EulerMethod {
|
|||||||
exampleEquation3
|
exampleEquation3
|
||||||
);
|
);
|
||||||
assert points3.get(points3.size() - 1)[1] == 1.1116729841674804;
|
assert points3.get(points3.size() - 1)[1] == 1.1116729841674804;
|
||||||
points3.forEach(point ->
|
points3.forEach(point -> System.out.printf("x: %1$f; y: %2$f%n", point[0], point[1]));
|
||||||
System.out.println(
|
|
||||||
String.format("x: %1$f; y: %2$f", point[0], point[1])
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,11 +71,7 @@ public class EulerMethod {
|
|||||||
"stepSize should be greater than zero"
|
"stepSize should be greater than zero"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
double yNext =
|
return yCurrent + stepSize * differentialEquation.apply(xCurrent, yCurrent);
|
||||||
yCurrent +
|
|
||||||
stepSize *
|
|
||||||
differentialEquation.apply(xCurrent, yCurrent);
|
|
||||||
return yNext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,7 +17,7 @@ public class FastInverseSqrt {
|
|||||||
i = 0x5f3759df - (i >> 1);
|
i = 0x5f3759df - (i >> 1);
|
||||||
x = Float.intBitsToFloat(i);
|
x = Float.intBitsToFloat(i);
|
||||||
x = x * (1.5f - xhalf * x * x);
|
x = x * (1.5f - xhalf * x * x);
|
||||||
return x == (float) ((float) 1 / (float) Math.sqrt(number));
|
return x == ((float) 1 / (float) Math.sqrt(number));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,11 +36,7 @@ public class KrishnamurthyNumber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//evaluating if sum of the factorials of the digits equals the number itself
|
//evaluating if sum of the factorials of the digits equals the number itself
|
||||||
if (tmp == s) {
|
return tmp == s;
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ package com.thealgorithms.maths;
|
|||||||
public class StandardScore {
|
public class StandardScore {
|
||||||
|
|
||||||
public static double zScore(double num, double mean, double stdDev) {
|
public static double zScore(double num, double mean, double stdDev) {
|
||||||
double z = (num - mean) / stdDev;
|
return (num - mean) / stdDev;
|
||||||
return z;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ public class BankersAlgorithm {
|
|||||||
while (count < totalProcess) {
|
while (count < totalProcess) {
|
||||||
boolean foundSafeSystem = false;
|
boolean foundSafeSystem = false;
|
||||||
for (int m = 0; m < totalProcess; m++) {
|
for (int m = 0; m < totalProcess; m++) {
|
||||||
if (finishProcesses[m] == false) {
|
if (!finishProcesses[m]) {
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
for (j = 0; j < totalResources; j++) {
|
for (j = 0; j < totalResources; j++) {
|
||||||
@ -112,7 +112,7 @@ public class BankersAlgorithm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we could not find a next process in safe sequence.
|
// If we could not find a next process in safe sequence.
|
||||||
if (foundSafeSystem == false) {
|
if (!foundSafeSystem) {
|
||||||
System.out.print(
|
System.out.print(
|
||||||
"The system is not in the safe state because lack of resources"
|
"The system is not in the safe state because lack of resources"
|
||||||
);
|
);
|
||||||
|
@ -131,15 +131,7 @@ class Graph {
|
|||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (
|
return neighbours != null ? neighbours.equals(vertex.neighbours) : vertex.neighbours == null;
|
||||||
neighbours != null
|
|
||||||
? !neighbours.equals(vertex.neighbours)
|
|
||||||
: vertex.neighbours != null
|
|
||||||
) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -120,7 +120,7 @@ class Trieac {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If prefix is present as a word.
|
// If prefix is present as a word.
|
||||||
boolean isWord = (pCrawl.isWordEnd == true);
|
boolean isWord = (pCrawl.isWordEnd);
|
||||||
|
|
||||||
// If prefix is last node of tree (has no
|
// If prefix is last node of tree (has no
|
||||||
// children)
|
// children)
|
||||||
|
@ -139,7 +139,7 @@ public class LowestBasePalindrome {
|
|||||||
// If the remainder is a digit < 10, simply add it to
|
// If the remainder is a digit < 10, simply add it to
|
||||||
// the left side of the new number.
|
// the left side of the new number.
|
||||||
if (decimalValue % b2 < 10) {
|
if (decimalValue % b2 < 10) {
|
||||||
output = Integer.toString(decimalValue % b2) + output;
|
output = decimalValue % b2 + output;
|
||||||
} // If the remainder is >= 10, add a character with the
|
} // If the remainder is >= 10, add a character with the
|
||||||
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
|
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
|
||||||
else {
|
else {
|
||||||
|
@ -46,7 +46,7 @@ public class MiniMaxAlgorithm {
|
|||||||
"The best score for " +
|
"The best score for " +
|
||||||
(isMaximizer ? "Maximizer" : "Minimizer") +
|
(isMaximizer ? "Maximizer" : "Minimizer") +
|
||||||
" is " +
|
" is " +
|
||||||
String.valueOf(bestScore)
|
bestScore
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,14 +88,12 @@ public class MiniMaxAlgorithm {
|
|||||||
// (1 x 2) = 2; ((1 x 2) + 1) = 3
|
// (1 x 2) = 2; ((1 x 2) + 1) = 3
|
||||||
// (2 x 2) = 4; ((2 x 2) + 1) = 5 ...
|
// (2 x 2) = 4; ((2 x 2) + 1) = 5 ...
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
String.format(
|
"From %02d and %02d, %s chooses %02d%n",
|
||||||
"From %02d and %02d, %s chooses %02d",
|
|
||||||
score1,
|
score1,
|
||||||
score2,
|
score2,
|
||||||
(isMaximizer ? "Maximizer" : "Minimizer"),
|
(isMaximizer ? "Maximizer" : "Minimizer"),
|
||||||
bestScore
|
bestScore
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ class PageRank {
|
|||||||
for (k = 1; k <= totalNodes; k++) {
|
for (k = 1; k <= totalNodes; k++) {
|
||||||
this.pagerank[k] = InitialPageRank;
|
this.pagerank[k] = InitialPageRank;
|
||||||
}
|
}
|
||||||
System.out.printf("\n Initial PageRank Values , 0th Step \n");
|
System.out.print("\n Initial PageRank Values , 0th Step \n");
|
||||||
|
|
||||||
for (k = 1; k <= totalNodes; k++) {
|
for (k = 1; k <= totalNodes; k++) {
|
||||||
System.out.printf(
|
System.out.printf(
|
||||||
@ -113,7 +113,7 @@ class PageRank {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Display PageRank
|
// Display PageRank
|
||||||
System.out.printf("\n Final Page Rank : \n");
|
System.out.print("\n Final Page Rank : \n");
|
||||||
for (k = 1; k <= totalNodes; k++) {
|
for (k = 1; k <= totalNodes; k++) {
|
||||||
System.out.printf(
|
System.out.printf(
|
||||||
" Page Rank of " + k + " is :\t" + this.pagerank[k] + "\n"
|
" Page Rank of " + k + " is :\t" + this.pagerank[k] + "\n"
|
||||||
|
@ -40,7 +40,7 @@ public class RemoveDuplicateFromString {
|
|||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
if (sb.toString().indexOf(s.charAt(i)) == -1) {
|
if (sb.toString().indexOf(s.charAt(i)) == -1) {
|
||||||
sb.append(String.valueOf(s.charAt(i)));
|
sb.append(s.charAt(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
|
|
||||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -86,23 +84,15 @@ class BinarySearch implements SearchAlgorithm {
|
|||||||
BinarySearch search = new BinarySearch();
|
BinarySearch search = new BinarySearch();
|
||||||
int atIndex = search.find(integers, shouldBeFound);
|
int atIndex = search.find(integers, shouldBeFound);
|
||||||
|
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
format(
|
"Should be found: %d. Found %d at index %d. An array length %d%n",
|
||||||
"Should be found: %d. Found %d at index %d. An array length %d",
|
|
||||||
shouldBeFound,
|
shouldBeFound,
|
||||||
integers[atIndex],
|
integers[atIndex],
|
||||||
atIndex,
|
atIndex,
|
||||||
size
|
size
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
||||||
System.out.println(
|
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
|
||||||
format(
|
|
||||||
"Found by system method at an index: %d. Is equal: %b",
|
|
||||||
toCheck,
|
|
||||||
toCheck == atIndex
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
|
|
||||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -29,24 +27,16 @@ class ExponentialSearch implements SearchAlgorithm {
|
|||||||
ExponentialSearch search = new ExponentialSearch();
|
ExponentialSearch search = new ExponentialSearch();
|
||||||
int atIndex = search.find(integers, shouldBeFound);
|
int atIndex = search.find(integers, shouldBeFound);
|
||||||
|
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
format(
|
"Should be found: %d. Found %d at index %d. An array length %d%n",
|
||||||
"Should be found: %d. Found %d at index %d. An array length %d",
|
|
||||||
shouldBeFound,
|
shouldBeFound,
|
||||||
integers[atIndex],
|
integers[atIndex],
|
||||||
atIndex,
|
atIndex,
|
||||||
size
|
size
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
||||||
System.out.println(
|
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
|
||||||
format(
|
|
||||||
"Found by system method at an index: %d. Is equal: %b",
|
|
||||||
toCheck,
|
|
||||||
toCheck == atIndex
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
@ -67,28 +65,20 @@ class InterpolationSearch {
|
|||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
// the element that should be found
|
// the element that should be found
|
||||||
Integer shouldBeFound = integers[r.nextInt(size - 1)];
|
int shouldBeFound = integers[r.nextInt(size - 1)];
|
||||||
|
|
||||||
InterpolationSearch search = new InterpolationSearch();
|
InterpolationSearch search = new InterpolationSearch();
|
||||||
int atIndex = search.find(integers, shouldBeFound);
|
int atIndex = search.find(integers, shouldBeFound);
|
||||||
|
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
String.format(
|
"Should be found: %d. Found %d at index %d. An array length %d%n",
|
||||||
"Should be found: %d. Found %d at index %d. An array length %d",
|
|
||||||
shouldBeFound,
|
shouldBeFound,
|
||||||
integers[atIndex],
|
integers[atIndex],
|
||||||
atIndex,
|
atIndex,
|
||||||
size
|
size
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
||||||
System.out.println(
|
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
|
||||||
format(
|
|
||||||
"Found by system method at an index: %d. Is equal: %b",
|
|
||||||
toCheck,
|
|
||||||
toCheck == atIndex
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
|
|
||||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -72,23 +70,15 @@ public final class IterativeBinarySearch implements SearchAlgorithm {
|
|||||||
IterativeBinarySearch search = new IterativeBinarySearch();
|
IterativeBinarySearch search = new IterativeBinarySearch();
|
||||||
int atIndex = search.find(integers, shouldBeFound);
|
int atIndex = search.find(integers, shouldBeFound);
|
||||||
|
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
String.format(
|
"Should be found: %d. Found %d at index %d. An array length %d%n",
|
||||||
"Should be found: %d. Found %d at index %d. An array length %d",
|
|
||||||
shouldBeFound,
|
shouldBeFound,
|
||||||
integers[atIndex],
|
integers[atIndex],
|
||||||
atIndex,
|
atIndex,
|
||||||
size
|
size
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
||||||
System.out.println(
|
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
|
||||||
format(
|
|
||||||
"Found by system method at an index: %d. Is equal: %b",
|
|
||||||
toCheck,
|
|
||||||
toCheck == atIndex
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
|
|
||||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -70,23 +68,15 @@ public class IterativeTernarySearch implements SearchAlgorithm {
|
|||||||
IterativeTernarySearch search = new IterativeTernarySearch();
|
IterativeTernarySearch search = new IterativeTernarySearch();
|
||||||
int atIndex = search.find(integers, shouldBeFound);
|
int atIndex = search.find(integers, shouldBeFound);
|
||||||
|
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
format(
|
"Should be found: %d. Found %d at index %d. An array length %d%n",
|
||||||
"Should be found: %d. Found %d at index %d. An array length %d",
|
|
||||||
shouldBeFound,
|
shouldBeFound,
|
||||||
integers[atIndex],
|
integers[atIndex],
|
||||||
atIndex,
|
atIndex,
|
||||||
size
|
size
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
||||||
System.out.println(
|
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
|
||||||
format(
|
|
||||||
"Found by system method at an index: %d. Is equal: %b",
|
|
||||||
toCheck,
|
|
||||||
toCheck == atIndex
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,14 +53,12 @@ public class LinearSearch implements SearchAlgorithm {
|
|||||||
LinearSearch search = new LinearSearch();
|
LinearSearch search = new LinearSearch();
|
||||||
int atIndex = search.find(integers, shouldBeFound);
|
int atIndex = search.find(integers, shouldBeFound);
|
||||||
|
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
String.format(
|
"Should be found: %d. Found %d at index %d. An array length %d%n",
|
||||||
"Should be found: %d. Found %d at index %d. An array length %d",
|
|
||||||
shouldBeFound,
|
shouldBeFound,
|
||||||
integers[atIndex],
|
integers[atIndex],
|
||||||
atIndex,
|
atIndex,
|
||||||
size
|
size
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
|
|
||||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
@ -48,24 +46,16 @@ class LowerBound implements SearchAlgorithm {
|
|||||||
LowerBound search = new LowerBound();
|
LowerBound search = new LowerBound();
|
||||||
int atIndex = search.find(integers, val);
|
int atIndex = search.find(integers, val);
|
||||||
|
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
format(
|
"Val: %d. Lower Bound Found %d at index %d. An array length %d%n",
|
||||||
"Val: %d. Lower Bound Found %d at index %d. An array length %d",
|
|
||||||
val,
|
val,
|
||||||
integers[atIndex],
|
integers[atIndex],
|
||||||
atIndex,
|
atIndex,
|
||||||
size
|
size
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
boolean toCheck = integers[atIndex] >= val || integers[size - 1] < val;
|
boolean toCheck = integers[atIndex] >= val || integers[size - 1] < val;
|
||||||
System.out.println(
|
System.out.printf("Lower Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
|
||||||
format(
|
|
||||||
"Lower Bound found at an index: %d. Is greater or max element: %b",
|
|
||||||
atIndex,
|
|
||||||
toCheck
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -187,13 +187,11 @@ public class MonteCarloTreeSearch {
|
|||||||
System.out.println("N.\tScore\t\tVisits");
|
System.out.println("N.\tScore\t\tVisits");
|
||||||
|
|
||||||
for (int i = 0; i < rootNode.childNodes.size(); i++) {
|
for (int i = 0; i < rootNode.childNodes.size(); i++) {
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
String.format(
|
"%02d\t%d\t\t%d%n",
|
||||||
"%02d\t%d\t\t%d",
|
|
||||||
i + 1,
|
i + 1,
|
||||||
rootNode.childNodes.get(i).score,
|
rootNode.childNodes.get(i).score,
|
||||||
rootNode.childNodes.get(i).visitCount
|
rootNode.childNodes.get(i).visitCount
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
|
|
||||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -89,23 +87,15 @@ public class TernarySearch implements SearchAlgorithm {
|
|||||||
TernarySearch search = new TernarySearch();
|
TernarySearch search = new TernarySearch();
|
||||||
int atIndex = search.find(integers, shouldBeFound);
|
int atIndex = search.find(integers, shouldBeFound);
|
||||||
|
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
format(
|
"Should be found: %d. Found %d at index %d. An array length %d%n",
|
||||||
"Should be found: %d. Found %d at index %d. An array length %d",
|
|
||||||
shouldBeFound,
|
shouldBeFound,
|
||||||
integers[atIndex],
|
integers[atIndex],
|
||||||
atIndex,
|
atIndex,
|
||||||
size
|
size
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
||||||
System.out.println(
|
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
|
||||||
format(
|
|
||||||
"Found by system method at an index: %d. Is equal: %b",
|
|
||||||
toCheck,
|
|
||||||
toCheck == atIndex
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
|
|
||||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
@ -48,24 +46,16 @@ class UpperBound implements SearchAlgorithm {
|
|||||||
UpperBound search = new UpperBound();
|
UpperBound search = new UpperBound();
|
||||||
int atIndex = search.find(integers, val);
|
int atIndex = search.find(integers, val);
|
||||||
|
|
||||||
System.out.println(
|
System.out.printf(
|
||||||
format(
|
"Val: %d. Upper Bound Found %d at index %d. An array length %d%n",
|
||||||
"Val: %d. Upper Bound Found %d at index %d. An array length %d",
|
|
||||||
val,
|
val,
|
||||||
integers[atIndex],
|
integers[atIndex],
|
||||||
atIndex,
|
atIndex,
|
||||||
size
|
size
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
boolean toCheck = integers[atIndex] > val || integers[size - 1] < val;
|
boolean toCheck = integers[atIndex] > val || integers[size - 1] < val;
|
||||||
System.out.println(
|
System.out.printf("Upper Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
|
||||||
format(
|
|
||||||
"Upper Bound found at an index: %d. Is greater or max element: %b",
|
|
||||||
atIndex,
|
|
||||||
toCheck
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,7 +27,7 @@ public class BeadSort {
|
|||||||
|
|
||||||
for(int i = 0; i < unsorted.length; i++) {
|
for(int i = 0; i < unsorted.length; i++) {
|
||||||
int k = 0;
|
int k = 0;
|
||||||
for(int j = 0; j < (int) unsorted[i] ; j++) {
|
for(int j = 0; j < unsorted[i]; j++) {
|
||||||
grid[count[max - k - 1]++][k] = '*';
|
grid[count[max - k - 1]++][k] = '*';
|
||||||
k++;
|
k++;
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ public class CircleSort implements SortAlgorithm {
|
|||||||
int left,
|
int left,
|
||||||
int right
|
int right
|
||||||
) {
|
) {
|
||||||
Boolean swapped = false;
|
boolean swapped = false;
|
||||||
|
|
||||||
if (left == right) {
|
if (left == right) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -23,7 +23,7 @@ class CombSort implements SortAlgorithm {
|
|||||||
private int nextGap(int gap) {
|
private int nextGap(int gap) {
|
||||||
// Shrink gap by Shrink factor
|
// Shrink gap by Shrink factor
|
||||||
gap = (gap * 10) / 13;
|
gap = (gap * 10) / 13;
|
||||||
return (gap < 1) ? 1 : gap;
|
return Math.max(gap, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,8 +13,7 @@ public class MergeSortRecursive {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Integer> mergeSort() {
|
public List<Integer> mergeSort() {
|
||||||
List<Integer> arrSorted = merge(arr);
|
return merge(arr);
|
||||||
return arrSorted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Integer> merge(List<Integer> arr) {
|
private static List<Integer> merge(List<Integer> arr) {
|
||||||
|
@ -72,20 +72,20 @@ public class TreeSort implements SortAlgorithm {
|
|||||||
// ==== Integer Array =======
|
// ==== Integer Array =======
|
||||||
System.out.println("Testing for Integer Array....");
|
System.out.println("Testing for Integer Array....");
|
||||||
Integer[] a = { 3, -7, 45, 1, 343, -5, 2, 9 };
|
Integer[] a = { 3, -7, 45, 1, 343, -5, 2, 9 };
|
||||||
System.out.print(String.format("%-10s", "unsorted: "));
|
System.out.printf("%-10s", "unsorted: ");
|
||||||
print(a);
|
print(a);
|
||||||
a = treeSort.sort(a);
|
a = treeSort.sort(a);
|
||||||
System.out.print(String.format("%-10s", "sorted: "));
|
System.out.printf("%-10s", "sorted: ");
|
||||||
print(a);
|
print(a);
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
// ==== Integer List =======
|
// ==== Integer List =======
|
||||||
System.out.println("Testing for Integer List....");
|
System.out.println("Testing for Integer List....");
|
||||||
List<Integer> intList = List.of(3, -7, 45, 1, 343, -5, 2, 9);
|
List<Integer> intList = List.of(3, -7, 45, 1, 343, -5, 2, 9);
|
||||||
System.out.print(String.format("%-10s", "unsorted: "));
|
System.out.printf("%-10s", "unsorted: ");
|
||||||
print(intList);
|
print(intList);
|
||||||
intList = treeSort.sort(intList);
|
intList = treeSort.sort(intList);
|
||||||
System.out.print(String.format("%-10s", "sorted: "));
|
System.out.printf("%-10s", "sorted: ");
|
||||||
print(intList);
|
print(intList);
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
@ -101,10 +101,10 @@ public class TreeSort implements SortAlgorithm {
|
|||||||
"apple",
|
"apple",
|
||||||
"pineapple",
|
"pineapple",
|
||||||
};
|
};
|
||||||
System.out.print(String.format("%-10s", "unsorted: "));
|
System.out.printf("%-10s", "unsorted: ");
|
||||||
print(b);
|
print(b);
|
||||||
b = treeSort.sort(b);
|
b = treeSort.sort(b);
|
||||||
System.out.print(String.format("%-10s", "sorted: "));
|
System.out.printf("%-10s", "sorted: ");
|
||||||
print(b);
|
print(b);
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
@ -120,10 +120,10 @@ public class TreeSort implements SortAlgorithm {
|
|||||||
"apple",
|
"apple",
|
||||||
"pineapple"
|
"pineapple"
|
||||||
);
|
);
|
||||||
System.out.print(String.format("%-10s", "unsorted: "));
|
System.out.printf("%-10s", "unsorted: ");
|
||||||
print(stringList);
|
print(stringList);
|
||||||
stringList = treeSort.sort(stringList);
|
stringList = treeSort.sort(stringList);
|
||||||
System.out.print(String.format("%-10s", "sorted: "));
|
System.out.printf("%-10s", "sorted: ");
|
||||||
print(stringList);
|
print(stringList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ public class WiggleSort implements SortAlgorithm {
|
|||||||
|
|
||||||
median =
|
median =
|
||||||
select(
|
select(
|
||||||
Arrays.<T>asList(sortThis),
|
Arrays.asList(sortThis),
|
||||||
(int) floor(sortThis.length / 2.0)
|
(int) floor(sortThis.length / 2.0)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ class Solution {
|
|||||||
String maxStr = "";
|
String maxStr = "";
|
||||||
for (int i = 0; i < n; ++i) {
|
for (int i = 0; i < n; ++i) {
|
||||||
for (int j = i; j < n; ++j) {
|
for (int j = i; j < n; ++j) {
|
||||||
if (isValid(s, i, j) == true) {
|
if (isValid(s, i, j)) {
|
||||||
if (j - i + 1 > maxStr.length()) { // update maxStr
|
if (j - i + 1 > maxStr.length()) { // update maxStr
|
||||||
maxStr = s.substring(i, j + 1);
|
maxStr = s.substring(i, j + 1);
|
||||||
}
|
}
|
||||||
|
@ -72,11 +72,6 @@ public static int myAtoi(String s) {
|
|||||||
if (db1 > (2147483647)) {
|
if (db1 > (2147483647)) {
|
||||||
return 2147483647;
|
return 2147483647;
|
||||||
}
|
}
|
||||||
}else if (number.length() == 10 && negative) {
|
|
||||||
double db1 = Double.parseDouble(number);
|
|
||||||
if (db1 >= 2147483648d) {
|
|
||||||
return -2147483648;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(negative){
|
if(negative){
|
||||||
|
Loading…
Reference in New Issue
Block a user