Skip to content

Commit a076061

Browse files
committedAug 4, 2020
initial commit :)
1 parent 0abfbf0 commit a076061

23 files changed

+464
-0
lines changed
 

‎.DS_Store

6 KB
Binary file not shown.

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
25+
#out dir
26+
/out

‎.idea/$CACHE_FILE$

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/workspace.xml

+104
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dsAlgJava.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

‎out/.DS_Store

6 KB
Binary file not shown.

‎out/production/dsAlgJava/Main.class

977 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

‎src/Main.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import com.jumbuna.ds.Queue;
2+
import com.jumbuna.ds.Stack;
3+
import com.jumbuna.ds.Vector;
4+
5+
import java.util.Arrays;
6+
7+
public class Main {
8+
public static void main(String[] args) {
9+
Vector<Integer> vector = new Vector<>();
10+
vector.insert(10);
11+
vector.insert(20);
12+
vector.insert(30);
13+
vector.insert(40);
14+
vector.insert(50);
15+
16+
for(int i = 0; i < vector.size(); i++) {
17+
System.out.println(vector.elementAt(i));
18+
}
19+
}
20+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.jumbuna.algs;
2+
3+
public class BinarySearch {
4+
//iterative
5+
public static <T extends Comparable<T>> int binarySearchR(T[] array, T value, int start, int end) {
6+
if(start < end) {
7+
int midian = (start +end)/2;
8+
if(array[midian].equals(value)) {
9+
return midian;
10+
}
11+
if(array[midian].compareTo(value) > 0) {
12+
return binarySearchR(array, value, start, midian-1);
13+
}else {
14+
return binarySearchR(array, value, midian+1, end);
15+
}
16+
}
17+
return array[start].equals(value) ? start : -1;
18+
}
19+
//recursive
20+
public static <T extends Comparable<T>> int binarySearchI(T[] array, T value, int start, int stop) {
21+
int midian;
22+
while(start < stop) {
23+
midian = (start+stop)/2;
24+
if(array[midian].equals(value)) {
25+
return midian;
26+
}
27+
if(array[midian].compareTo(value) > 0) {
28+
stop = midian-1;
29+
}else {
30+
start = midian+1;
31+
}
32+
}
33+
return array[start].equals(value) ? start : -1;
34+
}
35+
}

‎src/com/jumbuna/algs/QuickSort.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.jumbuna.algs;
2+
3+
public class QuickSort {
4+
public static <T extends Comparable<T>> void quickSort(T[] array, int start, int stop) {
5+
if(start < stop) {
6+
int pivot = stop;
7+
for(int i = stop-1; i >= 0; i--) {
8+
if(array[i].compareTo(array[pivot]) > 0) {
9+
T temp = array[pivot];
10+
array[pivot] = array[i];
11+
array[i] = array[pivot-1];
12+
array[pivot-1] = temp;
13+
}
14+
}
15+
quickSort(array, start, pivot-1);
16+
quickSort(array, pivot+1, stop);
17+
}
18+
}
19+
}

‎src/com/jumbuna/ds/LinkedList.java

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.jumbuna.ds;
2+
3+
public class LinkedList<T> {
4+
private class Node {
5+
Node(T element, Node n) {
6+
data = element;
7+
next = n;
8+
}
9+
T data;
10+
Node next;
11+
}
12+
private Node head = null;
13+
private Node tail = null;
14+
private int nodeCount = 0;
15+
16+
public void insertFront(T element) {
17+
if(element != null) {
18+
if(nodeCount == 0) {
19+
head = tail = new Node(element, null);
20+
}else {
21+
head = new Node(element, head);
22+
}
23+
++nodeCount;
24+
}
25+
}
26+
27+
public void insertBack(T element) {
28+
if(element != null) {
29+
if(nodeCount == 0) {
30+
head = tail = new Node(element, null);
31+
}else {
32+
tail.next = new Node(element, null);
33+
tail = tail.next;
34+
}
35+
++nodeCount;
36+
}
37+
}
38+
39+
public T removeFront() {
40+
if(nodeCount == 0) {
41+
throw new IndexOutOfBoundsException("list is empty");
42+
}
43+
T temp = head.data;
44+
head = head.next;
45+
--nodeCount;
46+
return temp;
47+
}
48+
49+
public T removeBack() {
50+
if(nodeCount == 0) {
51+
throw new IndexOutOfBoundsException("list is empty");
52+
}
53+
Node t = head;
54+
while(!t.next.equals(tail)) {
55+
t = t.next;
56+
}
57+
T temp = tail.data;
58+
t.next = null;
59+
tail = t;
60+
--nodeCount;
61+
return temp;
62+
}
63+
64+
public T valueAt(int i) {
65+
if(i >= nodeCount) {
66+
throw new IndexOutOfBoundsException("max index is "+i);
67+
}
68+
Node temp = head;
69+
for(int j = 0; j != i; j++) {
70+
temp = temp.next;
71+
}
72+
return temp.data;
73+
}
74+
75+
public int size() {
76+
return nodeCount;
77+
}
78+
79+
public boolean empty() {
80+
return nodeCount == 0;
81+
}
82+
}

‎src/com/jumbuna/ds/Queue.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.jumbuna.ds;
2+
3+
public class Queue<T> {
4+
private LinkedList<T> list = new LinkedList<>();
5+
6+
public void push(T element) {
7+
list.insertBack(element);
8+
}
9+
10+
public T pop() {
11+
return list.removeFront();
12+
}
13+
14+
public T peek() {
15+
return list.valueAt(0);
16+
}
17+
18+
public int size() {
19+
return list.size();
20+
}
21+
22+
public boolean empty() {
23+
return list.empty();
24+
}
25+
}

‎src/com/jumbuna/ds/Stack.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.jumbuna.ds;
2+
3+
public class Stack<T> {
4+
private LinkedList<T> list = new LinkedList<>();
5+
6+
public void push(T element) {
7+
list.insertFront(element);
8+
}
9+
10+
public T pop() {
11+
return list.removeFront();
12+
}
13+
14+
public T peek() {
15+
return list.valueAt(0);
16+
}
17+
18+
public int size() {
19+
return list.size();
20+
}
21+
22+
public boolean empty() {
23+
return list.empty();
24+
}
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.