Sunday, 29 April 2018

//Arrays.binarySearch() method demonstration?
//Arrays.sort() method demonstration?
import java.util.Arrays;
public class BinarySearchDemo { public static void main(String[] args) { int x[]= {10,20,30,40,50,60,70,80,90,100}; System.out.println("Index value="+Arrays.binarySearch(x, 60)); System.out.println("Insertion point="+Arrays.binarySearch(x, 500)); int y[]= {50,70,90,10,40,20,30,60,80,100};//in binary search array should be in sorted order..... Arrays.sort(y); for(int t:y) { System.out.println(t); } System.out.println("Index value="+Arrays.binarySearch(x, 30)); System.out.println("Insertion point="+Arrays.binarySearch(x, 35)); } }


o/p:-----------------------------------------------
Index value=5
Insertion point=-11
10
20
30
40
50
60
70
80
90
100
Index value=2
Insertion point=-4

note:---------------------
if element found then index value return
if element not found then insertion point return.....

No comments:

Post a Comment