Practical1
Practical1
1. Write a java program to implement a sequential search algorithm for searching for a
key
Program:
LinearSearch.java:
package mypack;
import java.util.Scanner;
public class LinearSearch {
public static int linearSearch(int[] num, int size, int key) {
for (int i = 0; i < size; i++) {
if (num[i] == key) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the array size: ");
int size = scanner.nextInt();
int[] num = new int[size];
System.out.println("Enter the array elements: ");
for (int i = 0; i < size; i++) {
num[i] = scanner.nextInt();
}
System.out.println("Enter the target or key: ");
int key = scanner.nextInt();
int output = linearSearch(num, size, key);
if (output == -1) {
System.out.println("The " + key + " is not found");
} else {
System.out.println("The " + key + " is found at index " + output);
}
scanner.close();
}
}
Output:
2. Write a java program to implement a recursive binary search algorithm for
searching for a key.
Program:
RecursiveBinarySearch.java
package mypack;
import java.util.Scanner;
public class RecursiveBinarySearch {
public static int recursiveBinarySearch(int[] num, int low, int high, int key) {
if (low > high) {
return -1; // Base case: key not found
}
int mid = (low + high) / 2;
// Check if the mid element is the key
if (num[mid] == key) {
return mid;
}
// If key is greater, search in the right half
else if (num[mid] < key) {
return recursiveBinarySearch(num, mid + 1, high, key);
}
// If key is smaller, search in the left half
else {
return recursiveBinarySearch(num, low, mid - 1, key);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the array size: ");
int size = scanner.nextInt();
int[] num = new int[size];
System.out.println("Enter the sorted array elements: ");
for (int i = 0; i < size; i++) {
num[i] = scanner.nextInt();
}
System.out.println("Enter the target or key: ");
int key = scanner.nextInt();
// Call the recursive binary search with initial low and high bounds
int output = recursiveBinarySearch(num, 0, size - 1, key);
if (output == -1) {
System.out.println("The " + key + " is not found");
} else {
System.out.println("The " + key + " is found at index " + output);
}
scanner.close();
}
}
Output:
3. Write a java program to implement a Bubble sort algorithm to sort 15 numbers.
Program:
BubbleSort.java
package mypack;
import java.util.Scanner;
public class BubbleSort {
public static void bubbleSort(long[] list, long size) {
long hold, walker;
int flag;
for (hold = 0; hold < size - 1; hold++) {
flag = 0; // no swap
for (walker = 0; walker < size - hold - 1; walker++) {
if (list[(int) walker] > list[(int) (walker + 1)]) {
long t;
t = list[(int) walker];
list[(int) walker] = list[(int) (walker + 1)];
list[(int) (walker + 1)] = t;
flag = 1; // done swapping
}
}
if (flag == 0) {
break;
}
System.out.println("\n\nPass: " + (hold + 1) + " : ");
for (long i = 0; i < size; i++) {
System.out.print(list[(int) i] + " ");
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the array size: ");
long size = scanner.nextLong();
long[] num = new long[(int) size];
System.out.println("Enter the array elements :");
for (int i = 0; i < size; i++) {
num[i] = scanner.nextLong();
}
System.out.println("\nUnsorted Array: ");
for (int i = 0; i < size; i++) {
System.out.print(num[i] + " ");
}
System.out.println("\n\nSorted array passes:");
bubbleSort(num, size); // num array will be passed by reference and size will be passed
by value
System.out.println("\n\nSorted Array: ");
for (int i = 0; i < size; i++) {
System.out.print(num[i] + " ");
}
scanner.close();
}
}
Output:
Output: