二分查找问题(Java版)
 
1.一般实现
package search;
 
/**
 * @author lei 2011-8-17
 */
public class BinarySearch {
    /**
     * 二分查找
     *
     * 注意:二分查找只是针对有序排列的各种数组或集合
     *
     * @param target
     * @param array
     * @return
     */
    static boolean binarySearch(int target, int[] array) {
        int front = 0;
        int tail = array.length - 1;
        // 判断子数组是否能再次二分
        while (front <= tail) {
            // 获取子数组的中间位置,并依据此中间位置进行二分
            int middle = (front + tail) / 2;
 
            if (array[middle] == target) {
                return true;
            } else if (array[middle] > target) {
                tail = middle - 1;
            } else {
                front = middle + 1;
            }
        }
        return false;
    }
 
    public static void main(String[] args) {
        int[] array = new int[] { 1, 2, 3, 5, 7, 9, 17, 121, 4545 };
        System.out.println(binarySearch(4545, array));
    }
}
 
 
 
 
 
  • Java二分查找实现,欢迎大家提出交流意见.  
  • /** 
  • *名称:BinarySearch 
  • *功能:实现了折半查找(二分查找)的递归和非递归算法. 
  • *说明: 
  • *     1、要求所查找的数组已有序,并且其中元素已实现Comparable<T>接口,如Integer、String等. 
  • *    2、非递归查找使用search();,递归查找使用searchRecursively(); 
  • *本程序仅供编程学习参考 
  • *@author:   Winty 
  • *@date:     2008-8-11 
  • *@email:    wintys@gmail.com 
  • */  
  •   
  • class BinarySearch<T extends Comparable<T>> {  
  •     private T[]  data;//要排序的数据  
  •   
  •     public BinarySearch(T[] data){  
  •         this.data = data;  
  •     }  
  •   
  •     public int search(T key){  
  •         int low;  
  •         int high;  
  •         int mid;  
  •   
  •         if(data == null)  
  •             return -1;  
  •   
  •         low = 0;  
  •         high = data.length - 1;  
  •   
  •         while(low <= high){  
  •             mid = (low + high) / 2;  
  •             System.out.println("mid " + mid + " mid value:" + data[mid]);///  
  •               
  •             if(key.compareTo(data[mid]) < 0){  
  •                 high = mid - 1;  
  •             }else if(key.compareTo(data[mid]) > 0){  
  •                 low = mid + 1;  
  •             }else if(key.compareTo(data[mid]) == 0){  
  •                 return mid;  
  •             }  
  •         }  
  •   
  •         return -1;  
  •     }  
  •   
  •     private int doSearchRecursively(int low , int high , T key){  
  •         int mid;  
  •         int result;  
  •   
  •         if(low <= high){  
  •             mid = (low + high) / 2;  
  •             result = key.compareTo(data[mid]);  
  •             System.out.println("mid " + mid + " mid value:" + data[mid]);///  
  •               
  •             if(result < 0){  
  •                 return doSearchRecursively(low , mid - 1 , key);  
  •             }else if(result > 0){  
  •                 return doSearchRecursively(mid + 1 , high , key);  
  •             }else if(result == 0){  
  •                 return mid;  
  •             }  
  •         }  
  •           
  •         return -1;  
  •     }  
  •   
  •     public int searchRecursively(T key){  
  •         if(data ==null)return -1;  
  •   
  •         return doSearchRecursively(0 , data.length - 1 , key);  
  •     }  
  •   
  •     public static void main(String[] args){  
  •         Integer[] data = {1 ,4 ,5 ,8 ,15 ,33 ,48 ,77 ,96};  
  •         BinarySearch<Integer> binSearch = new BinarySearch<Integer>(data);  
  •         //System.out.println("Key index:" + binSearch.search(33) );  
  •   
  •         System.out.println("Key index:" + binSearch.searchRecursively(3) );  
  •   
  •         //String [] dataStr = {"A" ,"C" ,"F" ,"J" ,"L" ,"N" ,"T"};  
  •         //BinarySearch<String> binSearch = new BinarySearch<String>(dataStr);  
  •         //System.out.println("Key index:" + binSearch.search("A") );  
  •     }  
  • }  
  •  
     
     
     
     
     
     
     
     
     
    package OrderedArray;
     
    /**
     * 有序数组
     * @author anan
     */
    import java.util.Scanner;
     
    class OrderedArray {
        private int[] array;
        private int nElems;
     
        // 构造函数,生成有序数组对象
        public OrderedArray(int max) {
            // 生成数组对象
            array = new int[max];
            // 数组初始元素值为0
            nElems = 0;
        }
     
        // 返回数组大小
        public int size() {
            return nElems;
        }
     
        // 二分查找
        public int BinarySearch(int searchKey) {
            int lowBound = 0;
            int highBound = nElems - 1;
            int currentIndex;
            while (true) {
                currentIndex = (lowBound + highBound) / 2;
                if (array[currentIndex] == searchKey)
                    // 返回数组中key所在位置
                    return currentIndex;
                if (lowBound > highBound)
                    return nElems;
                else {
                    if (array[currentIndex] < searchKey) {
                        lowBound = currentIndex + 1;
                    } else if (array[currentIndex] > searchKey) {
                        highBound = currentIndex - 1;
                    }
                }
            }
        }
     
        // 输入要查找的数
        public int input() {
            Scanner sc = null;
            System.out.println("please input the key you want to search --->");
            sc = new Scanner(System.in);
            int searchKey = sc.nextInt();
            sc.close();
            return searchKey;
        }
     
        // 显示数组元素
        public void display() {
            for (int i = 0; i < nElems; i++) {
                System.out.print(array[i] + " ");
            }
        }
     
        // 插入操作
        public void insert(int value) {
            int i;
            for (i = 0; i < nElems; i++) {
                if (array[i] > value) {
                    break;
                }
            }
            for (int j = nElems; j > i; j--) {
                // 数组元素依次向后移动一位
                array[j] = array[j - 1];
            }
     
            array[i] = value;
            nElems++;
        }
     
        // 删除操作
        public boolean delete(int value) {
            boolean flag = false;
            int j = BinarySearch(value);
            if (j == nElems)
                flag = false;
            else {
                for (int k = j; k < nElems; k++) {
                    array[k] = array[k + 1];
                }
                nElems--;
                flag = true;
            }
            return flag;
        }
     
    };
     
    public class OrderedArrayDemo {
     
        public static void main(String[] args) {
            int maxSize = 100;
            OrderedArray arr = new OrderedArray(maxSize);
            arr.insert(11);
            arr.insert(32);
            arr.insert(5);
            arr.insert(26);
            arr.insert(45);
            arr.insert(88);
            arr.insert(1);
            arr.insert(9);
            arr.delete(9);
            arr.delete(88);
            System.out.println(arr.size());
            System.out.println("---------------------");
            arr.display();
            System.out.println();
            int key = arr.input();
            if (arr.BinarySearch(key) != arr.size()) {
                System.out.println("found the key!");
            } else {
                System.out.println("not found the key!");
            }
     
        }
    }
     
     
     
     
     
     

    4.二分查找示例二(对链表进行查找)

    成员类:
    二分查找问题(Java版)package com.junglesong; 二分查找问题(Java版)}


    二分查找类:
    二分查找问题(Java版)package com.junglesong; 二分查找问题(Java版)二分查找问题(Java版)import java.util.ArrayList; 二分查找问题(Java版)import java.util.List; 二分查找问题(Java版)}

    相关文章:

    • 2021-12-21
    • 2022-02-05
    • 2022-12-23
    • 2021-12-10
    • 2021-11-25
    • 2021-09-22
    猜你喜欢
    • 2022-12-23
    • 2021-10-12
    • 2021-10-22
    • 2021-10-24
    • 2021-08-01
    • 2022-01-22
    相关资源
    相似解决方案