Skip to content

Instantly share code, notes, and snippets.

@jjlumagbas
Created November 25, 2016 03:12
Show Gist options
  • Save jjlumagbas/becefec8d5938a1813cc89c36240954b to your computer and use it in GitHub Desktop.
Save jjlumagbas/becefec8d5938a1813cc89c36240954b to your computer and use it in GitHub Desktop.
public class HeapSorter {
private int[] items;
private int heapSize;
private final int ROOT = 0;
public HeapSorter(int[] items) {
this.items = items;
this.heapSize = 0;
}
public void sort() {
for (int i = 0; i < items.length; i++) {
add(items[i]);
}
for (int i = items.length - 1; i >= 0; i--) {
items[i] = remove();
}
}
public void add(int x) {
// TODO
}
public void bubbleUp(int i) {
// TODO
}
public void swap(int i, int j) {
// TODO
}
public int parent(int i) {
// TODO
return -1;
}
public int remove() {
// TODO
return -1;
}
public void trickleDown(int i) {
// TODO
}
public boolean hasLeft(int i) {
// TODO
return false;
}
public boolean hasRight(int i) {
// TODO
return false;
}
public int right(int i) {
// TODO
return -1;
}
public int left(int i) {
// TODO
return -1;
}
}
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.*;
public class HeapSorterTest {
int[] items;
HeapSorter hs;
@Before
public void setUp() throws Exception {
items = new int[]{8, 12, 2, 10, 6, 4};
hs = new HeapSorter(items);
}
@Test
public void add() throws Exception {
hs.add(8);
assertEquals(
Arrays.toString(new int[]{8, 12, 2, 10, 6, 4}),
Arrays.toString(items)
);
hs.add(12);
assertEquals(
Arrays.toString(new int[]{12, 8, 2, 10, 6, 4}),
Arrays.toString(items)
);
}
@Test
public void bubbleUp() throws Exception {
hs.bubbleUp(1);
assertEquals(
Arrays.toString(new int[]{12, 8, 2, 10, 6, 4}),
Arrays.toString(items)
);
}
@Test
public void parent() throws Exception {
assertEquals(0, hs.parent(1));
}
@Test
public void sort() throws Exception {
hs.sort();
assertEquals(
Arrays.toString(new int[]{2, 4, 6, 8, 10, 12}),
Arrays.toString(items)
);
}
@Test
public void swap() throws Exception {
// TODO
fail();
}
@Test
public void remove() throws Exception {
// TODO
fail();
}
@Test
public void trickleDown() throws Exception {
// TODO
fail();
}
@Test
public void hasLeft() throws Exception {
// TODO
fail();
}
@Test
public void hasRight() throws Exception {
// TODO
fail();
}
@Test
public void right() throws Exception {
// TODO
fail();
}
@Test
public void left() throws Exception {
// TODO
fail();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment