你能帮我找到以下代码的Big-O吗:
/**
* This will:
* 1) Remove duplicates from the give List and sort.
* 2) find N-th largest element in the modified list and return the element.
* @param listWithDup
* @param index index of the largest element
* @return
*/
public static int findElementbyIndex(List<Integer> listWithDup, int index){
int toRet = 0, num = 0;
TreeSet<Integer> sortedSet = new TreeSet<Integer>(listWithDup); // remove duplicates and sorts
// System.out.println("printing the sorted List:");
// for(int i: sortedSet){
// System.out.println(i);
// }
Iterator<Integer> it = sortedSet.descendingIterator();
while(it.hasNext()){
toRet = it.next();
num++;
if(num == index)
break;
}
return toRet;
}
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(9);
a.add(5);
a.add(7);
a.add(2);
a.add(5);
System.out.println("Expecting 7, because 7 is 2nd largest element in the modified list="+findElementbyIndex(a, 2));
}
我从这段代码中得到的输出如下:
printing the sorted List:
1
2
5
7
9
Expecting 7, because 7 is 2nd largest element in the modified list=7
我需要计算 findElementbyIndex() 方法的平均复杂度。任何人都可以帮助我。
提前致谢