mirror of
https://github.com/Prominence/sorting-benchmarks.git
synced 2026-01-08 09:46:46 +03:00
Added benchmarks for bubble, selection, insertion, merge and default(Arrays class) sort.
This commit is contained in:
parent
58da1ceea0
commit
7811be77ab
6
.gitignore
vendored
6
.gitignore
vendored
@ -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
|
||||
71
pom.xml
Normal file
71
pom.xml
Normal file
@ -0,0 +1,71 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.github.prominence</groupId>
|
||||
<artifactId>sorting-benchmark</artifactId>
|
||||
<version>1.0</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>sorting-benchmark</finalName>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.openjdk.jmh.Main</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
<filters>
|
||||
<filter>
|
||||
<!--
|
||||
Shading signed JARs will fail without this.
|
||||
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
|
||||
-->
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/*.SF</exclude>
|
||||
<exclude>META-INF/*.DSA</exclude>
|
||||
<exclude>META-INF/*.RSA</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>1.21</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>1.21</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
12
src/main/java/com/github/prominence/Main.java
Normal file
12
src/main/java/com/github/prominence/Main.java
Normal file
@ -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());
|
||||
}
|
||||
}
|
||||
16
src/main/java/com/github/prominence/sort/BubbleSort.java
Normal file
16
src/main/java/com/github/prominence/sort/BubbleSort.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/main/java/com/github/prominence/sort/InsertionSort.java
Normal file
16
src/main/java/com/github/prominence/sort/InsertionSort.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
31
src/main/java/com/github/prominence/sort/MergeSort.java
Normal file
31
src/main/java/com/github/prominence/sort/MergeSort.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
19
src/main/java/com/github/prominence/sort/SelectionSort.java
Normal file
19
src/main/java/com/github/prominence/sort/SelectionSort.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user