Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Binary Search

Uploaded by

YOGENDRA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Binary Search

Uploaded by

YOGENDRA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Binary Search

public class BinarySearch {

// Method to perform binary search


public static int binarySearch(int[] arr, int target) {
int left = 0; // Starting index
int right = arr.length - 1; // Ending index

while (left <= right) {


int mid = left + (right - left) / 2; // Calculate the middle index

// Check if the target is present at mid


if (arr[mid] == target) {
return mid; // Target found, return the index
}

// If target is greater, ignore the left half


if (arr[mid] < target) {
left = mid + 1;
}
// If target is smaller, ignore the right half
else {
right = mid - 1;
}
}
return -1; // Target not found
}

public static void main(String[] args) {


int[] arr = {2, 3, 4, 10, 40}; // Sorted array
int target = 10; // Element to search for

int result = binarySearch(arr, target);


if (result == -1) {
System.out.println("Element not present in the array");
} else {
System.out.println("Element found at index: " + result);
}
}
}

Output: -
Element found at index : 3
Bubble Sort

public class BubbleSort {

// Method to perform bubble sort


public static void bubbleSort (int [] arr) {
int n = arr. length; // Get the length of the array

// Traverse through all array elements


for (int i = 0; i < n - 1; i++) {
// Last i elements are already in place
for (int j = 0; j < n - i - 1; j++) {
// Swap if the element found is greater than the next element
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

// Method to print the array


public static void printArray(int[] arr) {
for (int value : arr) {
System.out.print(value + " ");
}
System.out.println(); // Move to the next line
}

public static void main(String[] args) {


int[] arr = {64, 34, 25, 12, 22, 11, 90}; // Unsorted array

System.out.println("Unsorted array:");
printArray(arr); // Print the original array

bubbleSort(arr); // Sort the array

System.out.println("Sorted array:");
printArray(arr); // Print the sorted array
}
}

Output:-
Unsorted array:
64 34 25 12 22 11 90
Sorted array:
11 12 22 25 34 64 90
Stack

class Stack {
private int maxSize; // Maximum size of the stack
private int[] stackArray; // Array to hold the stack elements
private int top; // Index of the top element

// Constructor to initialize the stack


public Stack(int size) {
this.maxSize = size;
this.stackArray = new int[maxSize];
this.top = -1; // Stack is initially empty
}

// Method to add an element to the stack


public void push(int value) {
if (top < maxSize - 1) {
stackArray[++top] = value; // Increment top and add value
System.out.println(value + " pushed to stack.");
} else {
System.out.println("Stack is full. Cannot push " + value);
}
}

// Method to remove an element from the stack


public int pop() {
if (top >= 0) {
return stackArray[top--]; // Return top value and decrement top
} else {
System.out.println("Stack is empty. Cannot pop.");
return -1; // Return -1 if stack is empty
}
}

// Method to display the elements of the stack


public void display() {
if (top >= 0) {
System.out.print("Stack elements: ");
for (int i = top; i >= 0; i--) {
System.out.print(stackArray[i] + " ");
}
System.out.println();
} else {
System.out.println("Stack is empty.");
}
}
}

public class StackProgram {


public static void main(String[] args) {
Stack stack = new Stack(5); // Create a stack of size 5

stack.push(10); // Push elements onto the stack


stack.push(20);
stack.push(30);
stack.display(); // Display stack elements

System.out.println(stack.pop() + " popped from stack."); // Pop an


element
stack.display(); // Display stack elements again

stack.push(40); // Push another element


stack.push(50);
stack.push(60); // This will show that the stack is full
stack.display(); // Display final stack elements
}
}

Output:
When you run the program, the output will be:

10 pushed to stack.
20 pushed to stack.
30 pushed to stack.
Stack elements: 30 20 10
30 popped from stack.
Stack elements: 20 10
40 pushed to stack.
50 pushed to stack.
Stack is full. Cannot push 60
Stack elements: 50 40 20 10

You might also like