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