LAB # 5
OBJECTIVE # 1
Implement linear search and binary search algorithm (Discuss in class).
SOURCE CODE
import java.util.*;
public class Lab_5_Task_1 {
public static void main(String[] args) {
Scanner enter = new Scanner(System.in);
int[] list = new int[5];
list[0] = 11;
list[1] = 30;
list[2] = 40;
list[3] = 55;
list[4] = 60;
System.out.print(“Enter value for linear search : “);
int item_1 = enter.nextInt();
int k = 0;
while(k < list.length){
if(item_1 == list[k]){
int loc_1 = k;
System.out.print(“Item is located at position “+loc_1);
}// end if
k++;
}// end while
System.out.print(“\n\nEnter value for binary search : “);
int item_2 = enter.nextInt();
int start = 0, end = list.length;
int mid = (int)(start+end)/2;
while((start < end) && (list[mid] != item_2) ){
if(item_2 < list[mid]){
end = mid – 1;
}// end if
else {
start = mid + 1;
}// end else
mid = (int) (start + end) / 2;
}// end while
if(list[mid] == item_2){
int loc_2 = mid;
System.out.print(“Item is located at position “+loc_2);
}// end if
else {
System.out.print(“Item is not found”);
}
enter.close();
}// end method
}// end class
Output:
Enter value for linear search : 40
Item is located at position 2
Enter value for binary search : 55
Item is located at position 3
OBJECTIVE # 2
Implement linear search and binary search using array class for int, float, double and char array.
SOURCE CODE
import java.util.*;
public class Lab_5_Task_2 {
public static void main(String[] args) {
Scanner enter = new Scanner(System.in);
int[] n = {5, 19 , 7, 4, 11};
char[] c = {‘k’, ‘h’, ‘a’, ‘l’, ‘i’, ‘d’};
float[] f = {10, 48, 36, 45, 60};
double[] d = {10.5, 65.2, 8.2, 100.5, 200.6};
System.out.println(“Enter values for linear search”);
System.out.print(“For int array : “);
int item_a = enter.nextInt();
System.out.print(“For char array : “);
String item_b = enter.next();
char c_1 = item_b.charAt(0);
System.out.print(“For float array : “);
float item_c = enter.nextFloat();
System.out.print(“For double array : “);
double item_d = enter.nextDouble();
System.out.println();
linearSearchInt(n, item_a);
linearSearchChar(c, c_1);
linearSearchFloat(f, item_c);
linearSearchDouble(d, item_d);
System.out.println(“\n\nEnter values for binary search”);
System.out.print(“For int array : “);
int item_1 = enter.nextInt();
System.out.print(“For char array : “);
String item_2 = enter.next();
char c_2 = item_2.charAt(0);
System.out.print(“For float array : “);
float item_3 = enter.nextFloat();
System.out.print(“For double array : “);
double item_4 = enter.nextDouble();
Arrays.sort(n);
Arrays.sort(c);
Arrays.sort(f);
Arrays.sort(d);
System.out.println();
System.out.println(“In int array item is located at position “+Arrays.binarySearch(n, item_1));
System.out.println(“In char array item is located at position “+Arrays.binarySearch(c, c_2));
System.out.println(“In float array item is located at position “+Arrays.binarySearch(f, item_3));
System.out.println(“In double array item is located at position “+Arrays.binarySearch(d, item_4));
enter.close();
}// end method
static void linearSearchInt(int[] l1, int v1){
int k = 0;
while(k < l1.length){
if(v1 == l1[k]){
int loc_1 = k;
System.out.println(“In int array item is located at position “+loc_1);
}// end if
k++;
}// end while
}// end linearSearchInt method
static void linearSearchFloat(float[] l2, float v2){
int k = 0;
while(k < l2.length){
if(v2 == l2[k]){
int loc_2 = k;
System.out.println(“In float array item is located at position “+loc_2);
}// end if
k++;
}// end while
}// end linearSearchFloat method
static void linearSearchDouble(double[] l3, double v3){
int k = 0;
while(k < l3.length){
if(v3 == l3[k]){
int loc_3 = k;
System.out.println(“In double array item is located at position “+loc_3);
}// end if
k++;
}// end while
}// end linearSearchDouble method
static void linearSearchChar(char[] l4, char v4){
int k = 0;
while(k < l4.length){
if(v4 == l4[k]){
int loc_4 = k;
System.out.println(“In char array item is located at position “+loc_4);
}// end if
k++;
}// end while
}// end linearSearchChar method
}// end class
Output:
Enter values for linear search
For int array : 7
For char array : a
For float array : 10
For double array : 05.2
In int array item is located at position 2
In char array item is located at position 2
In float array item is located at position 0
Enter values for binary search
For int array : 4
For char array : l
For float array : 36
For double array : 8.2
In int array item is located at position 0
In char array item is located at position 5
In float array item is located at position 1
In double array item is located at position 0
HOME TASK
OBJECTIVE # 1
Write an overloaded function of public static int binarySearch(int item, int[] arr) in array search that finds all the occurrences of item in array arr[].
SOURCE CODE
import java.util.*;
public class Lab_5_Home_Task {
public static void main(String[] args) {
Scanner enter = new Scanner(System.in);
int[] list = {10,20,30,40,50};
System.out.print(“Enter Value for binary search : “);
int value = enter.nextInt();
System.out.println(“Item is located at position “+binarySearch(value, list));
enter.close();
}// end main method
public static int binarySearch(int item, int[] arr){
int start = 0, end = arr.length;
int mid = (int) (start + end)/2;
while((start < end) && (arr[mid] != item)){
if(item < arr[mid]){
end = mid – 1;
}// end if
else {
start = mid + 1;
}// end else
mid = (int) (start + end) / 2;
}// end while
int loc = 0;
if(arr[mid] == item){
loc = mid;
}
return loc;
}// end binarySearch method
}// end class
Output:
Enter Value for binary search : 30
Item is located at position 2