当前位置: 首页 > 知识库问答 >
问题:

>vs.>=在气泡排序中导致显著的性能差异

穆丁雨
2023-03-14
import java.util.Random;

public class BubbleSortAnnomaly {

    public static void main(String... args) {
        final int ARRAY_SIZE = Integer.parseInt(args[0]);
        final int LIMIT = Integer.parseInt(args[1]);
        final int RUNS = Integer.parseInt(args[2]);

        int[] a = new int[ARRAY_SIZE];
        int[] b = new int[ARRAY_SIZE];
        Random r = new Random();
        for (int run = 0; RUNS > run; ++run) {
            for (int i = 0; i < ARRAY_SIZE; i++) {
                a[i] = r.nextInt(LIMIT);
                b[i] = a[i];
            }

            System.out.print("Sorting with sortA: ");
            long start = System.nanoTime();
            int swaps = bubbleSortA(a);

            System.out.println(  (System.nanoTime() - start) + " ns. "
                               + "It used " + swaps + " swaps.");

            System.out.print("Sorting with sortB: ");
            start = System.nanoTime();
            swaps = bubbleSortB(b);

            System.out.println(  (System.nanoTime() - start) + " ns. "
                               + "It used " + swaps + " swaps.");
        }
    }

    public static int bubbleSortA(int[] a) {
        int counter = 0;
        for (int i = a.length - 1; i >= 0; --i) {
            for (int j = 0; j < i; ++j) {
                if (a[j] > a[j + 1]) {
                    swap(a, j, j + 1);
                    ++counter;
                }
            }
        }
        return (counter);
    }

    public static int bubbleSortB(int[] a) {
        int counter = 0;
        for (int i = a.length - 1; i >= 0; --i) {
            for (int j = 0; j < i; ++j) {
                if (a[j] >= a[j + 1]) {
                    swap(a, j, j + 1);
                    ++counter;
                }
            }
        }
        return (counter);
    }

    private static void swap(int[] a, int j, int i) {
        int h = a[i];
        a[i] = a[j];
        a[j] = h;
    }
}
Sorting with sortA: 4.214 seconds. It used  564960211 swaps.
Sorting with sortB: 2.278 seconds. It used 1249750569 swaps.
Sorting with sortA: 4.199 seconds. It used  563355818 swaps.
Sorting with sortB: 2.254 seconds. It used 1249750348 swaps.
Sorting with sortA: 4.189 seconds. It used  560825110 swaps.
Sorting with sortB: 2.264 seconds. It used 1249749572 swaps.
Sorting with sortA: 4.17  seconds. It used  561924561 swaps.
Sorting with sortB: 2.256 seconds. It used 1249749766 swaps.
Sorting with sortA: 4.198 seconds. It used  562613693 swaps.
Sorting with sortB: 2.266 seconds. It used 1249749880 swaps.
Sorting with sortA: 4.19  seconds. It used  561658723 swaps.
Sorting with sortB: 2.281 seconds. It used 1249751070 swaps.
Sorting with sortA: 4.193 seconds. It used  564986461 swaps.
Sorting with sortB: 2.266 seconds. It used 1249749681 swaps.
Sorting with sortA: 4.203 seconds. It used  562526980 swaps.
Sorting with sortB: 2.27  seconds. It used 1249749609 swaps.
Sorting with sortA: 4.176 seconds. It used  561070571 swaps.
Sorting with sortB: 2.241 seconds. It used 1249749831 swaps.
Sorting with sortA: 4.191 seconds. It used  559883210 swaps.
Sorting with sortB: 2.257 seconds. It used 1249749371 swaps.
Sorting with sortA: 3.983 seconds. It used  625941897 swaps.
Sorting with sortB: 4.658 seconds. It used  789391382 swaps.
#include <cstdlib>
#include <iostream>

#include <omp.h>

#ifndef ARRAY_SIZE
#define ARRAY_SIZE 50000
#endif

#ifndef LIMIT
#define LIMIT 10
#endif

#ifndef RUNS
#define RUNS 10
#endif

void swap(int * a, int i, int j)
{
    int h = a[i];
    a[i] = a[j];
    a[j] = h;
}

int bubbleSortA(int * a)
{
    const int LAST = ARRAY_SIZE - 1;
    int counter = 0;
    for (int i = LAST; 0 < i; --i)
    {
        for (int j = 0; j < i; ++j)
        {
            int next = j + 1;
            if (a[j] > a[next])
            {
                swap(a, j, next);
                ++counter;
            }
        }
    }
    return (counter);
}

int bubbleSortB(int * a)
{
    const int LAST = ARRAY_SIZE - 1;
    int counter = 0;
    for (int i = LAST; 0 < i; --i)
    {
        for (int j = 0; j < i; ++j)
        {
            int next = j + 1;
            if (a[j] >= a[next])
            {
                swap(a, j, next);
                ++counter;
            }
        }
    }
    return (counter);
}

int main()
{
    int * a = (int *) malloc(ARRAY_SIZE * sizeof(int));
    int * b = (int *) malloc(ARRAY_SIZE * sizeof(int));

    for (int run = 0; RUNS > run; ++run)
    {
        for (int idx = 0; ARRAY_SIZE > idx; ++idx)
        {
            a[idx] = std::rand() % LIMIT;
            b[idx] = a[idx];
        }

        std::cout << "Sorting with sortA: ";
        double start = omp_get_wtime();
        int swaps = bubbleSortA(a);

        std::cout << (omp_get_wtime() - start) << " seconds. It used " << swaps
                  << " swaps." << std::endl;

        std::cout << "Sorting with sortB: ";
        start = omp_get_wtime();
        swaps = bubbleSortB(b);

        std::cout << (omp_get_wtime() - start) << " seconds. It used " << swaps
                  << " swaps." << std::endl;
    }

    free(a);
    free(b);

    return (0);
}

此程序显示了相同的行为。有人能解释一下这里到底是怎么回事吗?

首先执行sortb然后执行sortta不会更改结果。

共有1个答案

平和雅
2023-03-14

我想可能确实是因为分支预测的缘故。如果您将交换的数量与您发现的内部排序迭代的数量进行比较:

限值=10

  • A=560M交换/1250M循环
  • B=1250M交换/1250M循环(交换比循环少0.02%)
    null
 类似资料:
  • 嗨,我用冒泡排序查看了其他帖子,但解决方案在我的例子中不起作用:所以算法在我循环时重复了几次之后才起作用。但我如何在不使用输入的情况下做到这一点?这是我的代码,你知道我的意思:

  • 本文向大家介绍气泡排序在Go Lang,包括了气泡排序在Go Lang的使用技巧和注意事项,需要的朋友参考一下 冒泡排序是一种排序算法,它通过交换顺序错误的元素来工作。在多次遍历中,它检查相邻元素的顺序是否正确(递增)。 冒泡排序的时间复杂度为O(n ^ 2),因为它需要两个嵌套循环才能检查相邻元素。 例如,让我们看下面的未排序数组- 冒泡排序算法首先遍历整个数组,然后在另一个循环中检查相邻元素是

  • 我需要使用气泡排序法按名称对我的杂货库存进行排序。 显然,我的代码不是按名字排序的 顺便说一句,库存存储的数据来自文件输入。 这是我的代码。 这是我的比较方法从我的超类(杂货店项目)

  • 所以我的代码有问题。我必须编写一个程序来对一个外部文本文件进行排序(基本上是一个按随机顺序排列的随机数列表)。所以我试着按照教授的步骤去做,即使我做了,我也没有得到正确的输出。输出应该如下所示: 1 2 2 2 3 3 5 5 6 6 11 13 13 13 13 16 17 17 19 25 27 27 33 34 37 37 43 45 49 51 52 54 57 58 60 63 64 6

  • 我是Java新手,所以一直在尝试将一些旧的JS练习翻译成Java。

  • 我试图在sortBabies中按字母顺序排序婴儿,但我无法交换位置,因为错误出现为“ArrayList类型中的方法集(int, Baby)不适用于参数(int, string)”。我将名称1和名称2设置为字符串值,以便我能够比较,但现在我无法交换位置。