I have an algorithm to compute a force set set using all bits between 0 and 2 ^ n:
public static <T> void findPowerSetsBitwise(Set<T> set, Set<Set<T>> results){
T[] arr = (T[]) set.toArray();
int length = arr.length;
for(int i = 0; i < 1<<length; i++){
int k = i;
Set<T> newSubset = new HashSet<T>();
int index = arr.length - 1;
while(k > 0){
if((k & 1) == 1){
newSubset.add(arr[index]);
}
k>>=1;
index --;
}
results.add(newSubset);
}
}
My question is: what is the running time of this algorithm. The loop runs 2 ^ n times, and at each iteration, the while loop starts lg (i) times. Therefore, I think work time
T(n) = the sum from i=0 to i=2^n of lg(i)
But I don’t know how to simplify this further, I know that it can be solved in O (2 ^ n) time (and not in space) recursively, so I wonder if the method is better or worse, since it is better in space .
source
share