0% found this document useful (0 votes)
19 views8 pages

First Experiment in Oops Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

First Experiment in Oops Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Solve problems by using sequential search, binary search, and quadratic


sorting algorithms (selection, insertion)

Program:Sequential search

import java.util.Scanner;

public class SequentialSearch

public static int sequentialSearch(int[] arr, int target)

for (int i = 0; i < arr.length; i++)

if (arr[i] == target)

return i; // Return the index if found

return -1; // Return -1 if not found

public static void main(String[] args)

{
Scanner scanner = new Scanner(System.in);

// Input array size and elements

System.out.print("Enter the number of elements in the array: ");

int n = scanner.nextInt();

int[] arr = new int[n];

System.out.println("Enter the elements of the array:");

for (int i = 0; i < n; i++)

arr[i] = scanner.nextInt();

// Input target value

System.out.print("Enter the target value to search for: ");

int target = scanner.nextInt();

// Perform sequential search

int index = sequentialSearch(arr, target);

if (index != -1)

System.out.println("Target " + target + " found at index " + index);

else

{
System.out.println("Target " + target + " not found in the array");

scanner.close();

Binary Search with User Input:

import java.util.Arrays;

import java.util.Scanner;

public class BinarySearch {

public static int binarySearch(int[] arr, int target) {

int left = 0;

int right = arr.length - 1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == target) {

return mid; // Return index if found

} else if (arr[mid] < target) {

left = mid + 1; // Search the right half

} else {

right = mid - 1; // Search the left half

}
}

return -1; // Return -1 if not found

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input array size and elements (assuming array is sorted for binary search)

System.out.print("Enter the number of elements in the sorted array: ");

int n = scanner.nextInt();

int[] arr = new int[n];

System.out.println("Enter the elements of the sorted array:");

for (int i = 0; i < n; i++) {

arr[i] = scanner.nextInt();

// Input target value

System.out.print("Enter the target value to search for: ");

int target = scanner.nextInt();

// Perform binary search

int index = binarySearch(arr, target);

if (index != -1) {

System.out.println("Target " + target + " found at index " + index);

} else {
System.out.println("Target " + target + " not found in the array");

scanner.close();

Selection Sort with User Input:

import java.util.Arrays;

import java.util.Scanner;

public class SelectionSort {

public static void selectionSort(int[] arr) {

int n = arr.length;

for (int i = 0; i < n - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < n; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

// Swap the found minimum element with the first element of the
unsorted part

int temp = arr[minIndex];

arr[minIndex] = arr[i];
arr[i] = temp;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input array size and elements

System.out.print("Enter the number of elements in the array: ");

int n = scanner.nextInt();

int[] arr = new int[n];

System.out.println("Enter the elements of the array:");

for (int i = 0; i < n; i++) {

arr[i] = scanner.nextInt();

// Perform selection sort

System.out.println("Original array: " + Arrays.toString(arr));

selectionSort(arr);

System.out.println("Sorted array: " + Arrays.toString(arr));

scanner.close();

}
Insertion Sort with User Input:

import java.util.Arrays;

import java.util.Scanner;

public class InsertionSort {

public static void insertionSort(int[] arr) {

int n = arr.length;

for (int i = 1; i < n; i++) {

int key = arr[i];

int j = i - 1;

// Move elements of arr[0..i-1], that are greater than key, to one


position ahead of their current position

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input array size and elements

System.out.print("Enter the number of elements in the array: ");


int n = scanner.nextInt();

int[] arr = new int[n];

System.out.println("Enter the elements of the array:");

for (int i = 0; i < n; i++) {

arr[i] = scanner.nextInt();

// Perform insertion sort

System.out.println("Original array: " + Arrays.toString(arr));

insertionSort(arr);

System.out.println("Sorted array: " + Arrays.toString(arr));

scanner.close();

You might also like