Skip to content
Snippets Groups Projects
Commit 65e1e2bb authored by Haley Glavina's avatar Haley Glavina
Browse files

QuickSelect bug removed

parents 14231bad b9cba827
No related branches found
No related tags found
No related merge requests found
......@@ -2,15 +2,19 @@ package search;
import java.util.Iterator;
public class Fish<Integer> implements Iterable<Integer> {
public class Bag<Integer> implements Iterable<Integer> {
private Node first;
private Node first; //pointer to head of linked list
private class Node{
Integer item;
Node next;
}
/**
* Adds new node to linked list
* @param item the item to be added to the linked list
*/
public void add(Integer item){
Node oldfirst = first;
first = new Node();
......
/**
*
*/
package search;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author HaleyGlavina
*
*/
public class RedBlackTreeTest {
private GeneralCompare<Integer> b1;
private Field<Integer, Integer[]> fld;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
fld = (a1) -> (Integer) a1[0];
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link search.RedBlackTree#root()}.
*/
@Test
public void testRoot() {
Integer[][] x = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}};
RedBlackTree<Integer, Integer[]> myTree = new RedBlackTree<Integer, Integer[]>(fld, b1);
// Add first 5 nodes, expected root is 2
for(int i = 0; i < 4; i++){
myTree.put(x[i]);
}
assert((Integer) myTree.root().key() == 2);
// Add remaining nodes, expected root is 4
for(int i = 5; i < x.length; i++){
myTree.put(x[i]);
}
assert((Integer) myTree.root().key() == 4);
}
/**
* Test method for {@link search.RedBlackTree#put(java.lang.Object)}.
*/
@Test
public void testPut() {
Integer[][] x = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}};
RedBlackTree<Integer, Integer[]> myTree = new RedBlackTree<Integer, Integer[]>(fld, b1);
for(int i = 0; i < x.length; i++){
myTree.put(x[i]);
}
Node h = myTree.root();
// Check if right-most branch of tree matches what is expected
Integer[] expect = {4, 6, 7, 8, 9};
for (int i = 0 ; i < expect.length ; i++) {
assert(expect[i] == h.key());
h = h.right();
}
}
/**
* Test method for {@link search.RedBlackTree#rotateLeft(search.Node)}.
*/
@Test
public void testRotateLeft() {
RedBlackTree<Integer, Integer[]> myTree = new RedBlackTree<Integer, Integer[]>(fld, b1);
Integer[][] x = {{1, 1}, {2, 2}, {3, 3}, {4, 4}};
myTree.put(x[0]);
myTree.put(x[1]);
assert((Integer) myTree.root().key() == 1);
// Check if left rotation changes the root of the tree as expected
myTree.put(x[2]);
myTree.put(x[3]);
assert((Integer) myTree.root().key() == 2);
}
/**
* Test method for {@link search.RedBlackTree#rotateRight(search.Node)}.
*/
@Test
public void testRotateRight() {
RedBlackTree<Integer, Integer[]> myTree = new RedBlackTree<Integer, Integer[]>(fld, b1);
Integer[][] x = {{1, 1}, {2, 2}, {3, 3}, {4, 4}};
myTree.put(x[3]);
myTree.put(x[2]);
assert((Integer) myTree.root().key() == 4);
// Check if right rotation changes the root of the tree as expected
myTree.put(x[1]);
myTree.put(x[0]);
assert((Integer) myTree.root().key() == 3);
}
}
......@@ -119,7 +119,7 @@ public class KDT<KeyVal extends Comparable<KeyVal>> implements Serializable {
int axis = depth % getK();
int mid = (lo + hi) / 2;
MergeSort.sort(keyvals, lo, hi, axes.get(axis));
QuickSelect.median(keyvals, lo, hi, axes.get(axis));
KeyVal median = (KeyVal) keyvals[mid];
//TODO: fix size
......
<<<<<<< HEAD
package sort;
public class MergeSort{
......@@ -64,3 +65,83 @@ public class MergeSort{
}
}
}
=======
package sort;
public class MergeSort{
/*// Main Function for Testing Purposes Only
public static void main(String[] args) {
GeneralCompare<Integer> b1;
b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
Integer[] test = {3, 4, 2, 1, 5, 7, 9, 10, 11};
//Integer[] test = {2, 1};
sort(test, 0, test.length - 1, b1);
for (int i = 0 ; i < (test.length) ; i++) {
System.out.println(test[i]);
}
}*/
/**
* Wrapper function for the MergeSort implementation
* @param x Array of comparable items to be sorted
* @param lo Lower bound of a sub-array to be sorted
* @param hi Upper bound of a sub-array to be sorted
* @param gc A lambda function that compares two comparable items
*/
public static <T> void sort(Comparable<T>[] x, int lo, int hi, GeneralCompare<T> gc) {
Comparable<T>[] aux;
aux = (Comparable<T>[]) new Comparable[x.length];
sortWrapped(x, lo, hi, gc, aux);
}
/**
* Recursively sort each half of a sub-array
* @param lo Lower bound of a sub-array to be sorted
* @param hi Upper bound of a sub-array to be sorted
* @param gc A lambda function that compares two comparable items
* @param aux Auxiliary array to accommodate temporary memory use in the algorithm
*/
private static <T> void sortWrapped(Comparable<T>[] x, int lo, int hi, GeneralCompare<T> gc, Comparable<T>[] aux) {
int n = hi - lo;
if(n < 1)
return;
// Recursively sort each half of the array
int mid = lo + (n/2);
sortWrapped(x, lo, mid, gc, aux);
sortWrapped(x, mid+1, hi, gc, aux);
merge(x, lo, hi, gc, aux);
}
/**
* Merges two sorted sub-arrays into a single sorted array
* @param x Array of comparable items to be sorted
* @param lo Lower bound of a sub-array to be sorted
* @param hi Upper bound of a sub-array to be sorted
* @param gc A lambda function that compares two comparable items
* @param aux Auxiliary array to accommodate temporary memory use in the algorithm
*/
private static <T> void merge(Comparable<T>[] x, int lo, int hi, GeneralCompare<T> gc, Comparable<T>[] aux){
int n = hi - lo;
int mid = lo + (n/2);
for(int k = lo; k <= hi; k++){
aux[k] = x[k];
}
int i = lo;
int j = mid + 1;
for (int k = lo; k <= hi ; k++) {
if (i > mid)
x[k] = aux[j++]; //All elems in first half already added to x
else if (j > hi)
x[k] = aux[i++]; //All elems in second half already added to x
else if (gc.compare(aux[i], aux[j]) > 0)
x[k] = aux[j++];
else
x[k] = aux[i++];
}
}
}
>>>>>>> b9cba827252461540a3adde3e53442162b843b0f
/**
*
*/
package sort;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author HaleyGlavina
*
*/
public class MergeSortTest {
private GeneralCompare<Integer> b1;
private Integer[] test1 = {3, 4, 2, 1, 5, 7, 9, 10, 11};
private Integer[] test2 = {5, 5, 23023948, -2, -10000};
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link sort.MergeSort#sort(java.lang.Comparable<T>[], int, int, sort.GeneralCompare)}.
*/
@Test
public void testSort() {
MergeSort.sort(test1, 0, test1.length - 1, b1);
Integer[] expect = {1, 2, 3, 4, 5, 7, 9, 10, 11};
for (int i = 0 ; i < expect.length ; i++) {
if (expect[i] != test1[i])
fail("MergeSort test1 fails.");
}
}
@Test
public void testBigValues() {
MergeSort.sort(test2, 0, test2.length - 1, b1);
Integer[] expect2 = {-10000, -2, 5, 5, 23023948};
for (int i = 0 ; i < 5 ; i++) {
if (!expect2[i].equals(test2[i]))
fail("MergeSort test2 fails.");
}
}
}
package sort;
import search.GeneralCompare;
//import search.GeneralCompare;
// Code from "Algorithms: 4th Edition" by Robert Sedgewick
// Adapted from the Sedgewick Quicksort implementation
public class QuickSelect {
/*
// Main function for testing purposes only
public static void main(String[] args) {
//{1, 2, 3, 4, 5, 6, 7, 8, 9} 5 is median.
Integer[] test = {4, 6, 7, 2, 2, 2, 2,2, 2, 2,2 ,2 ,2, 2, 2};
//Integer[] test = {10};
GeneralCompare<Integer> b1;
b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
median(test, b1);
System.out.println(test[test.length/2]);
}
*/
/**
* Partially sorts a comparable array such that elements smaller than the median occur in
......@@ -30,6 +31,19 @@ public class QuickSelect {
sort(a, 0, a.length - 1, a.length/2, gc);
}
/**
* Partially sorts a comparable array such that elements smaller than the median of a
* range occur in the first half of the range, and elements larger than the median occur
* in the second half
* @param a Array of comparable items
* @param lo Lower bound of the range
* @param hi Upper bound of the range
* @param gc Lambda function to compare items
*/
public static <T> void median(Comparable<T>[] a, int lo, int hi, GeneralCompare<T> gc) {
sort(a, lo, hi, (hi - lo) / 2, gc);
}
/**
* Partially sorts a comparable array such that elements smaller than the kth largest element
* occur in the first half of the array, and larger elements occur in the second half
......
/**
*
*/
package sort;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author HaleyGlavina
*
*/
public class QuickSelectTest {
private GeneralCompare<Integer> b1;
private Integer[] test1 = {4, 6, 7, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
private Integer[] test2 = {10};
private Integer[] test3 = {-200, -1000, 2048, 1996, 0};
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link sort.QuickSelect#median(java.lang.Comparable<T>[], search.GeneralCompare)}.
*/
@Test
public void testMedian() {
QuickSelect.median(test1, b1);
assert(test1[test1.length/2] == 2);
QuickSelect.median(test2, b1);
assert(test2[test2.length/2] == 10);
QuickSelect.median(test3, b1);
assert(test3[test3.length/2] == 0);
}
/**
* Test method for {@link sort.QuickSelect#partialSort(java.lang.Comparable<T>[], int, search.GeneralCompare)}.
*/
/*
@Test
public void testPartialSort() {
QuickSelect.partialSort((Comparable<Integer>[]) test1, 5, b1);
assert(test1[test1.length - 1] == 2);
QuickSelect.partialSort((Comparable<Integer>[]) test2, 1, b1);
assert(test2[test2.length - 1] == 10);
QuickSelect.partialSort((Comparable<Integer>[]) test3, 4, b1);
assert(test3[test3.length - 4] == -200);
}*/
}
package test;
import search.Graph;
import static org.junit.Assert.*;
import org.junit.*;
public class TestGraph {
@Before
public void setUp() throws Exception {
System.out.println("Setting up....");
}
@Test
public void testV(){
Graph g = new Graph(8);
assertEquals(g.V(), 8);
assertNotEquals(g.V(), 7);
assertNotEquals(g.V(), 9);
}
@Test
public void testAddEdgeAndE(){
Graph g = new Graph(8);
g.addEdge(4, 3);
g.addEdge(2, 3);
g.addEdge(1, 2);
g.addEdge(0, 2);
assertNotEquals(g.E(), 3);
assertEquals(g.E(), 4);
g.addEdge(7, 2);
assertNotEquals(g.E(), 4);
assertEquals(g.E(), 5);
}
@Test
public void adj(){
Graph g = new Graph(8);
g.addEdge(4, 3);
g.addEdge(2, 3);
g.addEdge(1, 2);
g.addEdge(0, 2);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment