Java Data Structures and algorithms (1)-Ordered table (Orderedarray)

Source: Internet
Author: User

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)

Contact Us

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.