使用 Arrays.sort 进行数组分析的性能

发布于 2025-01-01 08:34:24 字数 775 浏览 4 评论 0原文

我有一个按以下方式使用 Arrays.sort(char[]) 的代码:

    void arrayAnalysis(String[] array){
      for(int a=0; a<array.length;a++){
        char[] letters = array[a].toCharArray();
        Arrays.sort(letters);
        ...
        for(int b=a+1; b<array.length;b++){
          char[] letters2 = array[b].toCharArray();
          Arrays.sort(letters2);

          if(Arrays.equals(letters, letters2)
            print("equal");
        }
      }
    }

在本例中,n 等于数组大小。由于嵌套的 for 循环,性能自动为 O(n^2)。但是,我认为 Arrays.sort (使用 O(nlog(n)))也会影响性能并使其比 O(n^2) 更糟糕。这种想法正确吗?

最终的性能会是 O(n*nlog(n)*(n*nlog(n)) 吗?还是我离题太远了?

谢谢。

编辑:我应该补充一点,虽然 n 与数组大小有关,但 Arrays.sort 是如果应该将其添加到性能分析中,这是我感到困惑的一部分。

Edit2:如果反对者留下评论来说明为什么它被认为是不好的,那就太酷了。问题。

I have a code that uses Arrays.sort(char[]) in the following manner:

    void arrayAnalysis(String[] array){
      for(int a=0; a<array.length;a++){
        char[] letters = array[a].toCharArray();
        Arrays.sort(letters);
        ...
        for(int b=a+1; b<array.length;b++){
          char[] letters2 = array[b].toCharArray();
          Arrays.sort(letters2);

          if(Arrays.equals(letters, letters2)
            print("equal");
        }
      }
    }

In this case, n is equal to the array size. Due to the nested for loops, performance is automatically O(n^2). However, I think Arrays.sort (with O(nlog(n))) also affects the performance and makes it worse than O(n^2). Is this thinking correct?

Would the final performance be O(n*nlog(n)*(n*nlog(n))? Or am I way off?

Thanks.

Edit: I should add that while n is related to the array size, Arrays.sort is working with the number of letters in the array element. That is part of my confusion if this should be added to the performance analysis.

Edit2: It would be cool if the down-voter left a comment as to why it was deemed as a bad question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

内心旳酸楚 2025-01-08 08:34:24

如果 n 是数组的长度,m 是每个 array[i] 的长度,那么您将在每个 < code>n^2 次迭代,执行 O(m log m) 排序,因此总体来说是 O(n^2 (m log m)) (或 O(n^3 log n) 如果 n == m. [编辑:现在我想得更多了,你的猜测是正确的,这是错误的复杂性,但我下面所说的仍然是正确的!]]

但这并不是真正必要的。您可以创建数组的排序副本,然后使用该副本执行嵌套 for 循环,看看当 a 为 0 时会发生什么:首先对 array[0],然后在内部 for 循环中进行排序array[1]array[n]

然后,当 a 为 1 时,首先对 array[1] 进行排序。 >,然后在内部 for 循环 array[2]array[n] 中。但你已经对所有这些进行了排序,并且它不是这样的。是否在此期间发生了变化。

If n is the length of the array, and m is the length of each array[i], then you will, on each of n^2 iterations, perform an O(m log m) sort, so overall it's O(n^2 (m log m)) (Or O(n^3 log n) if n == m. [EDIT: now that I think more about this, your guess is right, and this is the wrong complexity. But what I say below is still correct!]]

This is not really necessary, though. You could just make a sorted copy of the array, and do your nested for-loop using that one. Look at what happens when a is 0: first you sort array[0], then in the inner for loop you sort array[1] through array[n].

Then when a is 1, you first sort array[1], then in the inner for loop array[2] through array[n]. But you already sorted all that, and it's not as if it will have changed in the interim.

旧梦荧光笔 2025-01-08 08:34:24

您运行 n 个外循环,每个循环运行 n 个内循环,每个循环调用 O(n log n)算法,因此最终结果(级别之间没有任何交互)为 O(n3 log n)。

You run n outer loops, each of which runs n inner loops, each of which calls an O(n log n) algorithm, so the final result — absent any interaction between the levels — is O(n3 log n).

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