Skip to content

Commit f26a6c3

Browse files
author
phishman3579
committed
Added initial algorithms
git-svn-id: https://java-algorithms-implementation.googlecode.com/svn/trunk@2 032fbc0f-8cab-eb90-e552-f08422b9a96a
1 parent 044bec1 commit f26a6c3

11 files changed

+1102
-0
lines changed

.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JavaAlgorithmsImplementation</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Thu Dec 15 14:40:25 EST 2011
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.6
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.6

src/com/jwetherell/algorithms/Main.java

+560
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.jwetherell.algorithms.sorts;
2+
3+
import java.util.Random;
4+
5+
6+
public class BinarySearchTreeSort {
7+
private static final Random RANDOM = new Random();
8+
9+
private static int[] unsorted = null;
10+
private static Node root = null;
11+
12+
public static enum ROOT_TYPE { FIRST, MIDDLE, RANDOM };
13+
public static ROOT_TYPE type = ROOT_TYPE.RANDOM;
14+
15+
private BinarySearchTreeSort() { }
16+
17+
public static int[] sort(ROOT_TYPE type, int[] unsorted) {
18+
BinarySearchTreeSort.unsorted = unsorted;
19+
BinarySearchTreeSort.type = type;
20+
BinarySearchTreeSort.root = null;
21+
22+
generateTree();
23+
sort();
24+
25+
return BinarySearchTreeSort.unsorted;
26+
}
27+
28+
private static void sort() {
29+
int i = 0;
30+
Node node = root;
31+
while (true) {
32+
if (node.lesserNode==null) {
33+
unsorted[i++] = node.value;
34+
if (node.greaterNode != null) {
35+
node.greaterNode.parentNode = node.parentNode;
36+
node = node.greaterNode;
37+
} else if (node.parentNode == null) {
38+
if (node.greaterNode != null) {
39+
node = node.greaterNode;
40+
node.parentNode = null;
41+
} else if (node.greaterNode == null) {
42+
break;
43+
}
44+
} else {
45+
node.parentNode.lesserNode = null;
46+
node = node.parentNode;
47+
}
48+
}
49+
if (node.lesserNode != null) {
50+
node = node.lesserNode;
51+
}
52+
}
53+
}
54+
55+
private static void generateTree() {
56+
int rootIndex = getRandom(unsorted.length);
57+
int rootValue = unsorted[rootIndex];
58+
root = new Node(null,rootValue);
59+
60+
for (int i=0; i<unsorted.length; i++) {
61+
if (i==rootIndex) continue;
62+
63+
if (root==null) {
64+
root = new Node(null,rootValue);
65+
}
66+
67+
int e = unsorted[i];
68+
Node node = root;
69+
while (true) {
70+
if (e > node.value) {
71+
if (node.greaterNode==null) {
72+
node.greaterNode = new Node(node,e);
73+
break;
74+
}
75+
node = node.greaterNode;
76+
} else {
77+
if (node.lesserNode==null) {
78+
node.lesserNode = new Node(node,e);
79+
break;
80+
}
81+
node = node.lesserNode;
82+
}
83+
}
84+
}
85+
}
86+
87+
private static final int getRandom(int length) {
88+
if (type==ROOT_TYPE.RANDOM && length>0) return RANDOM.nextInt(length);
89+
if (type==ROOT_TYPE.FIRST && length>0) return 0;
90+
else return length/2;
91+
}
92+
93+
private static class Node {
94+
private Integer value = null;
95+
private Node parentNode = null;
96+
private Node lesserNode = null;
97+
private Node greaterNode = null;
98+
99+
private Node(Node parent, int value) {
100+
this.parentNode = parent;
101+
this.value = value;
102+
}
103+
104+
public String toString() {
105+
return "value="+value+
106+
" parent="+((parentNode!=null)?parentNode.value:"NULL")+
107+
" lesser="+((lesserNode!=null)?lesserNode.value:"NULL")+
108+
" greater="+((greaterNode!=null)?greaterNode.value:"NULL");
109+
}
110+
}
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.jwetherell.algorithms.sorts;
2+
3+
4+
/**
5+
* Counting sort: a non-comparative integer sorting algorithm.
6+
* Family: Counting.
7+
* Space: An Array of length r.
8+
* Stable: True.
9+
*
10+
* Average case = O(n+r)
11+
* Worst case = O(n+r)
12+
* Best case = O(n+r)
13+
* NOTE: r is the range of numbers to be sorted.
14+
*
15+
* @author Justin Wetherell <[email protected]>
16+
*/
17+
public class CountingSort {
18+
private static int[] unsorted = null;
19+
private static int[] counts = null;
20+
21+
private CountingSort() { }
22+
23+
public static int[] sort(int[] unsorted, int maxValue) {
24+
CountingSort.unsorted = unsorted;
25+
26+
counts = new int[maxValue];
27+
updateCounts();
28+
populateCounts();
29+
30+
return CountingSort.unsorted;
31+
}
32+
33+
34+
private static void updateCounts() {
35+
for (int e : unsorted) counts[e]++;
36+
}
37+
38+
private static void populateCounts() {
39+
int index=0;
40+
for (int i=0; i<counts.length; i++) {
41+
int e = counts[i];
42+
if (e>0) unsorted[index++] = i;
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.jwetherell.algorithms.sorts;
2+
3+
4+
/**
5+
* Heap sort.
6+
* Family: Selection.
7+
* Space: In-place.
8+
* Stable: False.
9+
*
10+
* Average case = O(n*log n)
11+
* Worst case = O(n*log n)
12+
* Best case = O(n*log n)
13+
*
14+
* @author Justin Wetherell <[email protected]>
15+
*/
16+
public class HeapSort {
17+
private static int[] unsorted = null;
18+
19+
private HeapSort() { }
20+
21+
public static int[] sort(int[] unsorted) {
22+
HeapSort.unsorted = unsorted;
23+
24+
createHeap();
25+
sort();
26+
27+
return HeapSort.unsorted;
28+
}
29+
30+
private static void sort() {
31+
for (int heapsize=(unsorted.length-1); heapsize>0; heapsize--) {
32+
swap(0, heapsize); // swap root with the last heap element
33+
int i = 0; // index of the element being moved down the tree
34+
while (true) {
35+
int left = (i*2)+1;
36+
if (left >= heapsize) // node has no left child
37+
break;
38+
int right = left+1;
39+
if (right >= heapsize) { // node has a left child, but no right child
40+
if (unsorted[left] > unsorted[i]) swap(left, i); // if left child is greater than node
41+
break;
42+
}
43+
if (unsorted[left] > unsorted[i]) {
44+
if (unsorted[left] > unsorted[right]) {
45+
swap(left, i);
46+
i = left;
47+
continue;
48+
} else { // (right > left > i)
49+
swap(right, i);
50+
i = right;
51+
continue;
52+
}
53+
} else { // (i > left)
54+
if (unsorted[right] > unsorted[i]) {
55+
swap(right, i);
56+
i = right;
57+
continue;
58+
} else { // (n > left) & (n > right)
59+
break;
60+
}
61+
}
62+
}
63+
}
64+
}
65+
66+
private static void createHeap() {
67+
int size = 0;
68+
for (int i=0; i<unsorted.length; i++) {
69+
int e = unsorted[i];
70+
size = add(size,e);
71+
}
72+
}
73+
74+
private static int add(int length, int element) {
75+
int i = length;
76+
unsorted[length++] = element;
77+
78+
int e = unsorted[i];
79+
int parentIndex = ((i-1)/2);
80+
int parent = unsorted[parentIndex];
81+
while (e>parent) {
82+
swap(parentIndex, i);
83+
i = parentIndex;
84+
e = unsorted[i];
85+
parentIndex = ((i-1)/2);
86+
parent = unsorted[parentIndex];
87+
}
88+
return length;
89+
}
90+
91+
private static void swap(int parentIndex, int childIndex) {
92+
int parent = unsorted[parentIndex];
93+
unsorted[parentIndex] = unsorted[childIndex];
94+
unsorted[childIndex] = parent;
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.jwetherell.algorithms.sorts;
2+
3+
4+
/**
5+
* Insertion sort.
6+
* Family: Insertion.
7+
* Space: In-place.
8+
* Stable: True.
9+
*
10+
* Average case = O(n^2)
11+
* Worst case = O(n^2)
12+
* Best case = O(n)
13+
*
14+
* @author Justin Wetherell <[email protected]>
15+
*/
16+
public abstract class InsertionSort {
17+
private static int[] unsorted = null;
18+
19+
private InsertionSort() { }
20+
21+
public static int[] sort(int[] unsorted) {
22+
InsertionSort.unsorted = unsorted;
23+
int length = InsertionSort.unsorted.length;
24+
25+
for (int i=1; i<length; i++) {
26+
sort(i);
27+
}
28+
29+
return InsertionSort.unsorted;
30+
}
31+
32+
private static void sort(int i) {
33+
for (int j=i; j>0; j--) {
34+
if (unsorted[j] < unsorted[j-1]) {
35+
int a = unsorted[j-1];
36+
unsorted[j-1]=unsorted[j];
37+
unsorted[j]=a;
38+
} else {
39+
break;
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)