【问题标题】:Bubble sort doesnt sort last number with this algorithm冒泡排序不使用此算法对最后一个数字进行排序
【发布时间】:2011-12-16 04:53:52
【问题描述】:

我遇到问题,此代码中的最后一个数字未排序。

   // This is the more advanced optimzed version of bubble sort
        int modifiedBubbleSortArray(int array[])
        {

        int swapped = 0;

         do {
            swapped = false;
            // We specify a loop here for the sorting
            for(int i=0;i<NUM_ARRAYS;i++)
            {
                // We specify aother loop here for the sorting
                for(int j=0;j< i - 1 /* <<< here we make less comparisns on each pass each time */ ;j++)
                {
                    // If the array i is better than j we enter this swap
                    if (array[j]<array[j+1])  
                    {
                        // We swap here for the functions
                        swap(array[j], array[j+1]);
                        // We measure the amount of times we swap
                        amountOfSwaps += 1;
                        // If we swapped we break out of the loop
                        swapped = true;
                    }
                }
            }
             } while (swapped);
        printArray(array);
        return amountOfSwaps;

        }

        // Here we swap values to sort them
        void swap(int & value1, int & value2)
        {
            // We specify a temp and use it to swap
            int temp=value1; 
            value1=value2;
            value2=temp;

        }

【问题讨论】:

    标签: c++ bubble-sort


    【解决方案1】:

    亲爱的朋友(神秘主义者)

    下面的代码是用java编写的, 但这是完美的,例如。冒泡排序算法。

        int[] nums = { 12 , 5 , 16 , 9 , 25 , 3 , 45 , 11 , 14 };
        int temp;
    
        for(int y = 1 ; y < nums.length - 1 ; y++) {
    
            for(int x = 0 ; x < nums.length - y ; x++) {
    
                if(nums[x] > nums[x+1]) {
    
                    temp = nums[x];
                    nums[x] = nums[x+1];
                    nums[x+1] = temp;
    
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      内部循环应该是:

      if (array[j]>array[j+1]) 
      { 
        // We swap here for the functions 
        swap(array[j], array[j+1]); 
        // We measure the amount of times we swap 
        amountOfSwaps += 1; 
      } 
      

      试一试,看看你的冒泡排序变体是否正确。

      要按降序排序,只需更改 if 条件即可:

      if (array[j]<array[j+1]) 
      

      另一个错误是内部 for 循环。将其更改为:

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

      更新(基于添加了交换测试的最后更改):

      bool swapped = false; 
      
      // We specify a loop here for the sorting 
      for(int i=0;i<NUM_ARRAYS;i++) 
      { 
          // We specify aother loop here for the sorting 
          for(int j=0;j<=i-1;++j)
          { 
              // If the array i is better than j we enter this swap 
              if (array[j]<array[j+1])   
              { 
                  // We swap here for the functions 
                  swap(array[j], array[j+1]); 
                  // We measure the amount of times we swap 
                  amountOfSwaps += 1; 
                  // If we swapped we break out of the loop 
                  swapped = true; 
              } 
          } 
          // If no swaps this iteration, break out
          if (!swapped)
              break;
      } 
      

      如果您真的想要一个好的排序,请查看introspection sort - 快速排序的一个变体。算法更复杂,但排序也更高效,是 O(N log N) 而不是冒泡排序的复杂度 O(N^2)。

      【讨论】:

      • if(array[i]&gt;array[j+1]) { // We swap here for the functions swap(array[i], array[j+1]); // We measure the amount of times we swap amountOfSwaps += 1; } 我认为您的意思是 i 在前两个中,但现在它不对第一个数字进行排序。
      • 不,我的意思正是我所说的。试一试。变量 i 应该只控制内部循环的迭代次数。变量 j 是真正负责重新排序的变量。
      • 好的 lmfao 这很有趣,让我试试。
      • 现在它按升序排序,我希望它从高到小,所以我应该更改 +1 对吗?
      • 我把它改成了if(array[j+1]&gt;array[j]) swap(array[j+1], array[j]); 但不是它仍然没有对最后一个数字进行排序..
      猜你喜欢
      • 2012-03-01
      • 2021-05-03
      • 1970-01-01
      • 2020-05-24
      • 2018-09-22
      • 2016-07-25
      • 2019-05-10
      • 2017-09-27
      • 2020-07-17
      相关资源
      最近更新 更多