2021
2021
3. Assume that you have designed an algorithm to do a specific task in your final
project. Briefly explain how you can evaluate the efficiency of your algorithm.
• Time Complexity: Analyze how the running time increases with the size of the
input. This is often done using Big O notation.
• Space Complexity: Determine how much memory the algorithm uses in relation
to the input size.
• Benchmark Testing: Run the algorithm with various inputs to measure actual
running time and resource usage.
• Comparative Analysis: Compare with other algorithms solving the same problem
in terms of speed, memory usage, and scalability.
4. Write a Java function that calculate and returns the sum of all the even numbers of
a given array.
5. Write a Java program that finds the maximum and minimum elements in the
following array.
78 65 12 48 23 96 77 18 29 54
ADT is a specification of a mathematical set of data and the set of operations that
can be performed on the data. Ex: Stack, Queue, LinkedList, Tree
7. State whether the following data structures is leaner or nonlinear.
a) Array - Linear
b) Stack - Linear
c) Queue Linear
d) Graph-Non-Linear
8. Compare and contrast arrays and linked lists data structures, highlighting their
advantages and disadvantages
Arrays:
Advantages:
• Direct Access: Allows direct access to elements using indices.
• Memory Efficient: More memory efficient as it does not require extra storage for
references.
• Better Cache Performance: Due to contiguous memory allocation, they have
better cache locality.
Disadvantages:
• Fixed Size: The size of the array is fixed and needs to be known beforehand.
• Inefficient Insertions/Deletions: Inserting or deleting elements is costly because it
may require shifting elements.
Linked Lists:
Advantages:
• Dynamic Size: Can grow or shrink in size dynamically.
• Efficient Insertions/Deletions: More efficient insertions and deletions, as they
involve only updating references.
Disadvantages:
• No Direct Access: Cannot directly access elements; need to traverse from the
beginning.
• More Memory: Requires extra memory for storing pointers to next (and
previous for doubly linked lists) elements.
• Poor Cache Performance: Due to non-contiguous memory allocation, they have
poorer cache performance.
9. Declare a Java structure to store an integer and a String.
10. Implement a Java function to insert a node at the beginning of a linked list.
public class Node
{
int data;
Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
public class LinkedList
{
Node head;
public void insertAtBeginning(int data)
{
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}}
11. Differentiate Stack and Queue data structures.
Stack:
• LIFO Principle: Last-In-First-Out. The last element added is the first one to be
removed.
• Operations: Main operations are push (to add an item) and pop (to remove an
item). Additional operations include peek (to view the top element without
removing it) and isEmpty (to check if the stack is empty).
• Usage: Used in scenarios like function call stacks, undo mechanisms in software,
and for algorithms involving backtracking.
Queue:
• FIFO Principle: First-In-First-Out. The first element added is the first one to be
removed.
• Operations: Main operations are enqueue (to add an item to the rear) and
dequeue (to remove an item from the front). Additional operations include front
(to view the first element) and isEmpty/isFull (to check the status of the queue).
• Usage: Used in scenarios like managing requests on a single shared resource (like
a printer), handling call center operations, and in breadth-first search
algorithms.
Static Implementation: This is where the size of the stack is fixed at the time of
creation. If the stack is full and you try to push an element, it will result in a stack
overflow.
| | <- Top
|5 |
|3 |
|2 |
|1 |
‾‾‾
Here, the stack is implemented using a fixed-size array. The Top pointer points to
the current top of the stack.
Dynamic Implementation: This is where the size of the stack can grow and shrink as
needed. If the stack is full and you try to push an element, the stack will
automatically resize to make room for the new element.
In this implementation, the stack grows and shrinks dynamically with each push and
pop operation, using a linked list.
• A tree is a data structure that emulates a hierarchical tree structure with a set of
linked nodes.
First Pass:
Repeat for the rest, largest (62) to the end: [40, 2, 18, 62, 94]
Third Pass:
Repeat, (40) moves to its correct position: [2, 18, 40, 62, 94]
Fourth Pass:
16. Write down the pseudo codes to implement a linear search algorithm.