简体   繁体   English

这是什么类型的排序算法?

[英]What type of Sorting algorithm is this?

If this is Bubble sort then what is this ? 如果是冒泡排序又是什么这个

Do you see the placement of Swap()? 您看到Swap()的位置了吗?

这是一个选择排序

The first list is indeed selection sort. 第一个列表确实是选择排序。 It is in essence the same as the algorithm on the link you've provided. 从本质上讲,它与您提供的链接上的算法相同。 But instead of finding the element that has the minimum value, and swapping it with arr[i] once after the j loop, the first code immediately swaps arr[i] with any value it encounters which is smaller. 但是,没有找到具有最小值的元素,而是在j循环之后将其与arr[i]交换了一次,而是第一个代码立即将arr[i]与它遇到的任何较小的值交换。

In both cases, at the end of the i loop, arr[i] will contain the smallest element in a within the range i+1..SIZE . 在这两种情况下,在i循环的末尾, arr[i]都将在i+1..SIZE范围内包含a中的最小元素。

There are two differences between the two algorithms: the code you show here performs more than one swap per iteration, and it shuffles the data that is not yet sorted (this is not really important, as they will eventually get sorted). 两种算法之间有两个区别:您在此处显示的代码每次迭代执行多个交换,并且对尚未排序的数据进行混洗(这并不重要,因为它们最终将被排序)。 So, basically it is less efficient than the code you've linked. 因此,基本上,它的效率比链接的代码低。

It's a kind of Selection Sort (as Maciej Hehl already said), but very ineffective. 这是一种选择排序(正如Maciej Hehl所说的那样),但是效果很差。 You swap way to many times. 您交换方式很多次。 The effect is that you swap witch the minimum, but on the way there swap with each other that is smaller than the number you are looking at. 效果是您交换了最少的巫婆,但在交换的途中,交换的数目小于您要查看的数目。 That is unnecessary. 那是没有必要的。

Maybe I am missing something subtle, but the first link (lorenzod8n.wordpress.com) shows a bubble sort; 也许我缺少一些细微的东西,但是第一个链接(lorenzod8n.wordpress.com)显示了气泡排序; the second (cprogramminglanguage.net) a selection sort. 第二个(cprogramminglanguage.net)选择排序。 It even says so in the text. 甚至在文本中也是如此。

Edit: I missed/forgot something subtle: Bubble Sort exchanges adjacent entries; 编辑:我错过/忘记了一些细微的东西:气泡排序交换相邻的条目; the algorithm in the first link doesn't. 第一个链接中的算法没有。 Hence, it's not Bubble Sort, though it does has Bubble Sort's excessive swapping behavior. 因此,它不是冒泡排序,尽管它确实有冒泡排序的过度交换行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM