question on find value in array

Posted by davit-datuashvili on Stack Overflow See other posts from Stack Overflow or by davit-datuashvili
Published on 2010-06-15T08:33:09Z Indexed on 2010/06/15 8:42 UTC
Read the original article Hit count: 160

Filed under:

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

© Stack Overflow or respective owner

Related posts about algorithm