Java data structure and algorithm (2) -- array, java Data Structure
What is the use of arrays? -- When you need to arrange 30 numbers in size, it is a good choice to store data structures like arrays. When you are a class teacher, the array is also useful every time you want to record the number of absences of those students. You can insert, delete, and search arrays.
1) Creation and Memory Allocation
Java has two data types: basic and object types, also known as reference types. in Java, arrays are treated as objects and the new operator is used to create arrays.
int array[] = new int[10];
Since it is an object, array is a reference of the array. According to Java programming philosophy (1)-everything is the memory allocation of the object, array will open up space in the stack, in addition, the space stores the address for storing the array storage. The place where the object is actually saved is correct. The new operation opens up the required space in the heap, and then the array points to the header address.
Initialization:
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 array after new is initialized to 0 by default, and the object initialization is empty, null, of course, can also be initialized through.
2) 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){ int index = 0; for (int i= 0; i < number; i++) { if(array[i]==value) return index; } 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(); }else{ System.out.println("not found!"); } if(ua.delete(5)!=true){ System.out.println("can not delete!"); } ua.display(); }}
Encapsulate the entire array and use number to replace the number of arrays. when inserting data, you do not have to worry about which subscript to insert. Of course, you can also customize a specific method for downloading.
The method is simple and I will not introduce it, but one of the shortcomings is that the delete method only shifts left from the deletion of elements. Therefore, although the number is reduced, however, the last element is not deleted, but hidden when the display output is displayed. However, the position of the last element will be replaced by the new element when the element is inserted next time.
3) Search Optimization-Binary 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 prerequisite for Binary Search is that the array is ordered. At the beginning, the index is written as end and start subtraction, resulting in an endless loop. What we need is addition. 1, 2, 3, 6, and 7. Index = 2, value = 7, 3 less than 7, start = 3, then the index requires the intermediate number between 3 and 4, so it is the sum divided by less than 7, start = 4, find to 7.
For sorting, see -- Java data structure and algorithm (3) -- simple sorting.
The same principle applies to storage objects.
4) Large O notation
Set N to the total number of data, and add the insertion time to K.
The total linear search time T = K * N/2, because the query time is about half of the comparison time.
For binary search, T = k * log2 (N ).
In large O notation, O can be regarded as order of, which is about to mean, k/2 is also a constant, so it can be considered as O (N ).
The disadvantage of the array is that the size is fixed and the query is slow. If you want to find millions of data records frequently, will the array be used? No, so the data structure should be selected based on the actual situation to maximize the efficiency.
JAVA data structures and algorithms
I wrote the following answer for you and asked you if you have any questions.
B
A
C
Confirm
3
InfexOf
Head pointer to end
Pair
Pair
Sequence Table: easy to search, but difficult to insert;
Linked List: difficult to find, but easy to insert.
// Maximum public static int getMax (int n, int [] arr) {// n is the indexif (n = 0) return arr [0] of the last element of the array; if (arr [n]> getMax (n-1, arr) return arr [n]; return getMax (n-1, arr );} // average public static int getAverage (int n, int [] arr) {// n is the indexif (n = 1) return arr [0] of the last element of the array; return (arr [n] + getAverage (n-1, arr) * (n-1)/n;} // Delete the public static Node rmNode (Node head, node node) {Node temp = head; while (temp. ne Xt! = Null) {if (temp. next = node) {temp. next = node. next; break;} elsetemp = temp. next;} return head;} // array element reverse public static int [] inverseArray (int [] arr) {int start = 0; int end = arr. length-1; for (; start <arr. length/2; start ++, end --) {int temp = arr [start]; arr [start] = arr [end]; arr [end] = temp ;} return arr;
To write a java program, you must be able to complete sorting and searching, respectively using linked lists, arrays, binary trees, and other data structures to compare the advantages and disadvantages of various methods.
No java, but this problem is essentially a Data Structure problem. The so-called sorting and search efficiency depend on the combination of algorithms and data structures. Now you have set a linked list (without specific instructions, here we should refer to a one-way linked list), array, and binary tree. Among them, the sorting and searching data depends on the algorithm used and the corresponding data structure ~~~
In sorting algorithms, quick sorting is the fastest, and it is suitable for processing with a linked list. However, linked list searching is slow (two-way linked list can speed up searching ).
Array sorting will be slow, not an algorithm problem, but an array adjustment because of the need for displacement, but once the array order is ranked, the search is very fast-half-query.
The number of binary forks is relatively tied, So heap sorting can be used for sorting. You can create a binary sorting tree for searching (using B + or B-tree can be faster ).
My opinion is not necessarily true. You are welcome to make a brick. If you know the algorithm, go online and find it.