Ordered array of JAVA data structure, binary lookup method

Source: Internet
Author: User

1. Insert Data graph

2, two points to find the schematic:

Package Com.struct.array; /** * @ Description ordered array * @ Project name Java_datastruct * @ Package Name Com.struct.array * @ class name Basicarray * @author Chenlin * @date June 20, 2011
    PM 8:41:21 */public class Orderarray {//array private long arr[];
    The effective length of the array is private int elements;

    The maximum length of the array is private int max;
        Initializes public orderarray (int max) {this.max = max;
    arr = new Long[max]; /** * Insert Data * * @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++;
        /** * Show All data */public void display () {System.out.print ("["); for (int i = 0; I &lT Elements
        i++) {System.out.print ("" + arr[i]);
    } System.out.println ("]"); /** * Find index based on value * * @param value * @return/public int search (Long value) {I
        NT I;
            for (i = 0; i < elements; i++) {//represent found, exit loop if (arr[i] = = value) {break;
        } if (i = = elements) {return-1;
        else {return i;
        }/** * Binary Lookup method * * @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 {//go to large area to find if (Arr[mid] < value) {low = mid + 1;                   Look in the small area for} else if (Arr[mid] > value) {high = mid-1;
    /** * Find values based on index * * @param index * @return * * public long get (int index) {if (Index > elements | | Index < 0) {throw new Arrayindexoutofboun
        Dsexception ();
    return Arr[index]; /** * Changes the value on index * * @param index * @param value/public void change (int index, LON
        G value) {if (Index > elements | | | Index < 0) {throw new arrayindexoutofboundsexception ();
    } Arr[index] = value; /** * Delete data on index * * @param index */public void Delete (int index) {if (Index &G T 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);
        Display Array.display ();
        System.out.println ("======================================");
        Find System.out.println (Array.search (22));

        System.out.println ("======================================");
        Two-point search System.out.println (Array.halfsearch (44));
        System.out.println ("======================================");
        Modification of Array.change (2, 50);
        Array.display ();
        System.out.println ("======================================");
        Delete Array.delete (3);
    Array.display (); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.