Character search in Character Array Using Binary Search

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing unsorted char array
      char charArr[] = {'a', 'c', 'b', 'e','d'};

      // sorting array
      Arrays.sort(charArr);

      // let us print all the elements available in list
      System.out.println("The sorted char array is:");
      for (char number : charArr) {
         System.out.println("Number = " + number);
      }

      // entering the value to be searched
      char searchVal = 'e';

      int retVal = Arrays.binarySearch(charArr, searchVal);

      System.out.println("The index of e is : " + retVal);
   }
}

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...