0

Their purpose is to return the number of comparisons between the keys and the array items. Please let me know if there is anything that I should change, as I am new to Java and am not yet fully familiar with best practices.

public class BinaryVsLinear {

private static int linearSearch(int key, int[] array){
  int count = 0;
  for (int i = 0; i < array.length; i++){
      count++;
      if (array[i] == key){
          i += array.length +1;
      }  
  }
  return count;
}

private static int binarySearch(int key, int[] array){

  int count = 0, l = 0, r = array.length -1;
  while (l <= r){
        int m = (l+r)/2;
        count++;
        if (array[m] == key){
            return count;
        }
        count++;
        if (array[m] < key){
            l = m + 1;

        }
        else{
            r = m - 1;
        }
    }
    return count;
}
2
  • Is the input to both sorted? Your binary search will only work on sorted data, and the linear search can be sped up if it's working on sorted data (try returning when array[i]>key) Commented Oct 24, 2017 at 17:17
  • Yes, input to both is sorted. Commented Oct 24, 2017 at 17:21

1 Answer 1

1

Your code is correct, i.e., it counts how many comparisons will be executed both for linear and binary searches. As you is a novice, I would recommend some better practices when writing code, take a look.

public class BinaryVsLinear {

    private static int linearSearch( int key, int[] array ) {

        int count = 0;

        for ( int i = 0; i < array.length; i++ ){
            count++;
            if ( key == array[i] ){
                break;
            }  
        }

        return count;

    }

    private static int binarySearch( int key, int[] array ) {

        // one variable per line
        // use better names
        int count = 0;
        int left = 0;
        int right = array.length -1;

        while ( left <= right ){

            int middle = ( left + right ) / 2;

            count++;
            if ( array[middle] == key ){
                return count;
            }

            count++;
            if ( key > array[middle] ){
                left = middle + 1;
            } else{
                right = middle - 1;
            }

        }

        return count;

    }

}

I added some spaces, change some variable names to better names, etc. It is a matter of preference, but you must always pay attention to the readability of your code.

Sign up to request clarification or add additional context in comments.

6 Comments

Personal preference / company policy will dictate this, but personally I strongly dislike breaks and multiple returns. This article 'Single-entry, single-exit (SESE) heuristic' by Ian Joyner sums up my thoughts on this.
@d.j.brown Personal preference / company policy will dictate this yep
@d.j.brown how would you recommend the break be changed?
I like the better variable names, but I fail to see how this even approaches the question of working correctly. Please update to include something about correctness.
@EdwinBuck His code is correct for what it aims to do. I just modify his code to show better practices, since he said that he is new to Java and I am not yet fully familiar with best practices.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.