An array of data structures

Source: Internet
Author: User


Arrays are the most widely used data structures that are often embedded in programming languages and used as basic data types, so in some textbooks, arrays are not interpreted as a data structure alone (in fact, arrays are contiguous memory, even if they are not contiguous in physical memory, logically continuous). In fact, there is no need to do the concept of entanglement, the array can be used as a stepping stone to learn data structure, based on this, understand the basic concept of data structure and construction method

The data structure is not only the container of the information, but also provides the operation method of the data, such as retrieving, inserting, deleting, sorting, etc.

Unordered array

Let's create a class that encapsulates the retrieval, insertion, deletion, and printing operations of an array, and for simplicity, we assume that there are no duplicate values (in fact the array can contain duplicate values)

public class Array {private String [] strarray;       private int length = 0;      Number of array elements//construction method, passed in the array maximum length public array (int max) {strarray = new String [MAX];             }//detects if an array contains an element, if present returns its subscript, does not exist returns-1 public int contains (String target) {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++;             }//Deletes a specified element value, returns True if delete succeeds, otherwise returns false public boolean delete (String target) {int index =-1;                           if ((index = contains (target))!=-1) {for (int i=index;i<length-1;i++) {            Delete all elements after the element move forward one strarray[i] =strarray[i+1];         } length--;             return true;             }else{return false; }}//list all elements public void display () {for (int i=0;i<length;i++) {Syste             M.out.print (strarray[i]+ "\ t"); }      }     }

Advantages of unordered arrays: Quick Insert, if you know the subscript, you can quickly access

Disadvantages of unordered arrays: find slow, delete slow, fixed size.

Ordered array

The so-called ordered array is that the elements in the exponential group are arranged according to certain rules, the advantage is that when searching according to the element value can be used to find a binary , the search efficiency is much higher than the unordered array, more obvious when the data volume is large. Of course, the disadvantage is also obvious, when inserting an element, the first to determine the element should be inserted in the subscript, and then all the elements after the subscript to move one bit, in order to insert, which undoubtedly adds a lot of overhead.

As a result, an ordered array is useful for finding frequently, while inserting and deleting fewer operations

For convenience, we still assume that there are no duplicate values in the array, and that the data is arranged in small to large order.

public class Orderarray {private int [] intarray;       private int length = 0;      Number of array elements//construction method, passed in array maximum length public orderarray (int max) {intarray = new int [MAX];                 }//Locate an element with a binary lookup method, if present returns its subscript, does not exist returns-1 public int find (int target) {int lowerbound = 0;      The smallest element of the search segment is the small mark int upperbound = length-1;                                   Search for the largest element of the segment subscript int curin;             The index of the current detection element if (upperbound<0) {//If the array is empty, return directly-1 return-1;                                       } while (true) {Curin = (lowerbound+upperbound)/2;                    if (target = = Intarray[curin]) {return curin; }else if (Curin ==lowerbound) {//When the current subscript coincides with the minimum subscript of the search segment, represents only 1 or 2 elements in the search segment,//If the low and high elements are not equal In the target element, the proof array does not have the element, the search ends if (target!=intarray[upperbound]) {                                  return-1;                                  The}else{//search segment has at least three elements and the current element is not equal to the target element if (intarray[curin]< target) {                           If the current element is less than the target element, the lowest subscript of the next search segment is set to the subscript lowerbound =curin of the current element;                                  }else{//If the current element is greater than the target element, the maximum subscript of the next search segment is placed as the subscript of the current element                           Upperbound =curin; }}}}//Insert public void Insert (int elem) {int location =                         0;                           Determine where the subscript should be inserted for (; location<length;location++) {if (Intarray[location] >elem)             Break             }//system.out.println (location);             Move all elements after the subscript to a for (inti=length;i>location;i--) {intarray[i] = intarray[i-1];                  }       Insert element intarray[location] = Elem;      length++;             }//Deletes a specified element value, returns True if delete succeeds, otherwise returns false public boolean delete (int target) {int index =-1; if (index = find (target))! =-1) {for (inti=index;i<length-1;i++) {//                     Remove all elements after the element move forward 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++) {Syste             M.out.print (intarray[i]+ "\ t");      } System.out.println (); }}

The biggest advantage of an ordered array is that it can improve the efficiency of the lookup element, in the above example, the Find method uses a two-point lookup method, which is as follows:


This method sets variables lowerbound and upperbound at the beginning to point to the first and last non-empty data items of the array. You can determine the scope of the lookup by setting these variables. Then, in the while loop, the current subscript Curin is set to the middle value of this range

If Curin is the data item that we are looking for, then the subscript is returned, if not, there are two things to consider: if Curin points to a data item that is smaller than the data we are looking for, it proves that the element is only possible between Curin and Upperbound, That is, the second half of the array (the array is arranged from small to large), the next round to be retrieved from the second half; if Curin points to a data item larger than the data we are looking for, it proves that the element is only possible between lowerbound and Curin, and in the first half it will be retrieved

Iterate through the above method until the end

Advantages of ordered arrays: high efficiency of search

Disadvantages of ordered arrays: slow deletion and insertion, fixed size

An array of data structures

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.