有人可以向我解释一下关于冒泡排序这一说法背后的逻辑吗?

发布于 2024-12-21 09:13:19 字数 759 浏览 4 评论 0原文

假设我们有一个名为 A 的数组

。令 Π 为一组 (x,y) 对,其中 x,y 是 A 数组中存在的值,并且索引(x) < 。索引(y)并且x>y。

例如,如果我们有这个数组

      3 2 9 8 3 0

,那么 (3,2) 将在 Π 中。 (3,0) 也将在 Π 中。

Π 中的所有对将如下

   { (3,2), (3,0), (8,0), (9,0),(9,3),(2,0),(8,3),(9,8) }

我希望我没有忘记一些事情

我意识到如果我们修复所有这些对,那么我们将对数组进行排序。当我说修复时,我的意思是,例如 (3,2) 使其成为 (2,3) ,而对于其他人,

我不明白的是,冒泡排序每一步修复多少对?我的老师告诉我 1,我不明白,

让我们运行冒泡排序,

 3 2 9 8 3 0
 2 3 9 8 3 0
 2 3 9 8 3 0
 2 3 8 9 3 0
 2 3 8 3 9 0
 2 3 8 3 0 9

 2 3 8 3 0 9
 2 3 8 3 0 9
 2 3 3 8 0 9
 2 3 3 0 8 9

 2 3 3 0 8 9
 2 3 3 0 8 9
 2 3 0 3 8 9

 2 3 0 3 8 9
 2 0 3 3 8 9

 0 2 3 3 8 9

是不是有一些步骤冒泡排序无法解决任何问题?那么,冒泡排序每一步最多只能修复1个点是正确答案吗?

Let's suppose we have some array called A.

let Π be a set of (x,y) pairs, where x,y are values that exist in the array of A and index(x) < index(y) and x>y.

so for example if we had this array

      3 2 9 8 3 0

then (3,2) will be in Π.
(3,0) will also be in Π.

all the pairs in Π will be the following

   { (3,2), (3,0), (8,0), (9,0),(9,3),(2,0),(8,3),(9,8) }

I hope I haven't forgotten something

I realize that if we fix all these pairs, then we will sort the array. When I say fix I mean, for example (3,2) make it (2,3) and for the others as well

what I don't understand is, how many pairs at each step does bubble sort fix? my teacher told me 1 and I don't understand this

let's run bubble sort

 3 2 9 8 3 0
 2 3 9 8 3 0
 2 3 9 8 3 0
 2 3 8 9 3 0
 2 3 8 3 9 0
 2 3 8 3 0 9

 2 3 8 3 0 9
 2 3 8 3 0 9
 2 3 3 8 0 9
 2 3 3 0 8 9

 2 3 3 0 8 9
 2 3 3 0 8 9
 2 3 0 3 8 9

 2 3 0 3 8 9
 2 0 3 3 8 9

 0 2 3 3 8 9

aren't there some steps where bubble sort doesn't fix anything? So, is the correct answer that bubble sort will only fix at most 1 point in every step?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强辩 2024-12-28 09:13:19

在我看来,在您的示例数据集中,冒泡排序总是会“修复”一个元素,因为每个元素都是乱序的。但是,如果您将 0 移到更靠近原始列表前面的位置,那么您将生成一些已经按排序顺序的对。这些对不会被冒泡排序“修复”,在这种情况下,您可以正确地说冒泡排序可以在每个步骤上“修复”最多 1 个元素。

所以在一般情况下,你是正确的。在您的示例中使用的具体情况中,老师是正确的。

注意:我假设“步骤”是指将冒泡排序算法应用于集合中的一对数字。

It looks to me that in your example dataset, bubble-sort will always "fix" exactly one element, because each element is out of order. If, however, you were to move the 0 closer to the front of the original list, then you would generate some pairs that are already in sorted order. These pairs would not be "fixed" by bubble-sort, and in such a case you would be correct in saying that bubble sort may "fix" up to 1 element on each step.

So in the general case, you are correct. In the specific case you used in your example, the teacher is correct.

Note: I am assuming that "step" refers to the application of the bubble-sort algorithm to a single pair of numbers in the set.

空城缀染半城烟沙 2024-12-28 09:13:19

冒泡排序涉及重复循环数组。在每次遍历数组的过程中,它都会重复交换当它碰到时无序的相邻元素。

仅当元素无序时,从一个条目到另一个条目的每一步才会交换。

每次通过数组都会修复至少一个无序对,除非没有(即,数组已经排序,并且通过没有变化的传递是完成信号)。

我怀疑你正在考虑步骤,而你的教授正在考虑传递,但无论如何他都不太正确,因为穿过整个数组的一些传递可以修复多个无序对,而最后一次传递什么也修复不了(因为没有任何东西)那时需要修复)。

Bubble sort involves looping through the array repeatedly. During each pass through the array, it repeatedly swaps adjacent elements that are out of order when it hits them.

Each step from one entry to another swaps only if the elements are out of order.

Each pass through the array will fix at least one out-of-order pair unless there are none (i.e., the array is already sorted, and getting through a pass with no change is the completion signal).

I suspect you're thinking of steps and your professor is thinking of passes, but he's not quite right anyway, as some passes through the entire array can fix more than one out-of-order pair and the last pass fixes nothing (since nothing needs fixing at that point).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文