递归冒泡排序
我搜索过谷歌,但找不到任何相关内容。我一直在寻找各种类型的排序(正如您在我之前的问题中看到的那样),我想知道是否有人知道递归冒泡排序代码。对我来说,这个想法听起来很荒谬,但我想为此做好准备,并且我很好奇这是否可以做到。我确信可以,因为我的一位教授过去曾这样问过他的学生。我认为他不会重复问题,但我很好奇,想知道是否有递归冒泡排序的代码。
I have searched Google and I cannot find anything for this. I was looking for various types of sorts (as you can see in a previous question of mine) and I was wondering if anyone knew of a recursive bubble sort code. To me, the idea sounds ridiculous, but I want to be prepared for things and I'm curious as to whether or not this can be done. I'm sure it can, as a professor of mine has asked this of his students in the past. I don't think he'll repeat questions, but I became curious and wanted to know if there was code for a bubble sort recursively.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这当然是可能的,因为任何迭代算法都可以转换为递归算法,反之亦然。
这是我们可以做到这一点的一种方法。为简单起见,我使用 C++ 并假设输入都是整数。
有趣的是,这里的每个递归函数都是尾递归的,因此一个好的优化编译器应该能够生成非递归代码。换句话说,一个好的编译器应该生成与我们迭代编写的代码几乎相同的代码。如果我有时间,我会检查这是否真的发生。 :-)
It's certainly possible to do this, since any iterative algorithm can be converted to a recursive one and vice-versa.
Here's one way that we can do this. For simplicity, I'm using C++ and assuming the inputs are all integers.
Interestingly, every recursive function here is tail-recursive, so a good optimizing compiler should be able to generate non-recursive code. In other words, a good compiler should generate pretty much the same code as if we'd written this iteratively. If I get the time, I'll check out whether this actually happens. :-)