Performance 我的代码的渐进运行时复杂性是多少?

Performance 我的代码的渐进运行时复杂性是多少?,performance,time-complexity,complexity-theory,Performance,Time Complexity,Complexity Theory,下面是我的代码,我试图找出代码的渐进运行时复杂性,但我不确定 public static int myAlgorithm(List<Integer> myList) { if (myList.isEmpty()) { return 0; } Collections.sort(myList); // Can assume the sort algorithm is merge sort int sum = 0; int max = myList.get(myLi

下面是我的代码,我试图找出代码的渐进运行时复杂性,但我不确定

public static int myAlgorithm(List<Integer> myList) {
 if (myList.isEmpty()) {
 return 0;
    } 
 
Collections.sort(myList); // Can assume the sort algorithm is merge sort 

int sum = 0; 
int max = myList.get(myList.size() - 1); 
for (int item : myList) {
 int diff = Math.abs(max - item);
 sum = sum + diff; 
    } 
return sum; 
} 
publicstaticintmyalgorithm(列表myList){
if(myList.isEmpty()){
返回0;
} 
Collections.sort(myList);//可以假定排序算法为合并排序
整数和=0;
int max=myList.get(myList.size()-1);
用于(整型项目:myList){
int diff=数学绝对值(最大值-项目);
总和=总和+差额;
} 
回报金额;
} 

假设您使用的排序是合并排序,则算法的无症状运行时间为O(NlogN)

最复杂的步骤是排序步骤


之后的代码约为O(N);查找最大值时,1个循环和1个直通。

无需假设;应该记录
集合的复杂性。排序
。请注意,您的函数效率较低。排序是查找集合中最大项目的糟糕方法。您可以发现,只需在O(n)时间内扫描列表,而不是在O(n)时间内排序。