Added benchmarks for bubble, selection, insertion, merge and default(Arrays class) sort.

This commit is contained in:
2019-03-08 23:19:43 +03:00
parent 58da1ceea0
commit 7811be77ab
8 changed files with 230 additions and 0 deletions
@@ -0,0 +1,12 @@
package com.github.prominence;
import org.openjdk.jmh.runner.RunnerException;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, RunnerException {
org.openjdk.jmh.Main.main(args);
}
}
@@ -0,0 +1,59 @@
package com.github.prominence.benchmark;
import com.github.prominence.sort.BubbleSort;
import com.github.prominence.sort.InsertionSort;
import com.github.prominence.sort.MergeSort;
import com.github.prominence.sort.SelectionSort;
import org.openjdk.jmh.annotations.*;
import java.util.Arrays;
import java.util.stream.IntStream;
public class SortingBenchmark {
@State(Scope.Benchmark)
public static class MyState {
private int[] basicData;
{
String launchProperty = System.getProperty("array.size");
int arraySize = launchProperty != null ? Integer.valueOf(launchProperty) : 100000;
basicData = IntStream.generate(() -> (int) (Math.random() * 10000)).limit(arraySize).toArray();
}
int[] getData() {
return Arrays.copyOf(basicData, basicData.length);
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void javaDefaultSort(MyState myState) {
Arrays.sort(myState.getData());
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void bubbleSort(MyState state) {
BubbleSort.sort(state.getData());
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void selectionSort(MyState state) {
SelectionSort.sort(state.getData());
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void insertionSort(MyState state) {
InsertionSort.sort(state.getData());
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void mergeSort(MyState state) {
MergeSort.sort(state.getData());
}
}
@@ -0,0 +1,16 @@
package com.github.prominence.sort;
public class BubbleSort {
public static void sort(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j + 1] < array[j]) {
int tmp = array[j + 1];
array[j + 1] = array[j];
array[j] = tmp;
}
}
}
}
}
@@ -0,0 +1,16 @@
package com.github.prominence.sort;
public class InsertionSort {
public static void sort(int[] array) {
for (int i = 1; i < array.length; i++) {
for (int j = i; j > 0; j--) {
if (array[j - 1] > array[j]) {
int tmp = array[j - 1];
array[j - 1] = array[j];
array[j] = tmp;
}
}
}
}
}
@@ -0,0 +1,31 @@
package com.github.prominence.sort;
public class MergeSort {
public static void sort(int[] array) {
int[] aux = new int[array.length];
mergeSort(array, aux, 0, array.length);
}
private static void mergeSort(int[] array, int[] aux, int lo, int hi) {
if (hi - lo <= 1) return;
int mid = lo + (hi - lo) / 2;
mergeSort(array, aux, lo, mid);
mergeSort(array, aux, mid, hi);
int i = lo;
int j = mid;
for (int k = lo; k < hi; k++) {
if (i == mid) {
aux[k] = array[j++];
} else if (j == hi) {
aux[k] = array[i++];
} else if (array[j] < array[i]) {
aux[k] = array[j++];
} else {
aux[k] = array[i++];
}
}
System.arraycopy(aux, lo, array, lo, hi - lo);
}
}
@@ -0,0 +1,19 @@
package com.github.prominence.sort;
public class SelectionSort {
public static void sort(int[] array) {
for (int i = 0; i < array.length-1; i++) {
int min_idx = i;
for (int j = i+1; j < array.length; j++) {
if (array[j] < array[min_idx]) {
min_idx = j;
}
}
int tmp = array[min_idx];
array[min_idx] = array[i];
array[i] = tmp;
}
}
}