【问题标题】:Bubble Sort Outperforming Selection Sort?冒泡排序优于选择排序?
【发布时间】:2015-09-16 15:35:03
【问题描述】:

根据我的阅读,尽管它们的大 Oh 表示法相同,但在对数组进行冒泡排序和选择排序时,随着数组大小的增长,选择排序的性能应该优于冒泡排序。

但在我的代码中,随着数组变大,冒泡排序始终优于选择排序。

在程序中,用户指定数组中的项目数,以及应该创建和排序包含这么多项目的数组的次数(以获得更准确的结果)。

这是我的代码:

import java.util.Scanner;
import java.util.Random;

public class SelectionSorting
{
  public static int[] arr;
  public static long runningSelectionTime, runningBubbleTime;

   public static void main(String[] args)
   {
     Scanner in = new Scanner(System.in);
     System.out.println("Please enter the number of the items in the array: ");
     int i = in.nextInt();
     System.out.println("Please enter the number of iterations: ");
     int iters = in.nextInt();
     arr = new int[i];
     for (int x = 0; x < iters; x++)
     {
       for (int n = 0; n < i; n++)
       {
         arr[n] = randomness();
        }
       long startSelectionTime = System.nanoTime();
       selectionSort(arr);
       long endSelectionTime = System.nanoTime();
       runningSelectionTime += endSelectionTime - startSelectionTime;
     }
     System.out.println("Selection Sort: ");
     System.out.println("Total running time: " + runningSelectionTime + " and the average is " + runningSelectionTime/iters);

    for (int x = 0; x < iters; x++)
    {
      for (int n = 0; n < i; n++)
      {
        arr[n] = randomness();
      }
      long startBubbleTime = System.nanoTime();
      bubbleSort(arr);
      long endBubbleTime = System.nanoTime();
      runningBubbleTime += endBubbleTime - startBubbleTime;
    }
    System.out.println("Bubble Sort: ");
    System.out.println("Total running time: " + runningBubbleTime + " and the average is " + runningBubbleTime/iters);
   }

   public static void selectionSort(int[] array)
   {
     for (int i = 0; i < array.length - 1; i++)
     {
       int iMin = i;
       for (int j = i + 1; j < array.length; j++)
       {
         if (array[j] < array[iMin]) 
         {
          iMin = j;
         }
       }
       if (iMin != i)
       {
         int temp = array[i];
         array[i] = array[iMin];
         array[iMin] = temp;
       }
     }
  }

   public static void bubbleSort(int[] arr)
   {
     int unsorted = arr.length;
     while (unsorted != 0) 
     {
       int lastSwap = 0;
       for (int i = 1; i < unsorted; i++)
       {
         if (arr[i - 1] > arr[i])
         {
           swap(arr, i, i - 1);
           lastSwap = i;
         }
       }
       unsorted = lastSwap;
     }
   }

   private static void swap(int[] arr, int a, int b)
   {
     int temp = arr[a];
     arr[a] = arr[b];
     arr[b] = temp;
   }

   public static int randomness()
   {
    Random rand = new Random();
    int random = rand.nextInt();
    if (random < 0)
    {
      random = random * -1;
    }
    do {
      random = random / 10;
    } while (random > 100);
    return random;
  }

}

如果尝试放入 500 和 1000,冒泡排序的运行时间会比选择排序短。

有趣的是,如果我去掉变量“iters”,那么结果与预期的一样。但是,似乎 iters 应该使运行时间更准确,而不是更少。

关于为什么会这样的任何想法?我做错什么了吗?冒泡排序是否有可能(始终)优于选择排序?

(为了防止混淆,我看到了Why is my Java based Bubble Sort Outperforming my Selection sort and my Insertion Sort?这个问题,但它并没有解决同样的问题。)

【问题讨论】:

  • Big Oh 对于不同的情况是不同的。冒泡排序在数据排序时效率最高Big Oh == n,在数据逆序时效率最低。
  • 如果您还没有这样做,请查看this post。问题可能出在您的基准测试方法中。
  • 你的冒泡排序不起作用
  • @BevynQ 我知道大哦对于不同的情况是不同的。但是,在这种情况下,冒泡排序始终比选择排序快,并且数组始终不同(它们不可能都是 O(n))。我知道不同的情况下不同的哦 - 我的问题是为什么在理论上它不应该时冒泡排序始终优于选择排序?
  • @BevynQ 感谢您指出这一点。我从我的文件中复制了错误的冒泡排序...抱歉。请查看我编辑的帖子。

标签: java arrays sorting big-o


【解决方案1】:

您的冒泡排序更快,因为它不起作用。

      arr[i] = temp;

应该是

      arr[i-1] = temp;

输出为 10000 * 3

选择排序:
总运行时间:216077472 平均为 72025824
冒泡排序:
总运行时间:402583050,平均134194350

输出为 500 * 1000

选择排序:
总运行时间:123621068 平均为 123621
冒泡排序:
总运行时间:317450183,平均317450

使用 jdk 7

【讨论】:

  • 啊...这将占很多...谢谢!我确实在我的代码中改变了这一点,但我仍然得到比选择排序更短的冒泡排序运行时间。你也一样吗?
  • 很抱歉再问一个问题...但是当您输入 500 和 1000 时是否相同?在那之前,选择排序似乎更好,但是一旦我使用这些数字,冒泡排序再次表现出色......
  • 我想我最好试试另一台机器...非常感谢您的帮助!
猜你喜欢
  • 2019-01-13
  • 2015-02-14
  • 2016-11-01
  • 2015-04-25
  • 2013-04-18
  • 1970-01-01
  • 2017-04-05
相关资源
最近更新 更多