The order table needs to master the insertion method, the deletion method and the dichotomy method to find the method.
Insert method: In the past, find an array item that is larger than the value you want to insert, move the array item and the subsequent item back one (from the last item to the next), and then insert the value into the current array item.
Delete method: Find the item you want to delete from the previous one, move the item after the array item forward one (moving forward from the next item in the array);
Binary lookup: Finds specific data items by dividing the range of array data items in half-way.
Example code:
PackageCHAP02. Orderedarray;classOrdarray {Private Long[] A; Private intNelems; PublicOrdarray (intmax) {a=New Long[Max]; Nelems= 0; } Public intsize () {returnNelems; } //Insert Method Public voidInsertLongvalue) { intJ; for(j=0; j<nelems; J + +) { if(A[j] > value) {//(linear search) Break; } } for(intK=nelems; k>j; k--) {//move bigger ones upA[k] = a[k-1]; } A[j]=value; Nelems++; } //Delete Method Public BooleanDeleteLongvalue) { intj =find (value); if(j==Nelems) { return false; } Else { for(intK=j; k<nelems; k++) {A[k]= A[k+1]; } nelems--; return true; } } //Binary Method Search Public intFindLongSearchkey) { intlowerbound = 0; intUpperbound = nElems-1; intCurin; while(true) {Curin= (lowerbound + upperbound)/2; if(a[curin]==Searchkey) { returnCurin;//found it } Else if(Lowerbound >upperbound) { returnNelems;//can ' t find it } Else { if(A[curin] <Searchkey) {Lowerbound= Curin + 1;//it ' s in upper half } Else{upperbound= curIn-1;//it ' s in lower half } } } } Public voiddisplay () { for(intj=0; j<nelems; J + +) {System.out.print (A[j]+ " "); } System.out.println (""); } } classorderedapp{ Public Static voidMain (string[] args) {intmaxSize = 100; Ordarray arr; Arr=NewOrdarray (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); intSearchkey = 55; if(Arr.find (searchkey)! =arr.size ()) {System.out.println ("Found" +Searchkey); } Else{System.out.println ("Can ' t find" +Searchkey); } arr.display (); Arr.delete (00); Arr.delete (55); Arr.delete (99); Arr.display (); } }
Java Data Structures and algorithms (1)-Ordered table (Orderedarray)