对冒泡排序方法感到好奇

发布于 2024-12-12 06:10:20 字数 788 浏览 1 评论 0原文

我在这里创建了一个简单的冒泡排序脚本,它接受数组并对它们进行排序,这只是代码片段。但它是对其进行排序的代码。我想做到这一点,而不是在每次通过时进行九次左右的比较,而是修改冒泡排序以在第二次通过时进行八次或一次更少的比较,在第三次通过时进行七次比较,依此类推。我完全不知道如何实现它。最好的主意是什么?

        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 技术交流群。

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

发布评论

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

评论(3

第几種人 2024-12-19 06:10:20

您的代码已经在做您正在寻找的事情。由于 j 迭代到长度 i,因此它每次都会增大一。我认为您很困惑,因为您的代码实际上是从问题中的英语向后实现的;)

这是一个示例数组,以及在每次迭代时进行的修改。括号表示在每次迭代中检查数组的哪一部分:

(7)5 3 8 6 9 4 2 0 1
(7 5)3 8 6 9 4 2 0 1
(7 5 3)8 6 9 4 2 0 1
(8 7 5 3)6 9 4 2 0 1
(8 7 6 5 3)9 4 2 0 1
(9 8 7 6 5 3)4 2 0 1
(9 8 7 6 5 4 3)2 0 1
(9 8 7 6 5 4 3 2)0 1
(9 8 7 6 5 4 3 2 0)1
(9 8 7 6 5 4 3 2 1 0)

正如您第一次看到的那样,实际上什么也没做,也永远不会做,因为您正在将一个元素与其自身进行比较。第二次时,您现在比较两个元素,然后是三个,然后依此类推。

为了让你的代码从比较它们开始,然后每次少做一个(正如你的问题所述),你可以将循环修改为以下内容(注意 j<10-i):

    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10-i;j++)
        {

无论哪种方式,它都相当于相同的事情并且将工作到底。您可以通过设置 i = 1 进一步跳过与自身的第一次比较:

for(int i=1;i<10;i++)
    {
        for(int j=0;j<10-i;j++)
        {

这将省略上面的第一次比较,这是您正在寻找的其他优化。

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:

(7)5 3 8 6 9 4 2 0 1
(7 5)3 8 6 9 4 2 0 1
(7 5 3)8 6 9 4 2 0 1
(8 7 5 3)6 9 4 2 0 1
(8 7 6 5 3)9 4 2 0 1
(9 8 7 6 5 3)4 2 0 1
(9 8 7 6 5 4 3)2 0 1
(9 8 7 6 5 4 3 2)0 1
(9 8 7 6 5 4 3 2 0)1
(9 8 7 6 5 4 3 2 1 0)

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):

    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10-i;j++)
        {

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:

for(int i=1;i<10;i++)
    {
        for(int j=0;j<10-i;j++)
        {

This will leave off the first comparison above, which is the other optimization you were looking for.

谁对谁错谁最难过 2024-12-19 06:10:20

我认为您的 j 循环有点向后。我认为你需要类似的东西:

for (int i = 0; i < 10; i++) {
    for (int j = 1; j < 10 - i; j++) {
        // ...
    }
}

I think you've got your loop on j a bit backwards. I think you need something like:

for (int i = 0; i < 10; i++) {
    for (int j = 1; j < 10 - i; j++) {
        // ...
    }
}
三生一梦 2024-12-19 06:10:20

请注意,冒泡排序的一个显着特性是它检查每次传递是否都执行了交换。如果它没有交换,那么一切都井然有序,你可以提前休息。像这样的事情:

int bubbleSort(int *arry, size_t size)
{
  bool swapped;
  size_t upper_bound = size;
  int amountOfSwaps = 0;

  do
  {
    swapped = false;
    for(int i = 1; i < upper_bound; ++i)
    {
      if(arry[i] < arry[i - 1])
      {
        swap(arry[i], arry[i - 1]);
        swapped = true;
        ++amountOfSwaps;
      }
    }
    --upper_bound;
  }while(swapped);

  return amountOfSwaps;
}

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:

int bubbleSort(int *arry, size_t size)
{
  bool swapped;
  size_t upper_bound = size;
  int amountOfSwaps = 0;

  do
  {
    swapped = false;
    for(int i = 1; i < upper_bound; ++i)
    {
      if(arry[i] < arry[i - 1])
      {
        swap(arry[i], arry[i - 1]);
        swapped = true;
        ++amountOfSwaps;
      }
    }
    --upper_bound;
  }while(swapped);

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