PackageAA;classarray{//define an ordered array Private Long[] A; //Define Array Length Private intNelems; //Constructor Initialization PublicArray (intmax) {a=New Long[Max]; Nelems= 0; } //size function Public intsize () {returnNelems; } //Define Add function Public voidInsertLongvalue) { //assigning value to an array memberA[nelems] =value; //the array length is then added to aNelems + +; Longtemp; //sort by bubbling method for(inti = 0; i < nElems-1; i + +) { for(intj = 0; J < nElems-1-I; J + +) { if(A[j] > a[j + 1]) {temp=A[j]; A[J]= A[j + 1]; A[j+ 1] =temp; } } } } //Define a Lookup method Public intFindLongSearchkey) { //because it is an ordered array, we can use the dichotomy to find the time O (logn), if the linear lookup is O (N)//Lower Limit intlowerbound = 0; //Upper Limit intUpperbound = nElems-1; //Middle Value intCurindex; while(true) {Curindex= (lowerbound + upperbound)/2; if(A[curindex] = =Searchkey) { returnCurindex; } Else if(Lowerbound >upperbound) { returnNelems; } Else { if(A[curindex] >Searchkey) {Upperbound= CurIndex-1; } Else{lowerbound= Curindex + 1; } } } } //define the Delete method Public BooleanDeleteLongvalue) { intindex =find (value); if(Index = =size ()) { return false; } Else { for(inti = index; I < size (); i++) {A[i]= A[i + 1]; } nelems--; return false; } } //Defining Display Methods Public voiddisplay () { for(intj = 0; J < Nelems; J + +) {System.out.println (A[j]+ " "); } System.out.println (""); } } Public classARR { Public Static voidMain (string[] args) {intmaxSize = 100; Array arr=NewArray (maxSize); Arr.insert (77); Arr.insert (99); Arr.insert (44); Arr.insert (55); Arr.insert (22); Arr.insert (88); Arr.insert (11); Arr.insert (22); Arr.insert (66); Arr.insert (33); Arr.display (); intSearchkey = 54; if(Arr.find (searchkey)! =arr.size ()) {System.out.println ("Found" +Searchkey); } Else{System.out.println ("Cant find" +Searchkey); } arr.delete (22); Arr.delete (55); Arr.delete (99); Arr.display (); }}
PackageAA;//define an unordered arrayclasshigharray{Private Long[] A; Private intNelems; PublicHigharray (intmax) {a=New Long[Max]; Nelems= 0; } Public BooleanFindLongSearchkey) { intJ; for(j = 0; J < Nelems; J + +) { if(A[j] = =Searchkey) { Break; } } if(J = =Nelems) { return false; } Else { return true; } } Public voidInsertLongvalue) {A[nelems]=value; Nelems++; } Public BooleanDeleteLongvalue) { intJ; for(j = 0; J < Nelems; J + +) { if(Value = =A[j]) { Break; } } if(J = =Nelems) { return false; } Else { for(intK = J; K < Nelems; k++) {A[k]= A[k+1]; } nelems--; return true; } } Public voiddisplay () { for(intj = 0; J < Nelems; J + +) {System.out.println (A[j]+ ""); } System.out.println (""); }} Public classHigharrayapp { Public Static voidMain (string[] args) {intmaxSize = 100; Higharray arr=NewHigharray (maxSize); Arr.insert (77); Arr.insert (99); Arr.insert (44); Arr.insert (55); Arr.insert (22); Arr.insert (88); Arr.insert (11); Arr.insert (00); Arr.insert (66); Arr.insert (33); Arr.display (); intSearchkey = 35; if(Arr.find (Searchkey)) {System.out.println ("Found" +Searchkey); } Else{System.out.println ("Cant find" +Searchkey); } arr.delete (00); Arr.delete (55); Arr.delete (99); Arr.display (); }}
Large O notation
O (1): excellent. For example, no array insertion is required.
O (LOGN): good. For example, ordered binary lookups.
O (N): Pass. For example, the deletion of unordered arrays, the deletion and insertion of ordered arrays, and linear lookups.
O (N2): failed. such as bubble sort.
Summarize ordered arrays and unordered arrays
Ordered array: Insert + find + delete = O (n) +o (LOGN) +o (n);
Unordered array: Insert + find + delete = O (1) + O (n) + O (n);
So when the data biased to find operations with an ordered array faster, when the data bias insertion, the unordered array is better. The removal operation is as efficient.
Java object-oriented comparison of ordered arrays and unordered arrays