java - Feedback on my Binary Search Algorithm -


i have written binary search algorithm, seems bit different other peoples i've seen. hoping community give me feedback whether or not i'm missing something, or doing wrong way.

binary search:

import java.util.arrays;   public class binarysearch {      public int[] numbers = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};       /**      * @param num      * @param key      *       * function recursively searches sorted array of integers, finding specific number (key).      * search looks @ midpoint of array, checking see if midpoint number being sought,      * if not, depending of whether sought number greater than, or less than, midpoint      * function copies upper, or lower, half of array , passes recursive       * function call.      *       */     public int performsearch(int[] num, int key){         if(num.length == 0){             system.out.println("array empty");              return 0;          }else{             int mid;              int number=0;              mid = (num.length)/2;              if(key == num[mid]){                 number =  num[mid];                  system.out.println("found number " + number);                  return number;              }else if((key < num[mid]) && num.length > 1){                 num = arrays.copyofrange(num, 0, mid);                  system.out.println("low range: " + arrays.tostring(num));                  return performsearch(num, key);              }else if((key > num[mid]) && num.length > 1){                 num = arrays.copyofrange(num, mid, num.length);                  system.out.println("high range: " + arrays.tostring(num));                  return performsearch(num, key);              }else{                 system.out.println("number not exist in array.");                  return 0;              }             //return number;          }      }      /**      * @param args      */     public static void main(string[] args) {         int key = 22;          binarysearch bs = new binarysearch();          int index = bs.performsearch(bs.numbers, key);          system.out.println("number " + index);     }  } 

this implementation inefficient. problem line:

num = arrays.copyofrange(num, 0, mid);  

it creating copy of array, takes long time.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -