Java 資料結構之有序數組,二分尋找法

來源:互聯網
上載者:User

1 、插入資料圖

2、二分尋找示意圖:

package com.struct.array;/** * @描述 有序數組 * @項目名稱 Java_DataStruct * @包名 com.struct.array * @類名 BasicArray * @author chenlin * @date 2011年6月20日 下午8:41:21 */public class OrderArray {    // 數組    private long arr[];    // 數組的有效長度    private int elements;    // 數組的最大長度    private int max;    // 初始化    public OrderArray(int max) {        this.max = max;        arr = new long[max];    }    /**     * 插入資料     *      * @param value     */    public void insert(long value) {        if (elements > max) {            throw new ArrayIndexOutOfBoundsException();        }        int i;        for (i = 0; i < elements; i++) {            if (arr[i] > value) {                break;            }        }        for (int j = elements; j > i; j--) {            arr[j] = arr[j - 1];        }        arr[i] = value;        elements++;    }    /**     * 顯示全部資料     */    public void display() {        System.out.print("[");        for (int i = 0; i < elements; i++) {            System.out.print(" " + arr[i]);        }        System.out.println(" ]");    }    /**     * 根據值尋找索引     *      * @param value     * @return     */    public int search(long value) {        int i;        for (i = 0; i < elements; i++) {            // 表示找到,退出迴圈            if (arr[i] == value) {                break;            }        }        if (i == elements) {            return -1;        } else {            return i;        }    }    /**     * 二分尋找法     *      * @param value     * @return     */    public int halfSearch(long value) {        int mid = 0;        int low = 0;        int high = elements;        while (true) {            mid = (low + high) / 2;            if (arr[mid] == value) {                return mid;            } else if (low > high) {                return -1;            } else {                // 往大的地區尋找                if (arr[mid] < value) {                    low = mid + 1;                    // 往小的地區尋找                } else if (arr[mid] > value) {                    high = mid - 1;                }            }        }    }    /**     * 根據索引尋找數值     *      * @param index     * @return     */    public long get(int index) {        if (index > elements || index < 0) {            throw new ArrayIndexOutOfBoundsException();        }        return arr[index];    }    /**     * 更改index上的值     *      * @param index     * @param value     */    public void change(int index, long value) {        if (index > elements || index < 0) {            throw new ArrayIndexOutOfBoundsException();        }        arr[index] = value;    }    /**     * 刪除index上的資料     *      * @param index     */    public void delete(int index) {        if (index > elements || index < 0) {            throw new ArrayIndexOutOfBoundsException();        } else {            for (int i = index; i < elements; i++) {                arr[i] = arr[i + 1];            }            elements--;        }    }    public static void main(String[] args) {        OrderArray array = new OrderArray(30);        array.insert(22);        array.insert(12);        array.insert(34);        array.insert(44);        array.insert(86);        array.insert(57);        // 顯示        array.display();        System.out.println("======================================");        // 尋找        System.out.println(array.search(22));        System.out.println("======================================");        // 二分尋找        System.out.println(array.halfSearch(44));        System.out.println("======================================");        // 修改        array.change(2, 50);        array.display();        System.out.println("======================================");        // 刪除        array.delete(3);        array.display();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.