import static java.lang.System.out;
import java.util.Arrays;
class SearchArray4
{
static public void main(String...alt)
{
int[] f={5,12,36,14,20,1,3,30};
out.println("Before Sorting : ");
for(int x:f)
out.print(x+" ");
Arrays.sort(f); // Default natural sorting(Ascending Order)
out.println("\nAfter Sorting : ");
for(int x:f)
out.print(x+" ");
int result1=Arrays.binarySearch(f,1,6,3);
out.println("\n\n3 is available in index : "+result1);
int result2=Arrays.binarySearch(f,1,6,30);//30 is available in array but not available
//in this range(1-6) in unsorted array. So it returns unpredictible value.
out.println("30 is not available so its insertion point is : "+result2);
}
}
import java.util.Arrays;
class SearchArray4
{
static public void main(String...alt)
{
int[] f={5,12,36,14,20,1,3,30};
out.println("Before Sorting : ");
for(int x:f)
out.print(x+" ");
Arrays.sort(f); // Default natural sorting(Ascending Order)
out.println("\nAfter Sorting : ");
for(int x:f)
out.print(x+" ");
int result1=Arrays.binarySearch(f,1,6,3);
out.println("\n\n3 is available in index : "+result1);
int result2=Arrays.binarySearch(f,1,6,30);//30 is available in array but not available
//in this range(1-6) in unsorted array. So it returns unpredictible value.
out.println("30 is not available so its insertion point is : "+result2);
}
}
0 Comment to "Java Program to search an element in a primitive Array within a range."
Post a Comment