Code:
Import Java. util. arraylist; import Java. util. list; public class bit {int Max; int min; int [] arr; public bit (INT [] arrinput) {// find the extreme value for (INT I = 0; I <arrinput. length; I ++) {If (max <arrinput [I]) {max = arrinput [I];} If (min> arrinput [I]) {min = arrinput [I] ;}// create an array arr = new int [max-min + 1]; // array interpolation for (int I: arrinput) {int Index = I-min; arr [Index] ++ ;}} public Boolean hasd Uplicateitem () {for (INT I = 0; I <arr. length; I ++) {int value = arr [I]; If (value> 1) {return true ;}} return false;} public list <integer> getsortedlist () {list <integer> ls = new arraylist <integer> (); For (INT I = 0; I <arr. length; I ++) {int value = arr [I]; If (value! = 0) {for (Int J = 0; j <value; j ++) {ls. add (min + I) ;}}return ls ;}public list <integer> getuniquesortedlist () {list <integer> ls = new arraylist <integer> (); for (INT I = 0; I <arr. length; I ++) {int value = arr [I]; If (value! = 0) {ls. add (min + I) ;}} return ls;} public int [] getuniquesortedarray () {list <integer> ls = getuniquesortedlist (); int [] arr = new int [ls. size ()]; for (INT I = 0; I <arr. length; I ++) {arr [I] = ls. get (I) ;}return arr ;} /*** Binary Search ** @ Param sortedarray * sorted array * @ Param seachvalue * searched value * @ return element subscript, if no value is found,-1 */public static int binsearch (INT [] sortedarray, int seachvalue) is returned {// left boint leftbou ND = 0; // right boundary int rightbound = sortedarray. length-1; // The current subscript position int curr; while (true) {// located in the middle of the Left and Right Bound curr = (leftbound + rightbound)/2; if (sortedarray [curr] = seachvalue) {// find the return curr;} else if (leftbound> rightbound) {// The left boundary is greater than the right boundary, return-1;} else {If (sortedarray [curr] <seachvalue) {// when the value of the current subscript is smaller than the value of the search, shorten the left boundary leftbound = curr + 1;} else {// when the value of the current subscript is greater than the searched value, shorten the right boundary Rightbound = curr-1 ;}}} public static void main (string [] ARGs) {int [] arr = {-2,-1, 3, 5, 7, 9, 30, 4,-2, 5, 8, 3}; bit B = new bit (ARR); system. out. println ("sorted array"); int [] arr2 = B. getuniquesortedarray (); For (INT I = 0; I <arr2.length; I ++) {system. out. println (I + ":" + arr2 [I]);} int [] arr3 = {2, 5, 7,-2, 90}; For (INT I = 0; I <arr3.length; I ++) {int Index = bit. binsearch (arr2, arr3 [I]); If (index! =-1) {system. out. println (arr3 [I] + "the number of" + index + "" in the array arr2);} else {system. out. println (arr3 [I] + "not in array arr2 ");}}}}
Output:
Sorted array 0:-21:-12: 33: 44: 55: 76: 87: 98: 302 not in array arr2 5 in array arr2 4th in array arr2 7 in array arr2 5th-2 in array arr2 0th 90 not in array arr2