简体   繁体   English

在Java中使用Set / TreeSet的平均时间复杂度是多少?

[英]what is the average time complexity of using Set/TreeSet in java?

Does the below implementation using Set interface/TreeSet class in java satisfy the O(N*log(N)) average time complexity in Big-O notation. 使用Java中的Set interface / TreeSet类的以下实现是否满足Big-O表示法中的O(N * log(N))平均时间复杂度。

I am trying to find the k-th largest element in a supplied integer list with the above mentioned average time complexity. 我试图在提供的整数列表中找到具有上述平均时间复杂度的第k个最大元素。

   public class JavaBuiltInSort {

       public static void removeDupInIntArray(int[] ints, int k) {
           Set<Integer> setString = new TreeSet<Integer>(Collections.reverseOrder());
           for (int i = 0; i < ints.length; i++) {
               setString.add(ints[i]);
           }
           System.out.println(setString);
           Integer[] array = setString.toArray(new Integer[0]);
           System.out.println("The kth largest element in the list is "+ array[k-1]);
       }

       public static void main(String[] args) {
           int[] arr = { 8, 9, 4, 5, 2, 1, 6, 5, 7, 9, 5, 4, 8, 6, 3, 1, 2, 5, 4, 7, 8 };
           int k = 5;
           JavaBuiltInSort.removeDupInIntArray(arr,k);
       }
   }

From TreeSet : 来自TreeSet

This implementation provides guaranteed log(n) time cost for the basic operations ( add , remove and contains ). 此实现为基本操作( addremovecontains )提供了有保证的log(n)时间成本。

You are performing a ~log(n) operation n times, hence the complexity is indeed O(n log(n)). 您正在执行~log(n)操作n次,因此复杂度确实为O(n log(n))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM