Java Data structure and algorithm (ii)--array __java data structure and algorithm

Source: Internet
Author: User

Comments cited a lot of mistakes, has not been corrected, too lazy, really lazy after work, promised someone's things have not started, not easy to start,

A lot of time is wasted, but planting a tree is best ten years ago, or now. The goal is to be achieved.

Recorded in 2016-6-22 23:39:13


Error in the comments, find return number looks like a bug, but take a closer look at the method of judgment, it is very special.

if (Ua.find (1)!= ua.number) {
            System.out.println ("find,the number index is" +ua.find (1));
        } else{
            System.out.println ("not found!");
        
If the number is returned, then the description is not found. Different ideas.
There is a problem with the two-point lookup, indeed, while (End>=start).


What is the use of arrays? --When you need to size 30 of numbers, using data structure such as array storage is a good choice, when you are a class headteacher, each time you want to record the number of absences of those students, the array is also very useful. Arrays can be inserted, deleted, searched, and so on.


1) Creation and memory allocation

There are two types of data in Java, basic types and object types, others are referred to as reference types, in Java, arrays are objects, the new operator is used when creating arrays.

int array[] = new INT[10];
Since it is an object, then array is a reference to the array, according to the Java Programming Idea (i)-everything is the object's memory allocation, array will open up space in the stack, and the space is stored to save the array storage address, the real place to save the object is right, the new operation in the heap to open up the required space, The array then points to the header address.


Class

public class Usearray {public
    static void Main (string[] args) {
        int array[] = new INT[10];
        System.out.println (array[2]);
        Usearray a[] = new USEARRAY[12];
        System.out.println (a[1]);
        int array2[] ={1,2,3,4,5,5,6}
The value in the new array is initialized to 0 by default, and the initialization of the object is null, and, of course, it can be initialized by {}.


2) After the use of array encapsulation


public class Usearray {private int[] array;
    private int number = 0;
    Public Usearray (int max) {array = new Int[max];
        public void Insert (int value) {Array[number] = value;
    number++;
                public int find (int value) {for (int i= 0; i < number; i++) {if (Array[i]==value)
        return i;
    return number;
        public boolean Delete (int value) {int ' index = find (value);
            if (index!= number) {for (int i = index; i < number-1; i++) {array[i] = array[i+1];
            } number--;
        return true;
    return false; public void display () {for (int i = 0; i < number; i++) {System.out.printf (array[i]+ "")
        );
        } public static void Main (string[] args) {Usearray ua = new Usearray (5);
        Ua.insert (1);
        Ua.insert (2);
    Ua.insert (6);    Ua.insert (7);
        
        Ua.insert (3);
        Ua.display ();
        if (Ua.find (5)!= ua.number) {System.out.println ("find,the number index is" +ua.find (5));
        }else{System.out.println ("not found!");
        } if (Ua.delete (5)!=true) {System.out.println ("Can not delete!");
    } ua.display (); }
}

Encapsulate the entire array, use number instead of the array, and insert the data without having to pay attention to which subscript, of course, you can customize a specific subscript method.


The method is relatively simple, but the existence of a disadvantage in the delete there, in fact, only from the deletion of the beginning of the left, so, although number decreased, but the last element is not deleted, but the display output of the time hidden, but, The next time you insert an element, the new element replaces the position of the last element.


3) Find optimization--two-point search

public int find (int value) {
        int start = 0;
        int end = Number-1;
        while (End>=start) {
            int index = (end + start)/2; 
            if (array[index]==value) {return
                index;
            } else if (Array[index] >value) {End
                = index-1;
            } else { 
                start = index+1;
            }
        }
        return number;
    }
The binary lookup premise is that the array is already ordered. The first index is written as end and start subtraction, resulting in a dead loop. Actually want to be added. 1,2,3,6,7. index=2,value=7,3 is less than 7,start=3, then index wants the middle number between 3 and 4, so it is added and divided by 2,6 less than 7,start=4,find to 7.

Sort of words look--JAVA data structure and algorithm (c)--simple sort.

The same principle of storing objects.


4) Large O representation

Set N to the total number of data, add a data to insert a time of K.

Then the linear lookup total time is T=K*N/2 because the lookup is probably half the number of comparisons.

Two-point search words t=k*log2 (N).

The Big O notation, O can be seen as order of, about meaning, K/2 is also constant, so it can be regarded as O (N).


The disadvantage of the array is that the size is fixed, the lookup is slow, if you want to often find millions other data, you will also use an array. No, so the selection of data structure should be combined with concrete actual situation, to achieve maximum efficiency value.

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.