diff --git a/src/main/java/com/github/prominence/benchmark/SortingBenchmark.java b/src/main/java/com/github/prominence/benchmark/SortingBenchmark.java index 175623e..3052942 100644 --- a/src/main/java/com/github/prominence/benchmark/SortingBenchmark.java +++ b/src/main/java/com/github/prominence/benchmark/SortingBenchmark.java @@ -1,9 +1,6 @@ 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 com.github.prominence.sort.*; import org.openjdk.jmh.annotations.*; import java.util.Arrays; @@ -56,4 +53,10 @@ public class SortingBenchmark { public void mergeSort(MyState state) { MergeSort.sort(state.getData()); } + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + public void quickSort(MyState state) { + QuickSort.sort(state.getData()); + } } diff --git a/src/main/java/com/github/prominence/sort/QuickSort.java b/src/main/java/com/github/prominence/sort/QuickSort.java new file mode 100644 index 0000000..7addfa6 --- /dev/null +++ b/src/main/java/com/github/prominence/sort/QuickSort.java @@ -0,0 +1,36 @@ +package com.github.prominence.sort; + +public class QuickSort { + + public static void sort(int[] array) { + int startIndex = 0; + int endIndex = array.length - 1; + quickSort(array, startIndex, endIndex); + } + + private static void quickSort(int[] array, int start, int end) { + if (start >= end) return; + int i = start, j = end; + int cur = i - (i - j) / 2; + while (i < j) { + while (i < cur && (array[i] <= array[cur])) { + i++; + } + while (j > cur && (array[cur] <= array[j])) { + j--; + } + if (i < j) { + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + if (i == cur) { + cur = j; + } else if (j == cur) { + cur = i; + } + } + } + quickSort(array, start, cur); + quickSort(array, cur+1, end); + } +}