# "Data structure and algorithm 01" array

Source: Internet
Author: User

Arrays are the most widely used data storage structures. It is implanted in most programming languages, because the array is very easy to understand, so do not repeat here, mainly attached to both ends of the code, one is a normal array, and the other is an ordered array. Ordered arrays are sorted by keyword ascending (or descending), which makes it possible to find data items quickly, that is, you can use a binary lookup.

Java code for normal arrays:

` Public classGeneralarray {Private int[] A; Private intSize//the size of the array    Private intNelem;//How many items are in the array     PublicGeneralarray (intMax) {//initializing an array         This. A =New int[Max];  This. Size =Max;  This. Nelem = 0; }       Public BooleanFindintSearchnum) {//Find a value        intJ;  for(j = 0; J < Nelem; J + +){              if(A[j] = =searchnum) Break; }          if(J = =Nelem)return false; Else              return true; }       Public BooleanInsertintValue) {//Insert a value        if(Nelem = =size) {System.out.println ("The array is full!" "); return false; } A[nelem]=value; Nelem++; return true; }       Public BooleanDeleteintValue) {//Delete a value        intJ;  for(j = 0; J < Nelem; J + +) {              if(A[j] = =value) {                   Break; }          }          if(J = =Nelem)return false; if(Nelem = =size) {               for(intK = J; K < nElem-1; k++) {A[k]= A[k+1]; }          }          Else {               for(intK = J; K < Nelem; k++) {A[k]= A[k+1]; }} Nelem--; return true; }       Public voidDisplay () {//print the entire array         for(inti = 0; i < Nelem; i++) {System.out.print (A[i]+ " "); } System.out.println (""); }  }    `

Java code for an ordered array:

` Public classOrderedarray {Private Long[] A; Private intSize//the size of the array    Private intNelem;//How many items are in the array     PublicOrderedarray (intMax) {//initializing an array         This. A =New Long[Max];  This. Size =Max;  This. Nelem = 0; }       Public intSize () {//returns how many values the array actually has        return  This. Nelem; }  //--------------the dichotomy to find a value----------------//       Public intFindLongsearchnum) {          intLower = 0; intUpper = NElem-1; intCurr;  while(true) {Curr= (lower + upper)/2; if(A[curr] = =searchnum)returnCurr; Else if(Lower >Upper)return-1; Else {                  if(A[curr] <searchnum) Lower= Curr + 1; ElseUpper= Curr-1; }                        }      }       Public BooleanInsertLongValue) {//Insert a value        if(Nelem = =size) {System.out.println ("The array is full!" "); return false; }          intJ;  for(j = 0; J < Nelem; J + +){              if(A[j] >value) Break; }                     for(intK = Nelem; K > J; k--) {A[k]= A[k-1]; } A[j]=value; Nelem++; return true; }       Public BooleanDeleteLongValue) {//Delete a value        intj =find (value); if(j = =-1) {System.out.println ("No such element!" "); return false; }          if(Nelem = =size) {               for(intK = J; K < nElem-1; k++) {A[k]= A[k+1]; } A[nelem-1] = 0; }          Else {               for(intK = J; K < Nelem; k++) {A[k]= A[k+1]; }} Nelem--; return true; }       Public voidDisplay () {//print the entire array         for(inti = 0; i < Nelem; i++) {System.out.print (A[i]+ " "); } System.out.println (""); }  }  `

For the data structure of the array, linear search, time complexity is O (n), two points to find the time is O (LONGN), unordered array insertion time complexity is O (1), ordered array insertion time complexity is O (n), delete operation time complexity is O (n).

"Data structure and algorithm 01" array

Related Keywords:

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.