- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVOperations.java
More file actions
Latest commit
62 lines (60 loc) · 1.94 KB
/
Copy pathVOperations.java
File metadata and controls
62 lines (60 loc) · 1.94 KB
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
//Link: https://onecompiler.com/java/3xctrjg7v
importjava.io.*;
publicclassVOperations {
publicint[] initializer(){
int[] nums = {11,6,77,8,5,44,6,9,442,86,73,49,68,82};
returnnums;
}
publicintlinearSearch(int[] nums){
intkey = 22;
for(inti=0;i<nums.length;i++){
if(nums[i] == key){
returni;
}
}
return -1;
}
publicintbinSearch(int[] nums ,intl, intn, intkey){
intr = nums.length;
if (r >= l) {
intmid = l + (r - l) / 2;
if (nums[mid] == key)
returnmid;
if (nums[mid] > key)
returnbinSearch(nums, l, mid - 1, key);
returnbinSearch(nums, mid + 1, r, key);
}
return -1;
}
publicint[] bubbleSort(int[] nums){
intn = nums.length;
for (inti = 0; i < n-1; i++)
for (intj = 0; j < n-i-1; j++)
if (nums[j] > nums[j+1])
{
inttemp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
returnnums;
}
publicstaticvoidmain(String[] args)throwsIOException{
VOperationsobj = newVOperations();
int[] nums = obj.initializer();
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
System.out.printf("Enter 0 for Linear Search\n 1 for binary search\n 2 for BubbleSort: ");
intch = Integer.parseInt(br.readLine());
switch (ch) {
case0:
System.out.println(obj.linearSearch(nums));
break;
case1:
System.out.println(obj.binSearch(nums,0,nums.length,11));
break;
case2:
for (inti = 0; i < nums.length;i++){
System.out.print(obj.bubbleSort(nums)[i]+" ");
}
}
}
}