Import Java. util. callback;/***** @ author luozhonghua **/public class charsearchandsort {static void kuaisu (char [] A, int left, int right) // fast character sorting {int F, L, R; char t; L = left; r = right; F = A [(left + right)/2]; while (L <r) {While (A [l] <F) ++ L; while (A [R]> F) -- r; If (L <= r) {T = A [l]; A [l] = A [R]; A [R] = T; ++ L; -- r ;}} if (L = r) l ++; If (left <r) kuaisu (A, left L-1); // recursively call if (L <right) kuaisu (, R + 1, right); // recursive call} Static int searchfun (char [] A, int N, char X) {// semi-query int mid, low, high; Low = 0; high = n-1; while (low <= high) {mid = (low + high)/2; if (a [Mid] = x) return mid; // locate else if (a [Mid]> X) High = mid-1; elselow = Mid + 1;} return-1; // not found} public static void main (string [] ARGs) {char [] STR = new char [80]; int N; char [] x = new char [80]; system. out. print ("input a string:"); input = new partition (system. in); STR = input. next (). tochara Rray (); // input string n = Str. length; system. out. print ("Before sorting: \ n"); system. out. println (STR); // output kuaisu (STR, 0, N-1); // binary sorting system. out. print ("sorted: \ n"); system. out. print (STR); // output // binary search system. out. print ("\ n"); system. out. println ("Enter the character to be searched:"); effecinpu = new partition (system. in); X = inpu. next (). tochararray (); // enter the number int n = searchfun (STR, n, x [0]); // query if (n <0) // output the search result system. out. println ("no data found:" + x); else System. Out. println ("data:" + X [0] + "located at the" + (n + 1) + "element of the array. ");}}
Binary Character quick search