Fakultas Ilmu Komputer UI

Commit daa17556 authored by Avatar Azka's avatar Avatar Azka

Set up experiment

parent 9ffc4e28
package daa;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.Vector;
import daa.SortingVariant;
import daa.RNG;
public class Main {
public static void main(String[] args) {
// write your code here
int size = 1000000;
RNG rand = new RNG();
int arr[] = rand.getRandomIntArray(1000, 1, 1000000);
int random[] = rand.getRandomIntArray(size, 1, size);
int ascending[] = new int[size];
for (int i = 0; i < size; i++) {
ascending[i] = i;
}
int size = arr.length;
List<Integer> out = bucketSort(arr, size);
int descending[] = new int[size];
for (int i = size-1; i > -1; i--) {
descending[size-1-i] = i;
}
System.out.println("Result: ");
System.out.println(out);
int repeating[] = new int[size];
for (int i = 0; i < size; i++) {
repeating[i] = 42;
}
int randExceptOne[] = rand.getRandomIntArray(size, 1, 100);
randExceptOne[0] = size;
// Random array
long runningTime = 0;
int iterations = 20;
for (int i = 0; i < iterations; i++) {
runningTime += sortTime(random, size);
}
runningTime /= iterations;
System.out.println("Random: " + runningTime + "ms");
// Ascending array
runningTime = 0;
for (int i = 0; i < iterations; i++) {
runningTime += sortTime(ascending, size);
}
runningTime /= iterations;
System.out.println("Ascending: " + runningTime + "ms");
// Descending array
runningTime = 0;
for (int i = 0; i < iterations; i++) {
runningTime += sortTime(descending, size);
}
runningTime /= iterations;
System.out.println("Descending: " + runningTime + "ms");
// Repeating array
runningTime = 0;
for (int i = 0; i < 1; i++) {
runningTime += sortTime(repeating, size);
}
runningTime /= 1;
System.out.println("Repeating: " + runningTime + "ms");
// randExceptOne array
runningTime = 0;
for (int i = 0; i < 1; i++) {
runningTime += sortTime(randExceptOne, size);
}
// runningTime /= iterations;
System.out.println("RandExOne: " + runningTime + "ms");
}
public static long sortTime(int[] arr, int size) {
// System.out.println("Sorting...");
long startTime = System.currentTimeMillis();
List<Integer> out = bucketSort(arr, size);
long endTime = System.currentTimeMillis();
// System.out.println("Done.");
// System.out.println("Execution time: " + (endTime - startTime) + "ms");
return endTime - startTime;
}
private static int hash(int i, int max, int numberOfBuckets) {
......
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