对冒泡排序方法感到好奇
我在这里创建了一个简单的冒泡排序脚本,它接受数组并对它们进行排序,这只是代码片段。但它是对其进行排序的代码。我想做到这一点,而不是在每次通过时进行九次左右的比较,而是修改冒泡排序以在第二次通过时进行八次或一次更少的比较,在第三次通过时进行七次比较,依此类推。我完全不知道如何实现它。最好的主意是什么?
int bubbleSortArray(int array[])
{
for(int i=0;i<10;i++)
{
for(int j=0;j<i;j++)
{
if(array[i]>array[j])
{
swap(array[i], array[j]);
amountOfSwaps += 1;
}
}
}
printArray(array);
return amountOfSwaps;
}
void swap(int & value1, int & value2)
{
int temp=value1;
value1=value2;
value2=temp;
}
I created a simple bubblesorting script here that takes in arrays and sorts them, This is only a snippet of the code. But its the code that sorts it. I want to make it so Instead of making nine or so comparisons for example on every pass, to modify the bubble sort to make eight or one less comparisons on the second pass, seven on the third pass, and so on. I am totally lost how to implement that. What would be the best idea for that?
int bubbleSortArray(int array[])
{
for(int i=0;i<10;i++)
{
for(int j=0;j<i;j++)
{
if(array[i]>array[j])
{
swap(array[i], array[j]);
amountOfSwaps += 1;
}
}
}
printArray(array);
return amountOfSwaps;
}
void swap(int & value1, int & value2)
{
int temp=value1;
value1=value2;
value2=temp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码已经在做您正在寻找的事情。由于 j 迭代到长度 i,因此它每次都会增大一。我认为您很困惑,因为您的代码实际上是从问题中的英语向后实现的;)
这是一个示例数组,以及在每次迭代时进行的修改。括号表示在每次迭代中检查数组的哪一部分:
正如您第一次看到的那样,实际上什么也没做,也永远不会做,因为您正在将一个元素与其自身进行比较。第二次时,您现在比较两个元素,然后是三个,然后依此类推。
为了让你的代码从比较它们开始,然后每次少做一个(正如你的问题所述),你可以将循环修改为以下内容(注意 j<10-i):
无论哪种方式,它都相当于相同的事情并且将工作到底。您可以通过设置 i = 1 进一步跳过与自身的第一次比较:
这将省略上面的第一次比较,这是您正在寻找的其他优化。
Your code is already doing what you are looking for. Since j iterates to the length i, it grows by one larger each time. I think you are being confused because your code is actually implementing it backwards from your English in the question ;)
Here is a sample array, and the modifications that would be made at each iteration. The parenthesis denote what part of the array is being checked in each iteration:
As you can see the first time through nothing is actually done, nor ever will be because you are comparing one element against itself. The second time through you are now comparing two elements, then three, then so on.
To make your code start with comparing them all, and then doing one less each time (as your question states) you would modify you loop to the following (note j<10-i):
Either way it amounts to the same thing and will work in the end. You could further skip the first comparison against itself by setting i = 1:
This will leave off the first comparison above, which is the other optimization you were looking for.
我认为您的
j
循环有点向后。我认为你需要类似的东西:I think you've got your loop on
j
a bit backwards. I think you need something like:请注意,冒泡排序的一个显着特性是它检查每次传递是否都执行了交换。如果它没有交换,那么一切都井然有序,你可以提前休息。像这样的事情:
Note that a distinquishing property of a bubblesort is that it checks whether a swap was performed at all for each pass. If it didn't swap then everything is in order and you can break early. Something like this: