Professional Documents
Culture Documents
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 AlgorithmsAssiignment {
public static int search(int[] array, int key) {
int start = 0;
int end = array.length - 1;
if (array[mid] == key) {
return mid; // Key found
} else if (array[mid] < key) {
start = mid + 1; // Key may be in the right half
} else {
end = mid - 1; // Key may be in the left half
}
}
} else {
System.out.println("The key element " + searchKey + " is not found in the
array.");
}
}
}
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 AlgorithmsAssiignment {
}
System.out.println();
}
}
}
}
}
OutPut:
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;
private Employee tail;
if (head == null) {
head = newEmployee;
tail = newEmployee;
} else {
tail.next = newEmployee;
tail = newEmployee;
}
}
}
}
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:
public class AlgorithmsAssiignment {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
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:
public class AlgorithmsAssiignment {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
OutPut: