Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Student name: Hanan Abuzeyah

Student ID: 202108085

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 {

public static int binarySearch(int[] list, int key) {


int left = 0;
int right = list.length - 1;

while (left <= right) {


int mid = left + (right - left) / 2;

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
}
}

return -1; // Key not found


}
public static void main(String[] args) {
int[] list = {33, 55, 11, 99, 22, 88, 77, 66, 44};
int key = 99;

// Sort the list in ascending order


java.util.Arrays.sort(list);

// Perform binary search


int location = binarySearch(list, key);

// Display the result


if (location != -1) {
System.out.print("The key element " + key + " is found at index " + location +
"\n");
} else {
System.out.print("The key element " + key + " is not found in the list" +
"\n");
}
}

}
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};

// Perform bubble sort


bubbleSort(list);

// Display the sorted list


System.out.print("Sorted list: ");
for (int num : list) {
System.out.print(num + " ");
}
System.out.print("\n");
}

public static void bubbleSort(int[] list) {


int n = list.length;

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


for (int j = 0; j < n - i - 1; j++) {
if (list[j] > list[j + 1]) {
// Swap elements at j and j+1
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
}
}
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;

public Employee(int employeeId, String employeeName, double salary) {


this.employeeId = employeeId;
this.employeeName = employeeName;
this.salary = salary;
this.next = null;
}
}

class EmployeeLinkedList {
private Employee head;

public void insertAtBeginning(int employeeId, String employeeName, double salary) {


Employee newEmployee = new Employee(employeeId, employeeName, salary);
if (head == null) {
head = newEmployee;
} else {
newEmployee.next = head;
head = newEmployee;
}
}

public void displayEmployees() {


if (head == null) {
System.out.println("No employees in the list.");
} else {
Employee current = head;
System.out.println("Employee Details:");
while (current != null) {
System.out.println("Employee ID: " + current.employeeId);
System.out.println("Employee Name: " + current.employeeName);
System.out.println("Salary: " + current.salary);
System.out.println("----------------------");
current = current.next;
}
}
}
}

public class LinkedListExample {


public static void main(String[] args) {
EmployeeLinkedList employeeList = new EmployeeLinkedList();

// Insert employees at the beginning of the list


employeeList.insertAtBeginning(1, "John Doe", 5000);
employeeList.insertAtBeginning(2, "Jane Smith", 6000);
employeeList.insertAtBeginning(3, "Mike Johnson", 5500);

// Display employee details


employeeList.displayEmployees();
}
}

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 StackExample {


public static void main(String[] args) {
Stack<String> stack = new Stack<>();

// Pushing elements to the stack


stack.push("Group1");
stack.push("Group2");
stack.push("Group3");
stack.push("Group4");

System.out.println("Initial Stack: " + stack);

// Popping an element from the stack


String poppedElement = stack.pop();
System.out.println("Popped Element: " + poppedElement);

// Pushing another element to the stack


stack.push("Group5");

System.out.println("Updated Stack: " + 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 QueueExample {


public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();

// Enqueue elements to the queue


queue.add("A");
queue.add("B");

System.out.println("Initial Queue: " + queue);

// Dequeue an element from the queue


String dequeuedElement = queue.remove();
System.out.println("Dequeued Element: " + dequeuedElement);

// Enqueue more elements to the queue


queue.add("C");
queue.add("D");

System.out.println("Updated Queue: " + queue);


}
}

OutPut:

You might also like