Skip to content

Commit 8c43f86

Browse files
committed
Reconstructing broken files
1 parent 01bc793 commit 8c43f86

File tree

4 files changed

+124
-77
lines changed

4 files changed

+124
-77
lines changed
Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
package searchAlgorithms;
22

33

4-
public class BinarySearch {
5-
public static int binarySearch(Double[] data, Double value) {
6-
int low = 0;
7-
int high = data.length - 1;
8-
int mid = (int) (low + high) / 2;
9-
10-
while (low <= high) {
11-
if (data[mid].equals(value)) {
12-
return mid;
13-
} else {
14-
if (data[mid].compareTo(value) > 0) {
15-
high = mid - 1;
16-
mid = (int) (low + high) / 2;
17-
} else if(data[mid].compareTo(value) < 0) {
18-
low = mid + 1;
19-
mid = (int) (low + high) / 2;
20-
}
21-
return 0;
22-
}
23-
}
24-
return -1;
25-
}
26-
27-
public static void main(String[] args) {
28-
29-
}
4+
public class BinarySearch<T extends Comparable<? super T>> {
5+
public int binarySearch(T[] data, T value) {
6+
int low = 0;
7+
int high = data.length - 1;
8+
int mid = (int) (low + high) / 2;
9+
10+
while (low <= high) {
11+
// Execute if MID = value
12+
if (data[mid].equals(value)) {
13+
return mid;
14+
15+
// Execute if MID > value
16+
} else if (data[mid].compareTo(value) > 0) {
17+
high = mid - 1;
18+
mid = (int) (low + high) / 2;
19+
20+
// Execute if MID < value
21+
} else if(data[mid].compareTo(value) < 0) {
22+
low = mid + 1;
23+
mid = (int) (low + high) / 2;
24+
25+
}
26+
}
27+
28+
return -1;
29+
}
30+
31+
32+
/* Test algorithm */
33+
public static void main(String[] args) {
34+
BinarySearch<Integer> bs = new BinarySearch<>();
35+
Integer[] nums = new Integer[] {1, 2, 3, 4};
36+
37+
System.out.println(bs.binarySearch(nums, 3));
38+
}
3039
}

Java/Algorithms/src/searchAlgorithms/LinearSearch.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
import java.util.Random;
44
import java.util.Arrays;
55

6-
public class LinearSearch {
6+
public class LinearSearch<T> {
77

8-
public static int linearSearch(Object[] data, Object value) {
8+
public int linearSearch(T[] data, Object value) {
99
for (int i = 0; i < data.length; i++) {
1010
if (data[i].equals(value)) return i;
1111
}
1212

1313
return -1;
1414
}
15-
15+
16+
/* Test algorithm */
1617
public static void main(String[] args) {
18+
LinearSearch<Double> ls = new LinearSearch<>();
1719
Random random = new Random();
1820
Double[] data = new Double[10];
1921
for (int i = 0; i < 10; i++) {
@@ -25,8 +27,8 @@ public static void main(String[] args) {
2527

2628
System.out.println("Data: " + Arrays.toString(data));
2729

28-
System.out.printf("val1: %f -> result: %d\n", val1, linearSearch(data, val1));
29-
System.out.printf("val2: %f -> result: %d\n", val2, linearSearch(data, val2));
30+
System.out.printf("val1: %f -> result: %d\n", val1, ls.linearSearch(data, val1));
31+
System.out.printf("val2: %f -> result: %d\n", val2, ls.linearSearch(data, val2));
3032

3133
}
3234
}

Java/Data_Structures/src/lists/LinkedList.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,22 @@ public void add(E data) {
4545
}
4646

4747

48-
public void remove() {
49-
48+
public boolean remove(E data) {
49+
if (head.data.equals(data)) {
50+
this.head = this.head.next;
51+
} else {
52+
Node<E> curr = this.head;
53+
while(curr != null) {
54+
if (curr.next.data.equals(data)) {
55+
curr.next = curr.next.next;
56+
57+
return true;
58+
}
59+
}
60+
}
5061

51-
}
62+
return false;
63+
}
5264

5365

5466
public E get(int index) {

Java/Data_Structures/src/lists/Queue.java

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,66 +20,60 @@ public String toString() {
2020

2121
return data.toString();
2222
}
23+
24+
@Override
25+
public boolean equals(Object b) {
26+
if (this == b) {
27+
return true;
28+
}
29+
30+
Node<T> B = null;
31+
if (b instanceof Node) {
32+
B = (Node<T>) b;
33+
34+
} else {
35+
return false;
36+
}
37+
38+
if (this.data.equals(B.data)) {
39+
return true;
40+
}
41+
42+
return false;
43+
}
2344
}
2445

25-
2646
private Node<E> head;
47+
private Node<E> tail;
2748
private int size;
2849

2950
public Queue() {
3051
this.head = null;
52+
this.tail = null;
3153
}
3254

33-
34-
public void addToHead(E data) {
35-
55+
/* Adds data to the end of the line */
56+
public void add(E data) {
57+
this.addToEnd(data);
3658
}
3759

38-
39-
public void addToTail(E data) {
60+
public E next() throws Exception {
4061
if (this.head == null) {
41-
this.head = new Node<E>(data);
42-
} else {
43-
Node<E> curr = head;
44-
while(curr.next != null) {
45-
curr = curr.next;
46-
}
47-
curr.next = new Node<E>(data);
48-
}
49-
this.size++;
50-
}
51-
52-
53-
public void remove() {
62+
throw new Exception("Queue is Empty");
63+
}
5464

55-
56-
}
57-
58-
59-
public E get(int index) {
60-
E retval = null;
61-
if (index > this.size - 1 || index < 0) {
62-
throw new IllegalArgumentException("Index out of range");
63-
}
64-
int i = 0;
65-
for(E e: this) {
66-
if (i == index) {
67-
retval = e;
68-
break;
69-
}
70-
i++;
71-
}
65+
E retval = this.head.data;
66+
this.head = this.head.next;
67+
this.size--;
7268

7369
return retval;
7470
}
75-
76-
71+
7772
public int size() {
7873

7974
return this.size;
8075
}
8176

82-
8377
public Iterator<E> iterator() {
8478
return new Iterator<E>() {
8579
Node<E> curr = Queue.this.head;
@@ -100,7 +94,6 @@ public E next() {
10094
};
10195
}
10296

103-
10497
@Override
10598
public String toString() {
10699
Iterator<E> iter = this.iterator();
@@ -113,13 +106,44 @@ public String toString() {
113106
return retval;
114107
}
115108

116-
109+
110+
/* private methods */
111+
private void addToEnd(E data) {
112+
if (this.head == null) {
113+
this.head = new Node<>(data);
114+
115+
} else if (this.tail == null) {
116+
this.tail = new Node<>(data);
117+
this.head.next = this.tail;
118+
119+
} else {
120+
Node<E> newTail = new Node<>(data);
121+
this.tail.next = newTail;
122+
this.tail = newTail;
123+
}
124+
this.size++;
125+
}
126+
127+
128+
/* Test Queue */
117129
public static void main(String[] args) {
118-
LinkedList<Integer> l = new LinkedList<>();
130+
Queue<Integer> l = new Queue<>();
119131
for(int i = 0; i < 10; i++) {
120132
Integer k = i;
121133
l.add(k);
122134
}
123-
System.out.println(l);
135+
136+
System.out.println("l = " + l);
137+
System.out.println("Size = " + l.size());
138+
139+
for(int i = 0; i < 5; i++) {
140+
try {
141+
System.out.println(l.next());
142+
} catch(Exception e) {
143+
144+
}
145+
}
146+
System.out.println("l = " + l);
147+
System.out.println("New size = " + l.size());
124148
}
125149
}

0 commit comments

Comments
 (0)