使用 Arrays.sort 进行数组分析的性能
我有一个按以下方式使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
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, andm
is the length of eacharray[i]
, then you will, on each ofn^2
iterations, perform anO(m log m)
sort, so overall it'sO(n^2 (m log m))
(OrO(n^3 log n)
ifn == 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 sortarray[0]
, then in the inner for loop you sortarray[1]
througharray[n]
.Then when
a
is 1, you first sortarray[1]
, then in the inner for looparray[2]
througharray[n]
. But you already sorted all that, and it's not as if it will have changed in the interim.您运行 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).