Data structures and algorithms: the dichotomy Demo

Source: Internet
Author: User

Package Com.js.ai.modules.pointwall.testxfz;class ordarray{private long[] a;private int nelems;public OrdArray (int max ) {a=new long[max];nelems=0;} public int size () {return nelems;} Insert method public void Insert (Long value) {int j;for (j=0;j<nelems;j++) {if (a[j]>value) break;} for (int k=nelems;k>j;k--) {a[k]=a[k-1];} a[j]=value;nelems++;} Delete method Public Boolean delete (Long value) {int j=find (value); if (J==nelems) {return false;} else {for (int k=j;k<nelems;k++) {a[k]=a[k+1];} Nelems--;return true;}} Two-point lookup public int find (long searchkey) {int Lowerbound=0;int upperbound=nelems-1;int Curin;while (True) {curin= ( Lowerbound+upperbound)/2;if (a[curin]==searchkey) {return curin;} else if (lowerbound>upperbound) {return nelems;} else {if (a[curin]<searchkey) {lowerbound=curin+1;} else {upperbound=curin-1;}}} public void display () {for (int j=0;j<nelems;j++) {System.out.print (a[j]+ "");} System.out.print ("");}} public class Ordarraytest {public static void main (string[] args) {int Maxsize=100;ordarray arr;arr=new ORdarray (maxSize); Arr.insert (Arr.insert); Arr.insert (one); Arr.insert (+); Arr.insert (+); Arr.insert (99); Arr.insert (Arr.insert); Arr.insert (arr.insert); int searchkey=55;if (Arr.find (Searchkey)!=arr.size () {System.out.println ("Found:" +searchkey);} else {System.out.println ("Can not found:" +searchkey);} Arr.display (); Arr.delete (Arr.delete); Arr.delete (99); System.out.println ("\r\n==="); Arr.display ();}}

  

Data structures and algorithms: the dichotomy Demo

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.