Commit c1423cb2 authored by Sven Woltmann's avatar Sven Woltmann Committed by Sven Woltmann

Add demos to sort primitives with Arrays.sort()

parent 90e9411c
.classpath
.idea
.project
.settings
out
target
**/gitignore
*.iml
*.class
*.bin
src/eu/happycoders/sorting/tmp/
package eu.happycoders.sorting;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
public class DoubleArrayParallelSortDemo {
private static final int NUMBER_OF_ELEMENTS = 100_000_000;
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
sortTest("sort", Arrays::sort);
sortTest("parallelSort", Arrays::parallelSort);
}
}
private static void sortTest(String methodName,
Consumer<double[]> sortMethod) {
double[] a = createRandomArray(NUMBER_OF_ELEMENTS);
long time = System.currentTimeMillis();
sortMethod.accept(a);
time = System.currentTimeMillis() - time;
System.out.println(methodName + "() took " + time + " ms");
}
private static double[] createRandomArray(int n) {
ThreadLocalRandom current = ThreadLocalRandom.current();
double[] a = new double[n];
for (int i = 0; i < n; i++) {
a[i] = current.nextDouble();
}
return a;
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
public class IntArrayRangeSortDemo {
public static void main(String[] args) {
int[] a = {4, 8, 5, 9, 2, 3, 1, 7, 6};
Arrays.sort(a, 0, 5);
System.out.println(Arrays.toString(a));
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
public class IntArraySortDemo {
public static void main(String[] args) {
int[] a = {4, 8, 5, 9, 2, 3, 1, 7, 6};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}
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