0% found this document useful (0 votes)
5 views

Java Programs

Java sorting and search programs

Uploaded by

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

Java Programs

Java sorting and search programs

Uploaded by

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

import java.util.

Scanner;

public class BinarySearch {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Step 1: Accept the size of the array


System.out.print("Enter the number of elements in the
array: ");
int l = sc.nextInt(); // Number of elements

// Step 2: Input the array elements


int a[] = new int[l]; // Declaring the array
System.out.println("Enter the elements of the array in
sorted order:");
for (int i = 0; i < l; i++) {
a[i] = sc.nextInt(); // Storing array elements
}

// Step 3: Accept the number to be searched


System.out.print("Enter the number to search: ");
int ns = sc.nextInt(); // Number to search

// Step 4: Initialize variables for binary search


int start = 0; // Starting index
int end = l - 1; // Ending index
boolean found = false; // Flag to check if the number is
found
// Step 5: Perform binary search
while (start <= end) {
int mid = (start + end) / 2; // Middle index
if (ns == a[mid]) { // Check if the middle element
is the target
found = true;
break;
} else if (ns>a[mid]) { // If the target is greater, ignore
the left half
start = mid + 1;
} else { // If the target is smaller, ignore
the right half
end = mid - 1;
}
}

// Step 6: Display the result


if (found) {
System.out.println("Search successful");
} else {
System.out.println("Search unsuccessful");
}
}
}

Inserting an element
import java.util.Scanner;
public class Insertion {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input the array size and elements


System.out.print("Enter the number of elements (l): ");
int l = sc.nextInt();
int a[] = new int[l + 1]; // Create array with extra space for
insertion
System.out.println("Enter the elements of the array:");
for (int i = 0; i < l; i++) {
a[i] = sc.nextInt();
}

// Input the position and element to insert


System.out.print("Enter the position to insert : ");
int pos = sc.nextInt();
System.out.print("Enter the element to insert (IN): ");
int IN = sc.nextInt();

// Shift elements to the right and insert the new element


for (int i = l; i >= pos; i--) {
a[i] = a[i - 1];
}
a[pos - 1] = IN;
// Display the updated array
System.out.println("Array after insertion:");
for (int i = 0; i <= l; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}

Deleting an Element from an Array


import java.util.Scanner;

public class ArrayDeletion {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input the array size and elements


System.out.print("Enter the number of elements (l): ");
int l = sc.nextInt();
int a[] = new int[l];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < l; i++) {
a[i] = sc.nextInt();
}

// Input the position to delete


System.out.print("Enter the position to delete : ");
int pos = sc.nextInt();

// Shift elements to the left from the position of deletion


for (int i = pos - 1; i < l - 1; i++) {
a[i] = a[i + 1];
}

// Display the updated array


System.out.println("Array after deletion:");
for (int i = 0; i < l - 1; i++) {
System.out.println(a[i]);
}
}
}

Merging Two Arrays

import java.util.Scanner;

public class ArrayMerging {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input the size and elements of the first array


System.out.print("Enter the number of elements in the
first array: ");
int l1 = sc.nextInt();
int a1[] = new int[l1];
System.out.println("Enter the elements of the first array:");
for (int i = 0; i < l1; i++) {
a1[i] = sc.nextInt();
}

// Input the size and elements of the second array


System.out.print("Enter the number of elements in the
second array: ");
int l2 = sc.nextInt();
int a2[] = new int[l2];
System.out.println("Enter the elements of the second
array:");
for (int i = 0; i < l2; i++) {
a2[i] = sc.nextInt();
}

// Merging the arrays


int am[] = new int[l1 + l2];
for (int i = 0; i < l1; i++) {
am[i] = a1[i];
}
for (int i = 0; i < l2; i++) {
am[l1 + i] = a2[i];
}
int num=l1+l2;
// Display the merged array
System.out.println("Merged array:");
for (int i=0;i<num;i++) {
System.out.println(am[i] );
}

}
}

You might also like