除了教人们排序的基础知识之外,冒泡排序还有其他用途吗?
Is there a use for Bubble sort other than teaching people the basics of sorting?
除了教程和 class 房间之外,我从未遇到过使用冒泡排序的任何实用程序或代码。是否存在任何可能希望在应用程序中使用它的特定情况?
提前致谢。
它适用于非常小的数据集。请参阅此 link 以获得许多答案。
What is a bubble sort good for?
两个答案脱颖而出:
workmad3 写道:
I came across a great use for it in an optimisation anecdote recently. A program needed a set of sprites sorted in depth order each frame. The spites order wouldn't change much between frames, so as an optimisation they were bubble sorted with a single pass each frame. This was done in both directions (top to bottom and bottom to top). So the sprites were always almost sorted with a very efficient O(N) algorithm.
Tetha 写道:
we recently used bubblesort in an optimality proof for an algorithm. We had to transform an arbitrary optimal solution represented by a sequence of objects into a solution that was found by our algorithm. Because our algorithm was just "Sort by this criteria", we had to prove that we can sort an optimal solution without making it worse. In this case, bubble sort was a very good algorithm to use, because it has the nice invariant of just swapping two elements that are next to each other and are in the wrong order. Using more complicated algorithms there would have melted brains, I think.
除了教程和 class 房间之外,我从未遇到过使用冒泡排序的任何实用程序或代码。是否存在任何可能希望在应用程序中使用它的特定情况? 提前致谢。
它适用于非常小的数据集。请参阅此 link 以获得许多答案。
What is a bubble sort good for?
两个答案脱颖而出:
workmad3 写道:
I came across a great use for it in an optimisation anecdote recently. A program needed a set of sprites sorted in depth order each frame. The spites order wouldn't change much between frames, so as an optimisation they were bubble sorted with a single pass each frame. This was done in both directions (top to bottom and bottom to top). So the sprites were always almost sorted with a very efficient O(N) algorithm.
Tetha 写道:
we recently used bubblesort in an optimality proof for an algorithm. We had to transform an arbitrary optimal solution represented by a sequence of objects into a solution that was found by our algorithm. Because our algorithm was just "Sort by this criteria", we had to prove that we can sort an optimal solution without making it worse. In this case, bubble sort was a very good algorithm to use, because it has the nice invariant of just swapping two elements that are next to each other and are in the wrong order. Using more complicated algorithms there would have melted brains, I think.