- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经实现了插入排序和堆排序。理论上,堆排序的时间复杂度为nlogn,插入的时间复杂度为n^2。那么为什么我的插入实现要快 6 倍才能对 100,000 个长数组进行排序?
我使用 JMH 对每种排序算法的平均时间进行基准测试。这是我的基准代码:
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
public class MyBenchmark {
// setup the benchmark - create a new array for each iteration
@State(Scope.Thread)
public static class MyState {
int[] array = null;
@Setup(Level.Iteration)
public void doSetup() {
array = createArray(100000, 0, 100);
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.SECONDS)
public void insertionSort(MyState state) {
int[] array = state.array;
for (int i = 1; i < array.length; i++) {
int element = array[i];
for (int j = i - 1; j >= 0; j--) {
if (element < array[j]) {
int temp = array[j];
array[j] = element;
array[j + 1] = temp;
} else {
break;
}
}
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.SECONDS)
public void heapSort(MyState state) {
int[] array = state.array;
sort(array, array.length);
}
public static void sort(int[] arr, int size) {
for (int i = 0; i < size;) {
maxHeapify(size, arr);
int temp = arr[0];
arr[0] = arr[size - 1];
arr[size - 1] = temp;
size--;
}
}
private static void maxHeapify(int size, int[] arr) {
int nonLeafs = size / 2;
for (int i = nonLeafs; i > 0; i--) {
int arrayPos = heapToArrayPos(i), leftChild = heapToArrayPos(leftChild(i)),
rightChild = heapToArrayPos(rightChild(i));
if (rightChild < size) {
if (arr[rightChild] < arr[leftChild]) {
if (arr[arrayPos] < arr[leftChild]) {
switchWithLeftChild(arrayPos, arr);
}
} else if (arr[arrayPos] < arr[rightChild]) {
switchWithRightChild(arrayPos, arr);
}
} else if (arr[arrayPos] < arr[leftChild]) {
switchWithLeftChild(arrayPos, arr);
}
}
}
private static int heapToArrayPos(int heap) {
return heap - 1;
}
private static int rightChild(int pos) {
return pos * 2 + 1;
}
private static int leftChild(int pos) {
return pos * 2;
}
private static void switchWithRightChild(int pos, int[] arr) {
int father = arr[pos];
int childPos = heapToArrayPos(rightChild(pos + 1)), child = arr[childPos];
arr[childPos] = father;
arr[pos] = child;
}
private static void switchWithLeftChild(int pos, int[] arr) {
int father = arr[pos];
int childPos = heapToArrayPos(leftChild(pos + 1)), child = arr[childPos];
arr[childPos] = father;
arr[pos] = child;
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder().include(MyBenchmark.class.getSimpleName()).forks(1).build();
new Runner(opt).run();
}
public static int[] createArray(int length, int minValue, int maxValue) {
return IntStream.generate(() -> ThreadLocalRandom.current().nextInt(minValue, maxValue)).limit(length)
.toArray();
}
public static int[] createArray(int length) {
return createArray(length, 0, 10);
}
public static int[] createArray(int minValue, int maxValue) {
return createArray(10, minValue, maxValue);
}
}
这是基准测试输出:
JMH 1.12 (released 51 days ago) VM version: JDK 1.8.0_65, VM 25.65-b01 VM invoker: C:\Program Files\Java\jdk1.8.0_65\jre\bin\java.exe VM options: -Dfile.encoding=UTF-8 -Xbootclasspath:C:\Program Files\Java\jdk1.8.0_65\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_65\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_65\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_65\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_65\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_65\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_65\lib\tools.jar
Warmup: 20 iterations, 1 s each
Measurement: 20 iterations, 1 s each
Timeout: 10 min per iteration
Threads: 1 thread, will synchronize iterations
Benchmark mode: Average time, time/op
Benchmark: org.sample.MyBenchmark.heapSortRun progress: 0.00% complete, ETA 00:01:20
Fork: 1 of 1
Warmup Iteration 1: 17.651 s/op
Warmup Iteration 2: 16.004 s/op
Warmup Iteration 3: 14.640 s/op
Warmup Iteration 4: 14.699 s/op
Warmup Iteration 5: 14.836 s/op
Warmup Iteration 6: 14.900 s/op
Warmup Iteration 7: 14.758 s/op
Warmup Iteration 8: 15.084 s/op
Warmup Iteration 9: 15.652 s/op
Warmup Iteration 10: 15.121 s/op
Warmup Iteration 11: 15.315 s/op
Warmup Iteration 12: 15.299 s/op
Warmup Iteration 13: 15.234 s/op
Warmup Iteration 14: 14.822 s/op
Warmup Iteration 15: 15.078 s/op
Warmup Iteration 16: 15.565 s/op
Warmup Iteration 17: 15.509 s/op
Warmup Iteration 18: 15.189 s/op
Warmup Iteration 19: 14.748 s/op
Warmup Iteration 20: 14.902 s/op
Iteration 1: 14.888 s/op
Iteration 2: 15.381 s/op
Iteration 3: 16.099 s/op
Iteration 4: 15.536 s/op
Iteration 5: 15.635 s/op
Iteration 6: 16.446 s/op
Iteration 7: 16.034 s/op
Iteration 8: 15.828 s/op
Iteration 9: 15.666 s/op
Iteration 10: 16.071 s/op
Iteration 11: 15.962 s/op
Iteration 12: 15.777 s/op
Iteration 13: 15.757 s/op
Iteration 14: 15.424 s/op
Iteration 15: 15.449 s/op
Iteration 16: 15.920 s/op
Iteration 17: 14.609 s/op
Iteration 18: 14.651 s/op
Iteration 19: 14.661 s/op
Iteration 20: 14.607 s/opResult "heapSort": 15.520 ±(99.9%) 0.486 s/op [Average] (min, avg, max) = (14.607, 15.520, 16.446), stdev = 0.560 CI (99.9%): [15.034, 16.006] (assumes normal distribution)
JMH 1.12 (released 51 days ago) VM version: JDK 1.8.0_65, VM 25.65-b01 VM invoker: C:\Program Files\Java\jdk1.8.0_65\jre\bin\java.exe VM options: -Dfile.encoding=UTF-8 -Xbootclasspath:C:\Program Files\Java\jdk1.8.0_65\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_65\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_65\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_65\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_65\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_65\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_65\lib\tools.jar Warmup: 20 iterations, 1 s each Measurement: 20 iterations, 1 s each Timeout: 10 min per iteration Threads: 1 thread, will synchronize iterations Benchmark mode: Average time, time/op Benchmark: org.sample.MyBenchmark.insertionSort
Run progress: 50.00% complete, ETA 00:10:15 Fork: 1 of 1 Warmup Iteration 1: 1.726 s/op Warmup Iteration 2: 1.636 s/op Warmup Iteration 3: 1.968 s/op Warmup Iteration 4: 1.970 s/op Warmup Iteration 5: 1.961 s/op Warmup Iteration 6: 1.966 s/op Warmup Iteration 7: 1.962 s/op Warmup Iteration 8: 1.961 s/op Warmup Iteration 9: 1.959 s/op Warmup Iteration 10: 1.965 s/op Warmup Iteration 11: 1.966 s/op Warmup Iteration 12: 1.970 s/op Warmup Iteration 13: 1.964 s/op Warmup Iteration 14: 1.952 s/op Warmup Iteration 15: 1.955 s/op Warmup Iteration 16: 1.956 s/op Warmup Iteration 17: 1.972 s/op Warmup Iteration 18: 1.966 s/op Warmup Iteration 19: 1.954 s/op Warmup Iteration 20: 1.956 s/op
Iteration 1: 1.969 s/op
Iteration 2: 1.963 s/op
Iteration 3: 2.050 s/op
Iteration 4: 2.019 s/op Iteration 5: 1.934 s/op
Iteration 6: 1.953 s/op
Iteration 7: 1.961 s/op
Iteration 8: 1.972 s/op
Iteration 9: 1.957 s/op
Iteration 10: 1.956 s/op
Iteration 11: 1.975 s/op
Iteration 12: 1.950 s/op
Iteration 13: 1.965 s/op
Iteration 14: 1.961 s/op
Iteration 15: 1.950 s/op
Iteration 16: 1.956 s/op
Iteration 17: 1.975 s/op
Iteration 18: 1.966 s/op
Iteration 19: 1.959 s/op
Iteration 20: 1.965 s/opResult "insertionSort":
1.968 ±(99.9%) 0.022 s/op [Average] (min, avg, max) = (1.934, 1.968, 2.050), stdev = 0.025 CI (99.9%): [1.946, 1.990] (assumes normal distribution)Run complete. Total time: 00:09:55
Benchmark Mode Cnt Score Error Units
MyBenchmark.heapSort avgt 20 12.692 ± 0.282 s/op
MyBenchmark.insertionSort avgt 20 2.024 ± 0.020 s/op
编辑:由于我已经发布了问题,所以我在基准测试之前添加了 @setup 来设置数组,因此数组创建操作不会成为一个因素。我再次运行基准测试,插入排序的结果几乎相同。堆排序基准测试平均快了 3 秒。我只发布了更新后的结果摘要。
最佳答案
您的堆排序实现不正确。您发布的代码似乎正在进行选择排序。也就是说,对于每个项目,它调用 maxHeapify,获取堆中的第一个项目,将其放在末尾,并减少计数。因此 maxHeapify
被调用 size
次,每次的大小都会减小。 maxHeapify
中内部循环的迭代次数最终类似于 (n^2)/4
。
您已经实现了复杂度为 O(n^2) 的优化选择排序。
进行就地堆排序的技巧是首先构建堆(一次),然后重新排列它以进行排序。您调用 maxHeapify
一次:
maxHeapify(size, arr);
完成后,您将获得一个有效的最大堆,其中最大的项位于 arr[0]
等。这需要 O(n) 时间。
您想要的是一个按升序排列的数组。为此,您需要构建一个循环,从堆中复制最大的项(即 arr[0]
)并临时保存。然后,取出堆中的最后一个项目,将计数减一,然后在顶部重新插入该项目,根据需要向下筛选。最后,将前一个最大的项目放置在之前被最后一个项目占据的位置。当 count 达到 0 时,您就有了一个排序数组:
int count = size;
while (count > 0)
{
int save = arr[0]; // save the largest item
arr[0] = arr[count-1]; // move last item to top
arr[count-1] = save; // and place the largest item
count = count - 1; // reduce the count
SiftDown(0); // sift item into place
}
您所做的就是在堆上连续调用removeMax
,并将结果存储回数组中空出的位置。
SiftDown
与将项目插入堆时使用的方法相同。
请参阅我的博文,A Simple Heap of Integers ,查看使用 O(n) heapify 方法构建堆的完整示例。它是用 C# 编写的,但我认为很简单,如果你了解 Java,你就能理解它。我没有展示如何进行排序部分,但是使用该代码和上面的几行,您应该做得很好。
关于java - 堆排序与插入排序 JMH 基准测试 : why my insertion impl. 花费的时间更少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37365892/
我一直在使用 less 进行前端开发,但最近几天我遇到了这个错误。 我正在使用 PhpStorm 的观察器将 less 文件编译为 css 文件。但是当我编辑 less 文件时,编译器将这一行添加到
我在互联网上搜索Erlang的流程模型并找到了一些图表 slides 3-4在乔·阿姆斯特朗的一次演讲中。它们显示了 Erlang、java 和 C# 之间进程创建和消息传递时间之间的许多差异。谁能告
我怎样才能用更少的钱创建这个类? .class { display: none; } a:hover .class { display: block; } 最佳答案 像这样? .cla
全部,我有一些代码在 less 中做一个循环。但是如果我把px改成'%',less就不能编译less文件。我该怎么做呢?谢谢。 @iterations: 100; // helper class, w
According to the docs如果我做类似的事情: .child, .sibling { .parent & { color: black; } &
这是一个现有的通用 css 规则(原始文件): .caption-top { color: red; } 这是示意图,因为在现实生活中,我需要根据上下文将 .caption-top 选择器变成其他
所以问题是我想连接到msaccess 数据库,每次打开它时都有密码。 如果我直接打开 Access 文件,密码就有效。 如果我删除密码,我可以建立连接,这意味着如果不涉及密码,我的代码可以工作 密码是
news.less 看起来像这样; @import: "libs/base.less" base.less 看起来像这样; @import "colors.less"; @
当我在这里使用 WINLess 编译这段代码时出现错误: .icon-text-shadow (@icon-text-shadow: 0.0625rem 0.0625rem rgba(132, 108
我正在处理大型矩阵,例如 Movielens 20m dataset .我重组了在线文件,使其与页面上提到的尺寸(138000 x 27000)相匹配,因为原始文件包含的索引更大(138000 x 1
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是 Android 新手,刚来这里。 我只知道 Bitmap 逐像素存储图像并且不进行任何压缩。 Drawable和Bitmap一样吗? 或者 同样的图片文件,Drawable 比 Bitmap 占
我是一名前端开发人员,最近考虑使用 SASS 或 LESS 进行 CSS 开发。 不过,我不使用 Ruby,也不想依赖于使用 JavaScript 的用户。有没有人对使用 PHP 项目使用 SASS
我需要将日历添加到表单中。 我想安装这个项目: https://github.com/vitalets/bootstrap-datepicker 但据说: 某些样式需要 Bootstrap 的下拉组件
如果您运行如下代码: length(unique(runif(10000000))) length(unique(rnorm(10000000))) 你会看到只有大约 99.8% 的 runif 值是
我正在这样做Question首先使用 PriorityQueue 解决了这个问题:- public ArrayList solve(int A, int B, int C, int D) {
基本上就是标题所说的。我知道如果我只有一个字母,我可以使用 char 作为类型,但我需要 2 个字母的数据类型,例如“XY”。有没有比字符串使用更少存储空间(位)或更小的东西?或者多个字母通常只是保存
我有两个表,用户表和程序表。现在我只有 5-10 个计划和数以万计的用户,他们可以注册任何一个计划(也可以注册多个计划)。因此,在多对多关系的情况下,我正在考虑创建一个单独的表,例如 link_use
我们有一个基于 LESS 的样式表,我们希望为其生成多种颜色变化。我们已经定义了一个包含颜色变化(现在为 blue.less)的包含文件,并希望生成和使用该包含文件的绿色和红色变化。 我们想要做的是通
我想知道我是否可以改进我的 LESS-Snippet。我有很多带有颜色名称的变量/我自己的颜色标题和相关的前景和背景颜色。我根据我的颜色定义类名称。 @logocolorgreen: #40FF01;
我是一名优秀的程序员,十分优秀!