Problem:
Given an element x and a set of data that is in strictly ascending numerical order find whether or not x is preset in the set.
Solution:
package array.programs;
import java.util.Arrays;
import java.util.Scanner;
public class BinarySearch {
int[] array;
int lower;
int middle;
int upper;
int searchElement;
int arrayLimit;
public static void main(String[] args) {
BinarySearch binarySearch = new BinarySearch();
binarySearch.initializeTheArray();
int result = binarySearch.doBinarySearch(0, binarySearch.arrayLimit - 1);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index "+result);
}
private void initializeTheArray(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the array elements limit");
arrayLimit = scanner.nextInt();
array = new int[arrayLimit];
for (int i = 0; i < array.length; i++) {
System.out.println("enter the element");
array[i] = scanner.nextInt();
}
System.out.println("enter the element which need to be found");
searchElement = scanner.nextInt();
scanner.close();
Arrays.sort(array);
}
private int doBinarySearch(int lowerLimit, int upperLimit){
while(lowerLimit <= upperLimit){
int middleIndex = lowerLimit + (upperLimit - lowerLimit) / 2;
if(array[middleIndex] == searchElement){
return middleIndex;
}
if(array[middleIndex] < searchElement ){
lowerLimit = middleIndex + 1;
}
else {
upperLimit = middleIndex - 1;
}
}
return -1;
}
}
Output:
enter the array elements limit
5
enter the element
3
enter the element
23
enter the element
43
enter the element
44
enter the element
55
enter the element which need to be found
44
Element found at index 3
Given an element x and a set of data that is in strictly ascending numerical order find whether or not x is preset in the set.
Solution:
package array.programs;
import java.util.Arrays;
import java.util.Scanner;
public class BinarySearch {
int[] array;
int lower;
int middle;
int upper;
int searchElement;
int arrayLimit;
public static void main(String[] args) {
BinarySearch binarySearch = new BinarySearch();
binarySearch.initializeTheArray();
int result = binarySearch.doBinarySearch(0, binarySearch.arrayLimit - 1);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index "+result);
}
private void initializeTheArray(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the array elements limit");
arrayLimit = scanner.nextInt();
array = new int[arrayLimit];
for (int i = 0; i < array.length; i++) {
System.out.println("enter the element");
array[i] = scanner.nextInt();
}
System.out.println("enter the element which need to be found");
searchElement = scanner.nextInt();
scanner.close();
Arrays.sort(array);
}
private int doBinarySearch(int lowerLimit, int upperLimit){
while(lowerLimit <= upperLimit){
int middleIndex = lowerLimit + (upperLimit - lowerLimit) / 2;
if(array[middleIndex] == searchElement){
return middleIndex;
}
if(array[middleIndex] < searchElement ){
lowerLimit = middleIndex + 1;
}
else {
upperLimit = middleIndex - 1;
}
}
return -1;
}
}
Output:
enter the array elements limit
5
enter the element
3
enter the element
23
enter the element
43
enter the element
44
enter the element
55
enter the element which need to be found
44
Element found at index 3
No comments:
Post a Comment