1 /*ordered arrays: mainly to improve the efficiency of search2 * Find: Unordered array--order lookup, ordered array--binary find3 * where insertion is slower than unordered array4 * */5 Public classMyorderedarray {6 Private Long[] arr;7 Private intitems;8 9 PublicMyorderedarray (intmax) {Tenarr =New Long[Max]; OneItems = 0; A } - - //the number of elements in the array the Public intsize () { - returnitems; - } - + //binary Find - Public intFindLongkey) { + intLow = 0; A intHigh = Items-1; at intMid//changes in - while(Low <=High ) { -Mid = (low + high)/2; - if(Key = =Arr[mid]) { - returnmid; - } in Else if(Key >Arr[mid]) { -Low = mid + 1; to } + Else if(Key <Arr[mid]) { -High = Mid-1; the } * } $ return-1;Panax Notoginseng } - the //inserting elements + Public voidInsertLongkey) { A inti; the + for(i = 0; i < items; i++) {//Find your location first - if(Key <Arr[i]) { $ Break; $ } - } - the for(intK = items;k>i; k--) {//Moving Elements -Arr[k] = arr[k-1];Wuyi } the -Arr[i] = key;//insert element to location found Wuitems++; - } About $ //Delete Element--first judge the position - Public BooleanDeleteLongkey) { - inti =find (key); - if(i = =items) { A return false; + } the Else{ - for(intK = i; K < items; k++){ $Arr[k] = arr[k + 1]; the } theitems--; the return true; the } - } in the Public voidDisplayarray () { the for(inti = 0;i < items;i++){ AboutSystem.out.print (Arr[i] + ""); the } the System.out.println (); the } + -}
--java implementation of ordered linear table (storage structure array)