操作次数最少的排序算法
运算次数最少的排序算法是什么?我需要在 HLSL 中实现它,作为 WPF 的像素着色器效果 v2.0 的一部分,因此考虑到 Pixel Shader 的限制,它需要具有非常少量的操作。我需要对 9 个值进行排序,特别是当前像素及其邻居。
What is the sort algorithm with fewest number of operations? I need to implement it in HLSL as part of a pixel shader effect v2.0 for WPF, so it needs to have a really small number of operations, considering Pixel Shader's limitations. I need to sort 9 values, specifically the current pixel and its neighbors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要使用插入排序或基数排序。以下是一些 C++ 实现:
基数排序
您需要调用
radix()
四次,因为它仅适用于一个列。插入排序
You either want to use Insertion sort or Radix sort. Here are some C++ implementations:
Radix Sort
You need to call
radix()
four times, as it only works on one column.Insertion Sort
Knuth 在寻找最佳排序算法方面做了一些工作。然而,即使只有五个元素,比较次数最少的算法也是 实施起来非常复杂。
我建议您不要试图找到最佳算法,而是尝试找到一种易于实现且足以满足您的需求的算法。如果您可以使用标准排序算法,请首先尝试使用该算法。如果没有,那么您可以使用插入排序或合并排序来保持简单,看看这对您来说是否足够好。
插入排序:
Knuth has done some work on finding optimal sorting algorithms. However even for just five elements the algorithm with the smallest number of comparisons is very complicated to implement.
I suggest instead of trying to find the optimal algorithm that you try to find one that is simple to implement and good enough for your needs. If you have access to a standard sorting algorithm try using that first. If not then you could use an insertion sort or merge sort to keep it simple and see if that is good enough for you.
Insertion Sort: