0% found this document useful (0 votes)
9 views4 pages

Bit

The document contains various Java code snippets implementing different algorithms and data structures, including deletion from an array, binary search, bubble sort, insertion sort, selection sort, linear search, and stack operations. Each code section is structured with a main method and user input for array elements or search values. The document illustrates basic programming concepts and techniques in Java.
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)
9 views4 pages

Bit

The document contains various Java code snippets implementing different algorithms and data structures, including deletion from an array, binary search, bubble sort, insertion sort, selection sort, linear search, and stack operations. Each code section is structured with a main method and user input for array elements or search values. The document illustrates basic programming concepts and techniques in Java.
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

import [Link].

Scanner;
public class Delete import [Link];
{
public static void main(String[] args) public class QuickSort
{ {
int n, x, flag = 1, loc = 0;
Scanner s = new Scanner([Link]); public static void main(String[] args)
[Link]("Enter no. of elements in array:"); {
n = [Link](); int n,array[],i,pos,x;
int a[] = new int[n]; Scanner scanner = new Scanner([Link]);
[Link]("Enter the elements:");
for (int i = 0; i < n; i++) [Link]("Enter the number of elements: ");
{ n = [Link]();
a[i] = [Link]();
} array = new int[n+1];
[Link]("Enter the element you want to delete:");
x = [Link](); [Link]("Enter the elements:");
for (int i = 0; i < n; i++) for ( i = 0; i < n; i++)
{ {
if(a[i] == x) array[i] = [Link]();
{ }
flag =1;
loc = i; [Link]("Enter the position where you want to insert element:");
break; pos = [Link]();
[Link]("Enter the element you want to insert:");
} x = [Link]();
else for(i=n-1; i>=(pos-1);i--)
{ {
flag = 0; array[i+1]=array[i];
} }
} array[pos-1]=x;
if(flag == 1) [Link]("After inserting:");
{
for(int i = loc+1; i < n; i++) for (i = 0; i < n+1; i++)
{ {
a[i-1] = a[i]; [Link](array[i]+" ");
} }
[Link]("Array After Deleting:");
for (int i = 0; i <=n-2; i++) }
{ }
[Link](a[i]+" ");
}
}
else
{
[Link]("Element not found");
}
}
}

import [Link]; mid=(first+last)/2; // Online Java Compiler


// Use this editor to write, compile and run your Java code online
public class Binarysearch } import [Link];
{ if(first>last)
public static void main(String[] args) public class RecursiveBinarySearch {
{ [Link]("\n The element is not available in the given array");
int i,j,temp,n,array[],first,last,mid,search; // Recursive binary search function
Scanner s = new Scanner([Link]); } public static int binarySearch(int[] array, int target, int left, int right) {
} if (left > right) {
[Link]("Enter the size : "); return -1; // Target is not present in array
n = [Link](); }
array = new int[n];
int mid = left + (right - left) / 2; // Avoids overflow compared to (left + right) / 2
[Link]("Enter the elements:");
for ( i = 0; i < n; i++) if (array[mid] == target) {
{ return mid; // Target found
array[i] = [Link](); } else if (array[mid] > target) {
} return binarySearch(array, target, left, mid - 1); // Search in the left half
} else {
// Bubble Sort return binarySearch(array, target, mid + 1, right); // Search in the right half
for (i = 0; i < n - 1; i++) }
{ }
for ( j = 0; j < n - i - 1; j++) public static void main(String[] args) {
{ Scanner scanner = new Scanner([Link]);
if (array[j] > array[j + 1])
{ // Read the size of the array
// swap array[j] and array[j+1] [Link]("Enter the size of the array: ");
temp = array[j]; int size = [Link]();
array[j] = array[j + 1];
array[j + 1] = temp; int[] array = new int[size];
}
} // Read the array elements
} [Link]("Enter the elements of the array in Ascending order:");
for (int i = 0; i < size; i++) {
[Link]("Sorted list of elements:"); array[i] = [Link]();
for (i=0;i<n;i++) { }
// Read the search value
[Link](array[i]+ " "); [Link]("Enter the target value to search: ");
} int target = [Link]();

[Link]("\n Enter the elament to search"); // Perform binary search


search= [Link](); int result = binarySearch(array, target, 0, size - 1);
first=0;
last=n-1; // Output the result
mid=(first+last)/2; if (result != -1) {
while(first<=last) [Link]("Target found at index: " + result);
{ } else {
if(array[mid]<search) [Link]("Target not found in the array.");
first=mid+1; }
else if(array[mid]==search) }
{ }

[Link]("\n The elements found at index No:"+mid+"position "+(mid+1));


break;
}
else
last=mid-1;
import [Link]; import [Link];

public class BubbleSort public class InsertionSort


{ {
public static void main(String[] args)
{ public static void main(String[] args)
int i,j,temp,n,array[]; {
Scanner s = new Scanner([Link]); int arr[],n,i,j,key;
Scanner s = new Scanner([Link]);
[Link]("Enter the number of elements to sort: ");
n = [Link](); [Link]("Enter the number of elements: ");
array = new int[n]; n = [Link]();

[Link]("Enter the elements:"); arr = new int[n];


for ( i = 0; i < n; i++)
{ [Link]("Enter " + n + " integers:");
array[i] = [Link](); for ( i = 0; i < n; i++)
} {
arr[i] = [Link]();
// Bubble Sort }
for (i = 0; i < n - 1; i++)
{ // Sorting the array using insertion sort
for ( j = 0; j < n - i - 1; j++) for ( i = 1; i < n; i++)
{ {
if (array[j] > array[j + 1]) key = arr[i];
{ j = i - 1;
// swap array[j] and array[j+1] while (j >= 0 && arr[j] > key)
temp = array[j]; {
array[j] = array[j + 1]; arr[j + 1] = arr[j];
array[j + 1] = temp; j--;
} }
} arr[j + 1] = key;
} }

[Link]("Sorted list of elements:"); [Link]("Sorted array:");


for (i=0;i<n;i++) { for (i = 0; i < n; i++)
{
[Link](array[i]+ " "); [Link](arr[i] + " ");
} }
}
}
}
}

import [Link]; // Only one element → reset queue


public class LQueue front = -1; // Delete one element
{ rear = -1; int elem = [Link]();
int SIZE = 5; // Size of Linear Queue } else { if (elem != -1) {
int front, rear; front++; [Link]("Deleted Element is " + elem);
int items[] = new int[SIZE]; } }
return element;
// Constructor } [Link]();
}
LQueue() // Insert again
{ // Display queue [Link](70);
front = -1; void display() { [Link]();
rear = -1; if (isEmpty()) { }
} [Link]("Empty Queue"); }
} else {
// Check if queue is full [Link]("Queue: ");
boolean isFull() { for (int i = front; i <= rear; i++) {
return (rear == SIZE - 1); [Link](items[i] + " ");
} }
[Link]();
// Check if queue is empty [Link]("Front -> " + front);
boolean isEmpty() { [Link]("Rear -> " + rear);
return (front == -1 || front > rear); }
} }

// Insert element // Main method


void enQueue(int element) { public static void main(String[] args) {
if (isFull()) { LQueue q = new LQueue();
[Link]("Queue is full");
} else { // Try deleting from empty queue
if (front == -1) front = 0; // first element [Link]();
items[++rear] = element;
[Link]("Inserted " + element); // Insert elements
} [Link](10);
} [Link](20);
[Link](30);
// Delete element [Link](40);
int deQueue() { [Link](50);
if (isEmpty()) {
[Link]("Queue is empty"); // Overflow example
return -1; [Link](60);
} else {
int element = items[front]; // Display queue
if (front == rear) { [Link]();
import [Link];
public class LinearsearchDemo
{
int linearRecursion(int[] arrnum,int s,int l,int k)
public class SubarrayPatternMatching
{ {
if(l<s) public static void main(String[] args)
return -1; {
if(arrnum[s]==k)
int[] arr = {1, 2, 3, 4, 5, 6, 7};
return s;
return linearRecursion(arrnum,s+1,l,k); int[] pattern = {3, 4, 5};
}
if (containsPattern(arr, pattern))
public static void main(String[] args)
{
{
LinearsearchDemo obj=new LinearsearchDemo(); [Link]("Pattern found in array.");
int a,len,key,array[]; }
Scanner sc=new Scanner([Link]); else
[Link]("Enter ARRAY LENGTH"); {
len=[Link]();
array=new int[len]; [Link]("Pattern not found.");
[Link]("Enter those "+len+"elements:"); }
for(a=0;a<len;a++) }
array[a]=[Link]();
[Link]("Enter value to find:");
key=[Link]();
public static boolean containsPattern(int[] arr, int[] pattern)
int index=[Link](array,0,len-1,key); {
if(index!=-1) for (int i = 0; i <= [Link] - [Link]; i++)
[Link](key+"is found at"+(index+1)); {
else
boolean match = true;
[Link](key+"not found");
[Link](); for (int j = 0; j < [Link]; j++)
{
}
}
if (arr[i + j] != pattern[j])
{

match = false;
break;
}
}
if (match)
{
return true;
}
}
return false;
}
}

import [Link]; import [Link]; }

public class SelectionSort { public class Stack {

public static void main(String[] args)


private int[] arr; // Method to get the top element of the stack
{
private int top; public int peek() {
int n,arr[],i,minIndex,j,temp;
Scanner scanner = new Scanner([Link]); if (top == -1) {
[Link]("Enter the number of elements: ");
n = [Link](); // Constructor to initialize the stack [Link]("Stack is empty");
arr = new int[n];
[Link]("Enter the elements:"); public Stack(int size) { return -1;
for (i = 0; i < n; i++) arr = new int[size]; } else {
{
arr[i] = [Link](); top = -1; return arr[top];
}
} }

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


{
minIndex = i; // Method to push an element onto the stack
for (j = i + 1; j < n; j++)
{ public void push(int num) { // Method to check if the stack is empty
if (arr[j] < arr[minIndex])
if (top == [Link] - 1) { public boolean isEmpty() {
{
minIndex = j; [Link]("Stack is full"); return top == -1;
}
} } else { }
temp = arr[minIndex]; top++;
arr[minIndex] = arr[i];
arr[i] = temp; arr[top] = num; public void display() {
}
} if (top == -1) {

[Link]("Sorted array:"); } [Link]("Stack is empty");


for (i = 0; i < n; i++)
{ } else {
[Link](arr[i] + " ");
} // Method to pop an element from the stack [Link]("Stack elements: ");
}
} public int pop() { for (int i = top; i >= 0; i--) {

if (top == -1) { [Link](arr[i] + " ");

[Link]("Stack Underflow"); }

return -1; [Link]();

} else { }

int poppedElement = arr[top]; }

top--;

return poppedElement; public static void main(String[] args) {

} [Link]("Initialize a stack:");
Stack stack = new Stack(5);

[Link]("Is the stack empty? " + [Link]());

[Link]("\nInput some elements on the stack:");

[Link](1);

[Link](2);

[Link](3);

[Link](4);

[Link](5);

[Link]();

[Link]("\nTop element of the stack: " + [Link]());

[Link]("\nRemove two element from the stack:");

[Link]();

[Link]();

[Link]();

[Link]("\nTop element of the stack after popping: " + [Link]());

[Link]("\nIs the stack empty? " + [Link]());

You might also like