BlogJava-Be alaways javaing...-随笔分类-数据结构与算法http://www.blogjava.net/byrtiger/category/35113.htmlLoving Javazh-cnFri, 10 Oct 2008 11:41:03 GMTFri, 10 Oct 2008 11:41:03 GMT60二分搜索法http://www.blogjava.net/byrtiger/archive/2008/10/10/233625.html追风舞者追风舞者Fri, 10 Oct 2008 08:19:00 GMThttp://www.blogjava.net/byrtiger/archive/2008/10/10/233625.htmlhttp://www.blogjava.net/byrtiger/comments/233625.htmlhttp://www.blogjava.net/byrtiger/archive/2008/10/10/233625.html#Feedback0http://www.blogjava.net/byrtiger/comments/commentRss/233625.htmlhttp://www.blogjava.net/byrtiger/services/trackbacks/233625.htmlpublic class Search {

    
/**
     * 前提条件array数组已排序
     
*/
    
public static boolean binarySearch(int[] array, int target) {
        
boolean result = false;
        
int bottom = 0;
        
int top = array.length-1;
        
while (bottom <= top) {
            
int mid = (top + bottom) / 2;
            
if (target == array[mid]) {
                result 
= true;
                
break;
            } 
else if (target < array[mid]) {
                top 
= mid - 1;
            } 
else if (target > array[mid]) {
                bottom 
= mid + 1;
            }
        }

        
return result;
    }

    
public static void main(String[] args) {
        
int [] array = {1,3,5,7,9,10};
        
boolean result = binarySearch(array, 10);
        System.out.println(result);
    }
}


追风舞者 2008-10-10 16:19 发表评论
]]>
汉诺塔http://www.blogjava.net/byrtiger/archive/2008/10/10/233618.html追风舞者追风舞者Fri, 10 Oct 2008 07:56:00 GMThttp://www.blogjava.net/byrtiger/archive/2008/10/10/233618.htmlhttp://www.blogjava.net/byrtiger/comments/233618.htmlhttp://www.blogjava.net/byrtiger/archive/2008/10/10/233618.html#Feedback0http://www.blogjava.net/byrtiger/comments/commentRss/233618.htmlhttp://www.blogjava.net/byrtiger/services/trackbacks/233618.htmlimport java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Hanoi {
    
public static void main(String[] args) throws NumberFormatException,
            IOException {
        System.out.print(
"请输入盘数:");
        BufferedReader br 
= new BufferedReader(new InputStreamReader(System.in));
        
int n = Integer.parseInt(br.readLine());
        move(n, 
'A''B''C');
    }

    
/**
     * 将n个盘子借助b,从 a 移到 c
     
*/
    
public static void move(int n, char a, char b, char c) {
        
if (n == 1)
            System.out.println(
"盘 " + n + " 由 " + a + " 移至 " + c);
        
else {
            move(n 
- 1, a, c, b);
            System.out.println(
"盘 " + n + " 由 " + a + " 移至 " + c);
            move(n 
- 1, b, a, c);
        }
    }
}


追风舞者 2008-10-10 15:56 发表评论
]]>
Java快速排序http://www.blogjava.net/byrtiger/archive/2008/10/10/233608.html追风舞者追风舞者Fri, 10 Oct 2008 07:39:00 GMThttp://www.blogjava.net/byrtiger/archive/2008/10/10/233608.htmlhttp://www.blogjava.net/byrtiger/comments/233608.htmlhttp://www.blogjava.net/byrtiger/archive/2008/10/10/233608.html#Feedback0http://www.blogjava.net/byrtiger/comments/commentRss/233608.htmlhttp://www.blogjava.net/byrtiger/services/trackbacks/233608.htmlpublic class Sort {

    
public static void quickSort(int[] array) {
        quickSort(array, 
0, array.length - 1);
    }

    
private static void quickSort(int[] array, int low, int high) {
        
if (low < high) {
            
int p = partition(array, low, high);
            quickSort(array, low, p 
- 1);
            quickSort(array, p 
+ 1, high);
        }

    }

    
private static int partition(int[] array, int low, int high) {
        
int s = array[high];
        
int i = low - 1;
        
for (int j = low; j < high; j++) {
            
if (array[j] < s) {
                i
++;
                swap(array, i, j);
            }
        }
        swap(array, 
++i, high);
        
return i;
    }

    
private static void swap(int[] array, int i, int j) {
        
int temp;
        temp 
= array[i];
        array[i] 
= array[j];
        array[j] 
= temp;
    }
    
    
public static void main(String[] args) {
        
int [] array = {2,5,3,7,4};
        quickSort(array);
        
for(int i = 0;i<array.length;i++){
            System.out.println(array[i]);
        }
    }

}


追风舞者 2008-10-10 15:39 发表评论
]]>