0% found this document useful (0 votes)
46 views

Lujain Alg

The document contains the solutions to 7 programming assignments submitted by student Lujain Ahmed with ID 202102508. The assignments include programs to: 1) Perform a binary search to find an element in a sorted list 2) Sort a list of elements using bubble sort 3) Create a linked list of employee data and methods to insert and display employees 4) Perform operations on a stack including push and pop 5) Perform operations on a queue including enqueue and dequeue

Uploaded by

Maged Sarhan
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)
46 views

Lujain Alg

The document contains the solutions to 7 programming assignments submitted by student Lujain Ahmed with ID 202102508. The assignments include programs to: 1) Perform a binary search to find an element in a sorted list 2) Sort a list of elements using bubble sort 3) Create a linked list of employee data and methods to insert and display employees 4) Perform operations on a stack including push and pop 5) Perform operations on a queue including enqueue and dequeue

Uploaded by

Maged Sarhan
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
You are on page 1/ 6

Student name: Lujain Ahmed

Student ID: 202102508

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;

while (start <= end) {


int mid = start + (end - start) / 2;

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

return -1; // Key not found


}

public static void main(String[] args) {


int[] numbers = {33, 55, 11, 99, 22, 88, 77, 66, 44};
int searchKey = 99;

// Sort the array in ascending order


java.util.Arrays.sort(numbers);

// Perform binary search


int index = search(numbers, searchKey);

// Display the result


if (index != -1) {
System.out.println("The key element " + searchKey + " is found at index " +
index + ".");

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

public static void main(String[] args) {


int[] numbers = {33, 55, 11, 99, 22, 88, 77, 66, 44};
// Perform bubble sort
bubbleSort(numbers);
// Display the sorted list
System.out.print("Sorted list: ");
for (int num : numbers) {
System.out.print(num + " ");

}
System.out.println();
}

public static void bubbleSort(int[] array) {


int n = array.length;
for (int i = 0; i < n - 1; i++) {
boolean swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap elements at j and j+1
array[j] = array[j] + array[j + 1];
array[j + 1] = array[j] - array[j + 1];
array[j] = array[j] - array[j + 1];
swapped = true;
}
}
// If no elements were swapped in the inner loop, the array is already sorted
if (!swapped) {
break;

}
}
}
}

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;
private Employee tail;

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


Employee newEmployee = new Employee(employeeId, employeeName, salary);

if (head == null) {
head = newEmployee;
tail = newEmployee;
} else {
tail.next = newEmployee;
tail = 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 AlgorithmsAssiignment {


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

// Insert employees at the end of the list


employeeList.insertAtEnd(1, "Salma", 5000);
employeeList.insertAtEnd(2, "Marina", 6000);
employeeList.insertAtEnd(3, "Alisa", 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 AlgorithmsAssiignment {
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 AlgorithmsAssiignment {
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