1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
import java.util.Arrays;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.function.BiFunction;
class Array {
private double[] arr;
Array() throws Exception {
takeInput();
}
void takeInput() throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length: ");
int size = sc.nextInt();
if (size < 0)
throw new Exception("Array length can not be negative");
arr = new double[size];
System.out.print("Enter the array elements: ");
for (int i = 0; i < size; i++)
arr[i] = sc.nextDouble();
}
void display() {
System.out.print("The array elements are:");
for (int i = 0; i < arr.length; i++)
System.out.print(" " + arr[i]);
System.out.println();
}
double get(int i) throws IndexOutOfBoundsException {
return arr[i];
}
void set(int i, double val) throws IndexOutOfBoundsException {
arr[i] = val;
}
void sort() {
Arrays.sort(arr);
}
int size() { return arr.length; }
}
class ArrayOperations {
static int linearSearch(Array arr, double item) {
int size = arr.size();
for (int i = 0; i < size; i++) {
if (arr.get(i) == item)
return i;
}
return -1;
}
static int binarySearch(Array arr, double item) {
int size = arr.size();
int leftIndex = 0, rightIndex = size - 1;
while (leftIndex <= rightIndex) {
int midIndex = leftIndex + (rightIndex - leftIndex) / 2;
double midValue = arr.get(midIndex);
if (item == midValue) {
return midIndex;
} else if (item < midValue) {
rightIndex = midIndex - 1;
} else {
leftIndex = midIndex + 1;
}
}
return -1;
}
}
class ArraySearch {
static void menu() {
System.out.println(
"Menu:\n" +
" 1. Re-enter array\n" +
" 2. Display array elements\n" +
" 3. Perform linear search\n" +
" 4. Perform binary search\n" +
" 5. Exit\n");
}
static void performSearch(Scanner sc, Array arr, BiFunction<Array, Double, Integer> searcher) {
arr.display();
System.out.print("Enter the element to find: ");
double elem = sc.nextDouble();
int idx = searcher.apply(arr, elem);
if (idx == -1) {
System.out.println("The element " + elem + " was not found in the array.");
} else {
System.out.println("The element " + elem + " was found in the array at position " + (idx + 1) + ".");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Menu-driven program of array searching\n");
System.out.println("Enter the array:");
Array arr = null;
while (true) {
try {
if (arr == null) arr = new Array();
menu();
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
arr.takeInput();
arr.display();
break;
case 2:
arr.display();
break;
case 3:
performSearch(sc, arr, ArrayOperations::linearSearch);
break;
case 4:
System.out.println("Array is sorted before binary search.");
arr.sort();
performSearch(sc, arr, ArrayOperations::binarySearch);
break;
case 5:
System.out.println("Bye.");
return;
default:
System.out.println("Invalid choice, try again.");
}
} catch (InputMismatchException e) {
System.err.println("Error: Invalid input, try again");
sc.nextLine();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
}
|