Commit 751c4221 authored by Sven Woltmann's avatar Sven Woltmann Committed by Sven Woltmann

Count local variable assignments in Selection Sort + add FindMinimumTest

parent bd3ecc84
This source diff could not be displayed because it is too large. You can view the blob instead.
package eu.happycoders.sort;
import eu.happycoders.sort.utils.ArrayUtils;
import java.util.Locale;
/**
* Counts minPos/min assignments for finding the smallest element in an
* unsorted array.
*
* @author <a href="[email protected]">Sven Woltmann</a>
*/
public class FindMinimumTest {
private static final int NUM_SIZES = 29;
private static final int NUM_TESTS = 100;
private static final int[] counts = new int[NUM_SIZES];
private static final int[][] countsM = new int[NUM_SIZES][NUM_TESTS];
public static void main(String[] args) {
for (int i = 0; i < NUM_TESTS; i++) {
test(i);
printResults(i + 1);
}
}
private static void test(int iteration) {
for (int i = 0; i < NUM_SIZES; i++) {
int size = 2 << i;
int assignments = countAssignmentsForSize(size);
counts[i] += assignments;
countsM[i][iteration] = assignments;
}
}
private static int countAssignmentsForSize(int size) {
int[] array = ArrayUtils.createRandomArray(size);
int min = Integer.MAX_VALUE;
int assignments = 0;
for (int i = 0; i < size; i++) {
int element = array[i];
if (element < min) {
min = element;
assignments++;
}
}
return assignments;
}
private static void printResults(int iterations) {
System.out.printf(Locale.US, "Results after %d iterations:%n", iterations);
for (int i = 0; i < NUM_SIZES; i++) {
int size = 2 << i;
double avg = (double) counts[i] / iterations;
System.out.printf(Locale.US,
"- size: %,11d --> avg. no of assignments: %5.2f%n", size, avg);
}
System.out.println();
}
private static double median(int[] count, int iterations) {
long[] longs = new long[iterations];
for (int i = 0; i < iterations; i++) {
longs[i] = count[i] * 10;
}
long median = ArrayUtils.median(longs);
return median / 10.0;
}
}
......@@ -13,6 +13,7 @@ public class Counters {
private long comparisons;
private long reads;
private long writes;
private long localVariableAssignments;
public void incIterations() {
iterations++;
......@@ -56,15 +57,20 @@ public class Counters {
writes += x;
}
public void incLocalVariableAssignments() {
localVariableAssignments++;
}
@Override
public String toString() {
return String.format(Locale.US,
"iterations = %,11d, comparisons = %,11d, " +
"reads = %,11d, writes = %,11d",
"reads = %,11d, writes = %,11d, var.assignments = %,11d",
iterations,
comparisons,
reads,
writes);
writes,
localVariableAssignments);
}
}
......@@ -52,6 +52,7 @@ public class SelectionSort implements SortAlgorithm {
if (numAtJ < min) {
minPos = j;
min = numAtJ;
counters.incLocalVariableAssignments();
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment