This commit is related to the article BAEL-7172 (#15232)
This commit aims to add two classes that provide insights about the time complexity of Collections.sort().
This commit is contained in:
parent
290147ff08
commit
9d441e11b8
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.collectionssortcomplexity;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Fork(value = 1, warmups = 1)
|
||||
@Warmup(iterations = 5, time = 1)
|
||||
@Measurement(iterations = 5, time = 1)
|
||||
public class CollectionsSortTimeComplexityJMH {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void measureCollectionsSortBestCase(BestCaseBenchmarkState state) {
|
||||
List<Integer> sortedList = new ArrayList<>(state.sortedList);
|
||||
Collections.sort(sortedList);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void measureCollectionsSortAverageWorstCase(AverageWorstCaseBenchmarkState state) {
|
||||
List<Integer> unsortedList = new ArrayList<>(state.unsortedList);
|
||||
Collections.sort(unsortedList);
|
||||
}
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public static class BestCaseBenchmarkState {
|
||||
List<Integer> sortedList;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setUp() {
|
||||
sortedList = new ArrayList<>();
|
||||
for (int i = 1; i <= 1000000; i++) {
|
||||
sortedList.add(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public static class AverageWorstCaseBenchmarkState {
|
||||
List<Integer> unsortedList;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setUp() {
|
||||
unsortedList = new ArrayList<>();
|
||||
for (int i = 1000000; i > 0; i--) {
|
||||
unsortedList.add(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.collectionssortcomplexity;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class CollectionsSortTimeComplexityMain {
|
||||
// O(n log n) Time Complexity Example
|
||||
public static void worstAndAverageCasesTimeComplexity() {
|
||||
Integer[] sortedArray = {20, 21, 22, 23, 24, 25, 26, 17, 28, 29, 30, 31, 18, 19, 32, 33, 34, 27, 35};
|
||||
List<Integer> list = Arrays.asList(sortedArray);
|
||||
Collections.shuffle(list);
|
||||
long startTime = System.nanoTime();
|
||||
Collections.sort(list);
|
||||
long endTime = System.nanoTime();
|
||||
System.out.println("Execution Time for O(n log n): " + (endTime - startTime) + " nanoseconds");
|
||||
}
|
||||
|
||||
// O(n) Time Complexity Example
|
||||
public static void bestCaseTimeComplexity() {
|
||||
Integer[] sortedArray = {19, 22, 19, 22, 24, 25, 17, 11, 22, 23, 28, 23, 0, 1, 12, 9, 13, 27, 15};
|
||||
List<Integer> list = Arrays.asList(sortedArray);
|
||||
long startTime = System.nanoTime();
|
||||
Collections.sort(list);
|
||||
long endTime = System.nanoTime();
|
||||
System.out.println("Execution Time for O(n): " + (endTime - startTime) + " nanoseconds");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
worstAndAverageCasesTimeComplexity();
|
||||
bestCaseTimeComplexity();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue