JavaʵÏÖ ¶þ·Ö²éÕÒ
/**
* ʹÓöþ·Ö²éÕҵķ½Ê½²éѯָ¶¨µÄÖµ
* @author ZhangYu
* @data 2010-01-23
*/
public class BinSearch {
/**
* ÓõݹéʵÏÖ¶þ·Ö²éÕÒ
* @param data -±»²éÕÒµÄÊý×é
* @param value -Òª²éÕÒµÄÖµ
* @param left -²éÕÒ·¶Î§µÄ×îСֵ
* @param right -²éÕÒ·¶Î§µÄ×î´óÖµ
* @return ·µ»Ø²éÕÒµÄϱ꣬ûÓвéÕÒµÄÖµ·µ»Ø-1
*/
public int search(int[] data ,int value ,int left ,int right){
int mid = (right-left)/2 + left; //µ±Ç°±È½ÏÖµµÄϱê
/*
* Í˳öÌõ¼þ
*/
if(left > right){
return -1;
}
if(value == data[mid]){
return mid;
}else if(value > data[mid]){
return search(data ,value ,mid +1 ,right); //ÉèÖÃеÄ×îС·¶Î§
}else if(value < data[mid]){
return search(data, value, left, mid - 1); //ÉèÖÃеÄ×î´ó·¶Î§
}
return -1;
}
/**
* ÓÐÑ»·µÄ·½Ê½ÊµÏÖ¶þ·Ö²éÕÒ
* @param data -±»²éÕҵĶÔÏñ
* @param value -Òª²éÕÒµÄÖµ
* @return ·µ»Ø²éÕÒµÄϱ꣬ûÓвéÕÒµÄÖµ·µ»Ø-1
*/
public int search(int []data ,int value){
int left = 0; //²éÕÒ·¶Î§µÄ×îСֵ
int right = data.length - 1; //²éÕÒ·¶Î§µÄ×î´óÖµ
while(left <= right){
int mid = (right-left)/2 + left; //µ±Ç°±È½ÏÖµµÄϱê
if(value == data[mid]){
return mid;
}else if(value > data[mid]){
left = mid+1; //ÉèÖÃеÄ×îС·¶Î§
}else if(value < data[mid]){
right = mid-1; //ÉèÖÃеÄ×î´ó·¶Î§
}
}
return -1;
}
public static void main(String []args){
BinSearch bs = new BinSearch();
int [] data = {1,5,7,9,15,16,20,25,28,30,38};
System.out.println(bs.search(data, 5, 0, data.length-1));
System.out.println(bs.search(data, 7));
}
}
Ïà¹ØÎĵµ£º
/*
* @(#)MemoryMonitor.java 1.3 05/11/17
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are ......
1. ¼òµ¥ÀàÐÍÊÇ°´Öµ´«µÝµÄ
¡¡¡¡Java ·½·¨µÄ²ÎÊýÊǼòµ¥ÀàÐ͵Äʱºò£¬ÊÇ°´Öµ´«µÝµÄ (pass by value)¡£ÕâÒ»µãÎÒÃÇ¿ÉÒÔͨ¹ýÒ»¸ö¼òµ¥µÄÀý×ÓÀ´ËµÃ÷£º
/* Àý 1 */
/**
* @(#) Test.java
* @author fancy
*/
public class Test {
public static void test(boolean test) {
&nb ......
±¾×÷Æ·²ÉÓÃ֪ʶ¹²ÏíÊðÃû-·ÇÉÌÒµÐÔʹÓÃ-Ïàͬ·½Ê½¹²Ïí 2.5 Öйú´ó½Ðí¿ÉÐÒé½øÐÐÐí¿É¡£
JavaÖеıäÁ¿Óë¶ÔÏóÓÐÇø±ðÂð£¿
Òý×Ó£º±äÁ¿Óë¶ÔÏó
±äÁ¿ÊÇJavaÖÐ×î»ù±¾µÄ´æ´¢µ¥Ôª£¬Îª±äÁ¿¸³Öµ¿ÉÒÔʹÓø³Öµ±í´ïʽ¡£È磺
int i = 10;
¸Ã±í´ïʽµÄº¬ÒåÊǽ«Ò»¸ö×ÖÃæÁ¿£¨literal£©10¸³Öµ¸øÒ»¸öÀàÐÍΪintÐ͵ıäÁ¿£¬±äÁ¿ÃûΪi¡£ÕâÊÇÒ»¸öÎ ......
JAVAÊÇÇ¿ÀàÐ͵ÄÓïÑÔ¡£µ±ÒýÓöÔÏóµÄʱºò£¬Èç¹ûÊÇÉÏËݵĻ°£¬ÐèҪǿÖÆת»»¡£ÈçÄãÉÏÃæдµÄ£¬
PiePlot p = (PiePlot)Plot£¬Õâ¾ÍÊÇÉÏËÝ£¬Éú³ÉʵÀýʱÒýÓõÄÊǸ¸À࣬ÐèҪǿÖÆת»»£¬ËùÒÔ¾ÍÐèÒª¼Ó¸ö£¨PiePlot£©À´ÉùÃ÷Plot£¨PlotÊǸ¸Àࣩ¡£ ......
/* ¸ßÊÖÖ®×÷£¬±¾È˽÷ÒÔÊÕ²ØÕßÉí·Ý¹²ÏíÔ´Â룬¹©´ó¼Ò²Î¿¼Ö®! */
/*
* ÁбíADT½Ó¿Ú
*/
package dsa;
public interface List {
//²éѯÁÐ±íµ±Ç°µÄ¹æÄ£
public int getSize();
//ÅжÏÁбíÊÇ·ñΪ¿Õ
public boolean isEmpty();
//·µ»ØµÚÒ»¸öÔªËØ£¨µÄλÖã©
public Position first();
//· ......