Professional Documents
Culture Documents
H Algog
H Algog
1. Write a program to search for the key element from the give list and display its location using
Binary Search. The list is: 33, 55, 11, 99, 22, 88, 77, 66, 44, and the key is 99.
Solution:
Code:
public class BinarySearchExample {
if (list[mid] == key) {
return mid; // Key found
} else if (list[mid] < key) {
left = mid + 1; // Key may be in the right half
} else {
right = mid - 1; // Key may be in the left half
}
}
}
OutPut:
Write a program to sort the given list of elements using bubble sort: 33, 55, 11, 99, 22, 88, 77, 66, 44.
Solution:
Code:
public class BubbleSortExample {
public static void main(String[] args) {
int[] list = {33, 55, 11, 99, 22, 88, 77, 66, 44};
Write a program to create a linked list of employees where each employee node will contain the following
attributes: EmployeeId, EmployeeName ,Salary Create a method to insert an employee at the beginning of
the list and a method to display the details of all employees in the list.
Solution:
Code:
class Employee {
int employeeId;
String employeeName;
double salary;
Employee next;
class EmployeeLinkedList {
private Employee head;
OutPut:
Write a program to perform the following operations on a stack:
Push Group1 , push Group2 , push Group3, push Group4, pop, push Group5, pop.
Solution:
Code:
OutPut:
Write a program to implement a queue and perform the following operations on a queue:
enqueue(A), enqueue(B), dequeue( ), enqueue(C), enqueue(D), dequeue( ).
Solution:
Code:
OutPut: