简体   繁体   中英

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.

I am trying to find the k-th largest element in a supplied integer list with the above mentioned average time complexity.

   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 :

This implementation provides guaranteed log(n) time cost for the basic operations ( add , remove and contains ).

You are performing a ~log(n) operation n times, hence the complexity is indeed O(n log(n)).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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