I have a Scenario where I have to set a range of BitSet index to 1. So if I use
/*
*Code snippet
*/
BitSet myBitSet = new BitSet(100);
myBitSet.set(10, 50);
//**************************
What would be the time complexity for above code? will it iterate through 40 elements or some kind of bit operation will be performed?
For a single bit it will be O(1), the complexity for setting n bits is O(N).
For the sceptics: setting n bits is O(N), because setting 10_000 bits takes about 10 times longer than setting 1_000 bits.
That said, it is more effecient to call myBitSet.set(10,50)
than to write for (int i=10; i<=50; i++) myBitSet.set(i);
For some reason it isn't specified in the Javadoc, but the Oracle implementation is certainly O(1) for get, set, flip, and clear with one parameter; O(N) with two parameters. 'Vector of bits' may be intended as a hint.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With