Skip to content

Commit 88c6ad9

Browse files
authored
Merge pull request TheAlgorithms#1325 from mariaRoxana94/fix-error
Fixed 8XCorrectness_Bugs + 10XBad_Practice_Bugs + 14XDodgy_Code_Bugs
2 parents c37257c + a5f42e2 commit 88c6ad9

25 files changed

+293
-238
lines changed

Conversions/DecimalToBinary.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void main(String args[]) {
2727
public static void conventionalConversion() {
2828
int n, b = 0, c = 0, d;
2929
Scanner input = new Scanner(System.in);
30-
System.out.printf("Conventional conversion.\n\tEnter the decimal number: ");
30+
System.out.printf("Conventional conversion.%n Enter the decimal number: ");
3131
n = input.nextInt();
3232
while (n != 0) {
3333
d = n % 2;
@@ -46,7 +46,7 @@ public static void conventionalConversion() {
4646
public static void bitwiseConversion() {
4747
int n, b = 0, c = 0, d;
4848
Scanner input = new Scanner(System.in);
49-
System.out.printf("Bitwise conversion.\n\tEnter the decimal number: ");
49+
System.out.printf("Bitwise conversion.%n Enter the decimal number: ");
5050
n = input.nextInt();
5151
while (n != 0) {
5252
d = (n & 1);

Conversions/OctalToHexadecimal.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class OctalToHexadecimal {
1515
* @param s The Octal Number
1616
* @return The Decimal number
1717
*/
18-
public static int OctToDec(String s) {
18+
public static int octToDec(String s) {
1919
int i = 0;
2020
for (int j = 0; j < s.length(); j++) {
2121
char num = s.charAt(j);
@@ -32,7 +32,7 @@ public static int OctToDec(String s) {
3232
* @param d The Decimal Number
3333
* @return The Hexadecimal number
3434
*/
35-
public static String DecimalToHex(int d) {
35+
public static String decimalToHex(int d) {
3636
String digits = "0123456789ABCDEF";
3737
if (d <= 0)
3838
return "0";
@@ -54,10 +54,10 @@ public static void main(String args[]) {
5454
String oct = input.next();
5555

5656
// Pass the octal number to function and get converted deciaml form
57-
int decimal = OctToDec(oct);
57+
int decimal = octToDec(oct);
5858

5959
// Pass the decimla number to function and get converted Hex form of the number
60-
String hex = DecimalToHex(decimal);
60+
String hex = decimalToHex(decimal);
6161
System.out.println("The Hexadecimal equivalant is: " + hex);
6262
input.close();
6363
}

DataStructures/DynamicArray/DynamicArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void add(final E element) {
4141
}
4242

4343
public void put(final int index, E element) {
44-
Objects.checkIndex(index, this.size);
44+
// Objects.checkIndex(index, this.size);
4545

4646
this.elements[index] = element;
4747
}
@@ -79,7 +79,7 @@ private void fastRemove(final Object[] elements, final int index) {
7979
}
8080

8181
private E getElement(final int index) {
82-
Objects.checkIndex(index, this.size);
82+
// Objects.checkIndex(index, this.size);
8383
return (E) this.elements[index];
8484
}
8585

DataStructures/Graphs/BellmanFord.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Edge
2323
* @param v End vertex
2424
* @param c Weight
2525
*/
26-
Edge(int a,int b,int c)
26+
public Edge(int a,int b,int c)
2727
{
2828
u=a;
2929
v=b;

DataStructures/Graphs/MatrixGraphs.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ public boolean removeEdge(int from, int to) {
127127
* @return returns a string describing this graph
128128
*/
129129
public String toString() {
130-
String s = new String();
131-
s = " ";
130+
String s = " ";
132131
for (int i = 0; i < this.numberOfVertices(); i++) {
133132
s = s + String.valueOf(i) + " ";
134133
}

DataStructures/Heaps/HeapElement.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,21 @@ public String toString() {
117117
* @return true if the keys on both elements are identical and the additional info objects
118118
* are identical.
119119
*/
120-
public boolean equals(HeapElement otherHeapElement) {
121-
return (this.key == otherHeapElement.key) && (this.additionalInfo.equals(otherHeapElement.additionalInfo));
120+
@Override
121+
public boolean equals(Object o) {
122+
if (o != null) {
123+
if (!(o instanceof HeapElement)) return false;
124+
HeapElement otherHeapElement = (HeapElement) o;
125+
return (this.key == otherHeapElement.key) && (this.additionalInfo.equals(otherHeapElement.additionalInfo));
126+
}
127+
return false;
128+
}
129+
130+
@Override
131+
public int hashCode() {
132+
int result = 0;
133+
result = 31*result + (int) key;
134+
result = 31*result + (additionalInfo != null ? additionalInfo.hashCode() : 0);
135+
return result;
122136
}
123137
}

DataStructures/Heaps/MaxHeap.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ private void swap(int index1, int index2) {
4949
// Toggle an element up to its right place as long as its key is lower than its parent's
5050
private void toggleUp(int elementIndex) {
5151
double key = maxHeap.get(elementIndex - 1).getKey();
52-
while (getElementKey((int) Math.floor(elementIndex / 2)) < key) {
53-
swap(elementIndex, (int) Math.floor(elementIndex / 2));
54-
elementIndex = (int) Math.floor(elementIndex / 2);
52+
while (getElementKey((int) Math.floor(elementIndex / 2.0)) < key) {
53+
swap(elementIndex, (int) Math.floor(elementIndex / 2.0));
54+
elementIndex = (int) Math.floor(elementIndex / 2.0);
5555
}
5656
}
5757

@@ -101,7 +101,7 @@ public void deleteElement(int elementIndex) {
101101
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));
102102
maxHeap.remove(maxHeap.size());
103103
// Shall the new element be moved up...
104-
if (getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex / 2))) toggleUp(elementIndex);
104+
if (getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex / 2.0))) toggleUp(elementIndex);
105105
// ... or down ?
106106
else if (((2 * elementIndex <= maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))) ||
107107
((2 * elementIndex < maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))))

DataStructures/Heaps/MinHeap.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ private void swap(int index1, int index2) {
4444
// Toggle an element up to its right place as long as its key is lower than its parent's
4545
private void toggleUp(int elementIndex) {
4646
double key = minHeap.get(elementIndex - 1).getKey();
47-
while (getElementKey((int) Math.floor(elementIndex / 2)) > key) {
48-
swap(elementIndex, (int) Math.floor(elementIndex / 2));
49-
elementIndex = (int) Math.floor(elementIndex / 2);
47+
while (getElementKey((int) Math.floor(elementIndex / 2.0)) > key) {
48+
swap(elementIndex, (int) Math.floor(elementIndex / 2.0));
49+
elementIndex = (int) Math.floor(elementIndex / 2.0);
5050
}
5151
}
5252

@@ -96,7 +96,7 @@ public void deleteElement(int elementIndex) {
9696
minHeap.set(elementIndex - 1, getElement(minHeap.size()));
9797
minHeap.remove(minHeap.size());
9898
// Shall the new element be moved up...
99-
if (getElementKey(elementIndex) < getElementKey((int) Math.floor(elementIndex / 2))) toggleUp(elementIndex);
99+
if (getElementKey(elementIndex) < getElementKey((int)Math.floor(elementIndex / 2.0))) toggleUp(elementIndex);
100100
// ... or down ?
101101
else if (((2 * elementIndex <= minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))) ||
102102
((2 * elementIndex < minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))))

DataStructures/Lists/CircleLinkedList.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private Node(E value, Node<E> next) {
1414
//For better O.O design this should be private allows for better black box design
1515
private int size;
1616
//this will point to dummy node;
17-
private Node<E> head;
17+
private Node<E> head = null;
1818

1919
//constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
2020
public CircleLinkedList() {

DataStructures/Lists/DoublyLinkedList.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,12 @@ public void insertTail(int x) {
8686
public Link deleteHead() {
8787
Link temp = head;
8888
head = head.next; // oldHead <--> 2ndElement(head)
89-
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
90-
if (head == null)
89+
90+
if (head == null) {
9191
tail = null;
92+
} else {
93+
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
94+
}
9295
return temp;
9396
}
9497

@@ -100,10 +103,13 @@ public Link deleteHead() {
100103
public Link deleteTail() {
101104
Link temp = tail;
102105
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
103-
tail.next = null; // 2ndLast(tail) --> null
106+
104107
if (tail == null) {
105108
head = null;
109+
} else{
110+
tail.next = null; // 2ndLast(tail) --> null
106111
}
112+
107113
return temp;
108114
}
109115

DataStructures/Stacks/NodeStack.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void push(Item item) {
7474
} else {
7575
newNs.setPrevious(NodeStack.head);
7676
NodeStack.head.setNext(newNs);
77-
NodeStack.head = newNs;
77+
NodeStack.head.setHead(newNs);
7878
}
7979

8080
NodeStack.setSize(NodeStack.getSize() + 1);
@@ -89,7 +89,7 @@ public Item pop() {
8989

9090
Item item = (Item) NodeStack.head.getData();
9191

92-
NodeStack.head = NodeStack.head.getPrevious();
92+
NodeStack.head.setHead(NodeStack.head.getPrevious());
9393
NodeStack.head.setNext(null);
9494

9595
NodeStack.setSize(NodeStack.getSize() - 1);

DataStructures/Trees/LevelOrderTraversal.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public Node(int item) {
1515
// Root of the Binary Tree
1616
Node root;
1717

18-
public LevelOrderTraversal() {
19-
root = null;
18+
public LevelOrderTraversal( Node root) {
19+
this.root = root;
2020
}
2121

2222
/* function to print level order traversal of tree*/

DataStructures/Trees/LevelOrderTraversalQueue.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ public Node(int item) {
1919
}
2020
}
2121

22-
Node root;
23-
2422
/* Given a binary tree. Print its nodes in level order
2523
using array for implementing queue */
26-
void printLevelOrder() {
24+
void printLevelOrder(Node root) {
2725
Queue<Node> queue = new LinkedList<Node>();
2826
queue.add(root);
2927
while (!queue.isEmpty()) {

DataStructures/Trees/ValidBSTOrNot.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ public Node(int item) {
1313
}
1414

1515
//Root of the Binary Tree
16-
Node root;
1716

1817
/* can give min and max value according to your code or
1918
can write a function to find min and max value of tree. */
2019

2120
/* returns true if given search tree is binary
2221
search tree (efficient version) */
23-
boolean isBST() {
22+
boolean isBST(Node root) {
2423
return isBSTUtil(root, Integer.MIN_VALUE,
2524
Integer.MAX_VALUE);
2625
}

DynamicProgramming/LongestIncreasingSubsequence.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void main(String[] args) {
2222

2323
private static int upperBound(int[] ar, int l, int r, int key) {
2424
while (l < r - 1) {
25-
int m = (l + r) / 2;
25+
int m = (l + r) >>> 1;
2626
if (ar[m] >= key)
2727
r = m;
2828
else

DynamicProgramming/MatrixChainMultiplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static void main(String[] args) {
2525
count++;
2626
}
2727
for (Matrix m : mArray) {
28-
System.out.format("A(%d) = %2d x %2d\n", m.count(), m.col(), m.row());
28+
System.out.format("A(%d) = %2d x %2d%n", m.count(), m.col(), m.row());
2929
}
3030

3131
size = mArray.size();

Maths/GCD.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ public static void main(String[] args) {
5252

5353
// call gcd function (input array)
5454
System.out.println(gcd(myIntArray)); // => 4
55-
System.out.printf("gcd(40,24)=%d gcd(24,40)=%d\n", gcd(40, 24), gcd(24, 40)); // => 8
55+
System.out.printf("gcd(40,24)=%d gcd(24,40)=%d%n", gcd(40, 24), gcd(24, 40)); // => 8
5656
}
5757
}

0 commit comments

Comments
 (0)