Let's say I have n number of records(key, value).
Now I want to look for x number of keys for getting their values.
Clearly seeing that if x is small, binary search is more efficient than looping through all of the records to look for the correct key. BinarySearch is a part of Arrays in Java 8. Given that my array is already sorted before performing search.
So my time complexity is O(XlogN) + O(X)
Explains:
- O(logN) is complexity of binary search
- O(X) is complexity of getting value action
- O(XlogN) dues to the number of records I want to look for
But at some point, if x becomes too big (close to value of n), it seems that just perform 1 single loop through all of my records, then comparing with all the keys I need to get values out is more efficient...
for(Record record : records){
//Since the columns that I look for start with a specific prefix.
//This one is one of the factor that makes me confused
//when checking the performance.
if(record.key.startWith(key-family){
switch(record.key){
case key 0:
getvalue
break;
.......
case key x:
getvalue
break;
}
}
}
For this solution, my complexity is O(N) + O(NX)
Explains:
- O(N) is complexity of the for loop
- O(NX) is complexity of switch statement and getting value out (Worst case scenario) until the looping is done.
Given that the appreance of record's key is not uniform. Some record collection will have a significant different in number of keys that start with my key-family, compares to other record collection.
My questions are:
How should we define when x is too big and makes binary search solution become inefficiency?
Is there something that I can learn from you? :)
Thank you.