Binary Character quick search

Source: Internet
Author: User
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

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.