Skip to content

Commit

Permalink
lab13: add skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
alxhwg committed Apr 18, 2018
1 parent 69b3e32 commit 27400cf
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
72 changes: 72 additions & 0 deletions lab13/CountingSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Class with 2 ways of doing Counting sort, one naive way and one "better" way
*
* @author Akhil Batra, Alexander Hwang
*
**/
public class CountingSort {
/**
* Counting sort on the given int array. Returns a sorted version of the array.
* Does not touch original array (non-destructive method).
* DISCLAIMER: this method does not always work, find a case where it fails
*
* @param arr int array that will be sorted
* @return the sorted array
*/
public static int[] naiveCountingSort(int[] arr) {
// find max
int max = Integer.MIN_VALUE;
for (int i : arr) {
max = max > i ? max : i;
}

// gather all the counts for each value
int[] counts = new int[max + 1];
for (int i : arr) {
counts[i]++;
}

// when we're dealing with ints, we can just put each value
// count number of times into the new array
int[] sorted = new int[arr.length];
int k = 0;
for (int i = 0; i < counts.length; i += 1) {
for (int j = 0; j < counts[i]; j += 1, k += 1) {
sorted[k] = i;
}
}

// however, below is a more proper, generalized implementation of
// counting sort that uses start position calculation
int[] starts = new int[max + 1];
int pos = 0;
for (int i = 0; i < starts.length; i += 1) {
starts[i] = pos;
pos += counts[i];
}

int[] sorted2 = new int[arr.length];
for (int i = 0; i < arr.length; i += 1) {
int item = arr[i];
int place = starts[item];
sorted2[place] = item;
starts[item] += 1;
}

// return the sorted array
return sorted;
}

/**
* Counting sort on the given int array, must work even with negative numbers.
* Note, this code does not need to work for ranges of numbers greater
* than 2 billion.
* Does not touch original array (non-destructive method).
*
* @param arr int array that will be sorted
*/
public static int[] betterCountingSort(int[] arr) {
// TODO make counting sort work with arrays containing negative numbers.
return null;
}
}
60 changes: 60 additions & 0 deletions lab13/CountingSortTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import static org.junit.Assert.*;
import org.junit.Test;

public class CountingSortTester {

/**
* Array that will cause CountingSort.naiveCountingSort to fail, but
* CountingSort.betterCountingSort can handle.
**/
private static int[] someNegative = {9, 5, -4, 2, 1, -2, 5, 3, 0, -2, 3, 1, 1};

/**
* Array that both sorts should sort successfully.
**/
private static int[] nonNegative = {9, 5, 2, 1, 5, 3, 0, 3, 1, 1};

public static void assertIsSorted(int[] a) {
int previous = Integer.MIN_VALUE;
for (int x : a) {
assertTrue(x >= previous);
previous = x;
}
}

@Test
public void testNaiveWithNonNegative() {
int[] sortedNonNegative = CountingSort.naiveCountingSort(nonNegative);
assertIsSorted(sortedNonNegative);
}

// This test should PASS to indicate that the naive solution is in fact WRONG
@Test
public void testNaiveWithSomeNegative() {
try {
int[] sortedSomeNegative = CountingSort.naiveCountingSort(someNegative);
assertTrue("Naive counting sort should not sort arrays with negative numbers.",
false);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Great! Exception happened as we expected,"
+ "since this sort is broken for negative numbers.");
}
}

@Test
public void testBetterWithNonNegative() {
int[] sortedNonNegative = CountingSort.betterCountingSort(nonNegative);
assertIsSorted(sortedNonNegative);
}

@Test
public void testBetterWithSomeNegative() {
int[] sortedSomeNegative = CountingSort.betterCountingSort(someNegative);
assertIsSorted(sortedSomeNegative);
}


public static void main(String[] args) {
jh61b.junit.TestRunner.runTests(CountingSortTester.class);
}
}
48 changes: 48 additions & 0 deletions lab13/RadixSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Class for doing Radix sort
*
* @author Akhil Batra, Alexander Hwang
*
*/
public class RadixSort {
/**
* Does LSD radix sort on the passed in array with the following restrictions:
* The array can only have ASCII Strings (sequence of 1 byte characters)
* The sorting is stable and non-destructive
* The Strings can be variable length (all Strings are not constrained to 1 length)
*
* @param asciis String[] that needs to be sorted
*
* @return String[] the sorted array
*/
public static String[] sort(String[] asciis) {
// TODO: Implement LSD Sort
return null;
}

/**
* LSD helper method that performs a destructive counting sort the array of
* Strings based off characters at a specific index.
* @param asciis Input array of Strings
* @param index The position to sort the Strings on.
*/
private static void sortHelperLSD(String[] asciis, int index) {
// Optional LSD helper method for required LSD radix sort
return;
}

/**
* MSD radix sort helper function that recursively calls itself to achieve the sorted array.
* Destructive method that changes the passed in array, asciis.
*
* @param asciis String[] to be sorted
* @param start int for where to start sorting in this method (includes String at start)
* @param end int for where to end sorting in this method (does not include String at end)
* @param index the index of the character the method is currently sorting on
*
**/
private static void sortHelperMSD(String[] asciis, int start, int end, int index) {
// Optional MSD helper method for optional MSD radix sort
return;
}
}

0 comments on commit 27400cf

Please sign in to comment.