Arrays are the most widely used data structures. They are often embedded in programming languages and used as basic data types. Therefore, in some textbooks, arrays are not explained separately as a data structure (in fact, arrays are sequential memory, even if they are not sequential in physical memory, they must be sequential logically ). In fact, there is no need to tangle concepts. Arrays can be used as a stepping stone for learning data structures. On this basis, we can understand the basic concepts and construction methods of data structures.
The data structure is not only a data container, but also a data operation method, such as retrieval, insertion, deletion, and sorting.
Unordered Array
Next, we create a class to encapsulate the search, insert, delete, and print operations on the array. For simplicity, we assume that there are no duplicate values in the array (in fact, the array can contain duplicate values)
Public class Array {private String [] strArray; private int length = 0; // Number of Array elements // constructor, input the maximum length of the Array public Array (int max) {strArray = new String [max];} // checks whether the array contains an element. If its subscript exists and does not exist,-1 public int contains (String target) is returned) {int index =-1; for (int I = 0; I <length; I ++) {if (strArray [I] = target) {index = I; break;} return index;} // insert public void insert (String elem) {strArray [length] = elem; length + + ;}// Delete a specified Element value. If the deletion is successful, true is returned. Otherwise, false public boolean delete (String target) {int index =-1 is returned; if (index = contains (target ))! =-1) {for (int I = index; I <length-1; I ++) {// After the element is deleted, all elements are moved forward to one position. strArray [I] = strArray [I + 1];} length --; return true;} else {return false ;}} // list all elements public void display () {for (int I = 0; I <length; I ++) {System. out. print (strArray [I] + "\ t ");}}}
Advantages of unordered Arrays:Fast insertion. If you know the subscript, you can quickly access it.
Disadvantages of unordered Arrays:Slow query, slow deletion, and fixed size.
Ordered array
The so-called ordered array means that the elements in the index group are arranged according to certain rules. The advantage is that it can be used for searching based on element values.Binary SearchThe search efficiency is much higher than that of the unordered array, which is more obvious when the data volume is large. Of course, the disadvantage is also obvious. when inserting an element, you must first determine the subscript that the element should be inserted, and then move one bit after all the elements after the subscript to insert the element, this undoubtedly increases the overhead.
Therefore, ordered arrays are suitable for queries that are frequent, but insert and delete operations are rare.
The encapsulation class of an ordered array is as follows. For convenience, we still assume that there are no repeated values in the array, and the data is arranged in a descending order.
Public class OrderArray {private int [] intArray; private int length = 0; // Number of array elements // constructor, input the maximum length of the array public OrderArray (int max) {intArray = new int [max];} // locate an element by means of binary search. If its subscript exists and does not exist,-1 public int find (int target) is returned) {int lowerBound = 0; // small int upperBound = length-1 for the smallest element in the search segment; // The subscript int curIn for the largest element in the search segment; // if (upperBound <0) {// if the array is empty, return-1 return-1;} while (true) {curIn = (lowerBound + upperBo Und)/2; if (target = intArray [curIn]) {return curIn;} else if (curIn = lowerBound) {// when the current subscript and the minimum subscript of the search segment are duplicated, it indicates that the search segment contains only one or two elements, // if both the low element and the high element are not equal to the target element, it proves that this element is not in the array and the search ends if (target! = IntArray [upperBound]) {return-1 ;}} there are at least three elements in the else {// search segment, and the current element is not equal to the target element if (intArray [curIn] <target) {// if the current element is smaller than the target element, set the minimum subscript of the next search segment to the subscript lowerBound = curIn;} else {// if the current element is greater than the target element, set the maximum subscript of the next search segment to the subscript upperBound = curIn ;}}// insert public void insert (int elem) {int location = 0; // determine the subscript for (; location <length; location ++) {if (intArray [location]> elem) break;} // System. out. println (location );/ /Move all elements after the subscript is inserted one for (inti = length; I> location; I --) {intArray [I] = intArray [I-1];} // Insert the element intArray [location] = elem; length ++;} // delete a specified Element value. If the deletion is successful, true is returned, otherwise, false public boolean delete (int target) {int index =-1; if (index = find (target) is returned ))! =-1) {for (inti = index; I <length-1; I ++) {// After the element is deleted, all elements are moved forward by one intArray [I] = intArray [I + 1];} length --; return true;} else {return false ;}} // list all elements public void display () {for (int I = 0; I <length; I ++) {System. out. print (intArray [I] + "\ t");} System. out. println ();}}
The biggest advantage of an ordered array is that it can improve the efficiency of searching elements. In the above example, the find method uses the binary search method. The algorithm is as follows:
This method sets the variables lowerBound and upperBound at the beginning to point to the first and last non-null data items in the array. You can set these variables to determine the search range. Then, in the while loop, the current subscript curIn is set to the middle value of this range.
If curIn is the data item we are looking for, the subscript is returned. If not, we have to consider two situations: If curIn points to a smaller data item than we are looking, it is proved that the element can only be between curIn and upperBound, that is, the second half of the array (the array is arranged from small to large), and the next round needs to be retrieved from the second half; if curIn points to a data item that is larger than the data we are looking for, it proves that this element is only possible between lowerBound and curIn, And the next Round needs to be searched in the first half segment.
Perform iterative search based on the above method until the end of the search.
Advantages of ordered array:High search efficiency
Disadvantages of ordered array:Slow deletion and insertion, fixed size