question on find value in array
- by davit-datuashvili
i have seen a few days ago such problem
there is given two array find elements which are common of these array
one of the solution was sort big array and then use binary search algorithm
and also there is another algorithm- brute-force algorithm
for (int i=0;i<array1.length;i++){
for (int j=0;j<array2.length;j++){
if (array1[i]==array2[j]){
//code here
}
}
it's complexity is O(array1.length*array2.length);
and i am interested the first method's complexity is also same yes?
because we should sort array first and then use search method
binary search algorithm's complexity is log_2(n) so it means that total time will be
array.length*log_2(n) and about sort?
please explain me which is better