From 7811be77abafa7b545a8da0ba13a6099d64ebf11 Mon Sep 17 00:00:00 2001 From: Alexey Zinchenko Date: Fri, 8 Mar 2019 23:19:43 +0300 Subject: [PATCH] Added benchmarks for bubble, selection, insertion, merge and default(Arrays class) sort. --- .gitignore | 6 ++ pom.xml | 71 +++++++++++++++++++ src/main/java/com/github/prominence/Main.java | 12 ++++ .../benchmark/SortingBenchmark.java | 59 +++++++++++++++ .../github/prominence/sort/BubbleSort.java | 16 +++++ .../github/prominence/sort/InsertionSort.java | 16 +++++ .../com/github/prominence/sort/MergeSort.java | 31 ++++++++ .../github/prominence/sort/SelectionSort.java | 19 +++++ 8 files changed, 230 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/github/prominence/Main.java create mode 100644 src/main/java/com/github/prominence/benchmark/SortingBenchmark.java create mode 100644 src/main/java/com/github/prominence/sort/BubbleSort.java create mode 100644 src/main/java/com/github/prominence/sort/InsertionSort.java create mode 100644 src/main/java/com/github/prominence/sort/MergeSort.java create mode 100644 src/main/java/com/github/prominence/sort/SelectionSort.java diff --git a/.gitignore b/.gitignore index a1c2a23..aae491d 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,9 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + + +.idea +dependency-reduced-pom.xml +*.iml +target \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ee4d1e5 --- /dev/null +++ b/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + com.github.prominence + sorting-benchmark + 1.0 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + org.apache.maven.plugins + maven-shade-plugin + 2.2 + + + package + + shade + + + sorting-benchmark + + + org.openjdk.jmh.Main + + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + + + + org.openjdk.jmh + jmh-core + 1.21 + + + org.openjdk.jmh + jmh-generator-annprocess + 1.21 + provided + + + \ No newline at end of file diff --git a/src/main/java/com/github/prominence/Main.java b/src/main/java/com/github/prominence/Main.java new file mode 100644 index 0000000..1643f91 --- /dev/null +++ b/src/main/java/com/github/prominence/Main.java @@ -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); + } +} diff --git a/src/main/java/com/github/prominence/benchmark/SortingBenchmark.java b/src/main/java/com/github/prominence/benchmark/SortingBenchmark.java new file mode 100644 index 0000000..175623e --- /dev/null +++ b/src/main/java/com/github/prominence/benchmark/SortingBenchmark.java @@ -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()); + } +} diff --git a/src/main/java/com/github/prominence/sort/BubbleSort.java b/src/main/java/com/github/prominence/sort/BubbleSort.java new file mode 100644 index 0000000..e4047e6 --- /dev/null +++ b/src/main/java/com/github/prominence/sort/BubbleSort.java @@ -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; + } + } + } + } +} diff --git a/src/main/java/com/github/prominence/sort/InsertionSort.java b/src/main/java/com/github/prominence/sort/InsertionSort.java new file mode 100644 index 0000000..4033fc1 --- /dev/null +++ b/src/main/java/com/github/prominence/sort/InsertionSort.java @@ -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; + } + } + } + } +} diff --git a/src/main/java/com/github/prominence/sort/MergeSort.java b/src/main/java/com/github/prominence/sort/MergeSort.java new file mode 100644 index 0000000..b159135 --- /dev/null +++ b/src/main/java/com/github/prominence/sort/MergeSort.java @@ -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); + } +} diff --git a/src/main/java/com/github/prominence/sort/SelectionSort.java b/src/main/java/com/github/prominence/sort/SelectionSort.java new file mode 100644 index 0000000..ad63d38 --- /dev/null +++ b/src/main/java/com/github/prominence/sort/SelectionSort.java @@ -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; + } + } +}