#Ruby 二分发查找defself.binary_searcha=[10,57,68,70,78,90,122]search=78index=0,start_index=0,end_index=a.lengthwhile(true)index=start_index+((end_index-start_index)/2)start_index=indexifa[index]<searchend_index=indexifa[index]>searchbreakifa[index]==searchendputs"find by "+index.to_send
//Java 二分发查找publicstaticvoidmain(String[]args){// sorted arrayint[]a={10,57,68,70,78,90,122};intsearch=78;intindex=0;intstart=0;intend=a.length;while(true){//get indexindex=start+((end-start)/2);if(a[index]<search){start=index;}elseif(a[index]>search){end=index;}else{break;}}System.out.println("find by "+index);}