# Data structure and algorithm (i)--array

Source: Internet
Author: User

Array

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:

`1  Public classGeneralarray {2     Private int[] A; 3     Private intSize//the size of the array4     Private intNelem;//How many items are in the array5      PublicGeneralarray (intMax) {//initializing an array6          This. A =New int[Max]; 7          This. Size =Max; 8          This. Nelem = 0; 9     }  Ten      Public BooleanFindintSearchnum) {//Find a value One         intJ;  A          for(j = 0; J < Nelem; J + +){   -             if(A[j] = =searchnum) -                  Break;  the         }   -         if(J = =Nelem) -             return false;  -         Else   +             return true;  -     }   +      Public BooleanInsertintValue) {//Insert a value A         if(Nelem = =size) {   atSystem.out.println ("The array is full! ");  -             return false;  -         }   -A[nelem] =value;  -nelem++;  -         return true;  in     }   -      Public BooleanDeleteintValue) {//Delete a value to         intJ;  +          for(j = 0; J < Nelem; J + +) {   -             if(A[j] = =value) {   the                  Break;  *             }   \$         }  Panax Notoginseng         if(J = =Nelem) -             return false;  the         if(Nelem = =size) {   +              for(intK = J; K < nElem-1; k++) {   AA[k] = a[k+1];  the             }   +         }   -         Else {   \$              for(intK = J; K < Nelem; k++) {   \$A[k] = a[k+1];  -             }   -         }   thenelem--;  -         return true; Wuyi     }   the      Public voidDisplay () {//print the entire array -          for(inti = 0; i < Nelem; i++) {   WuSystem.out.print (A[i] + "");  -         }   AboutSystem.out.println ("");  \$     }   -}`

Java code for an ordered array:

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

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).

Mainly used for their own o learning to organize the use!

Reference post: http://blog.csdn.net/eson_15/article/details/51126182

Https://www.cnblogs.com/wuchanming/p/4371055.html

Data structure and algorithm (i)--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.