-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpractical01.java
78 lines (61 loc) · 1.98 KB
/
practical01.java
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
// Sort the given array using bubble sort and apply binary search on it.
import java.util.Scanner;
class practical01 {
public static void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("\n");
}
public static void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int flag = 1;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
flag = 0;
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
if (flag == 1) {
break;
}
}
}
public static void binarySearch(int arr[], int n, int key) {
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
System.out.println("Element found at index " + mid);
return;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
System.out.println("Element not found");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in an array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("\nEnter " + n + " elements: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("\nUnsorted array: ");
printArray(arr, n);
bubbleSort(arr, n);
System.out.println("Sorted array: ");
printArray(arr, n);
System.out.println("Enter the number you want to search: ");
int key = sc.nextInt();
binarySearch(arr, n, key);
sc.close();
}
}