Algorithm 11 Questions & Answers For Exam
Algorithm 11 Questions & Answers For Exam
#1. .Look up the words algorithm and algorithm in your dictionary and write down their
meanings?
Algorithm: A step-by-step set of instructions to solve a problem or perform a task.
#4. Performance of a Program with two examples: The performance of a program is the
amount of the computer memory and time that a program needs to run means how much
time and memory it needs to run.
They are measured as:
Examples
1. Time Complexity: How the runtime of the program changes as the input size grows
Linear Search: time complexity is O(n) The time it takes to search grows directly with the
number of items in the list. If there are 10 items, it checks up to 10 times; if 100 items, up to 100
checks.
Binary Search: time complexity is O(logn) The time grows much slower because it repeatedly
halves the number of items to search. If there are 16 items, it takes about 4 checks; for 32 items,
about 5 checks.
Merge Sort: space complexity is O(n) it uses additional memory when create temporary arrays
during the sorting process
1. Based on Functionality:
Sorting Algorithms: Used to arrange data in a specific order (e.g., smallest to largest).
Examples: Bubble Sort, Merge Sort.
Searching Algorithms: Help find specific data in a collection.
Examples: Linear Search, Binary Search.
Graph Algorithms: Solve problems related to graphs (nodes and edges).
Examples: Dijkstra's Algorithm, BFS, DFS.
Divide and Conquer: Break the problem into smaller parts, solve them, and combine the results.
Example: Merge Sort.
Greedy: Make the best decision at each step, hoping it leads to the best overall solution.
Example: Kruskal’s Algorithm.
Dynamic Programming: Solve smaller sub problems, save their results, and reuse them.
Example: Fibonacci Series.
Backtracking: Try all possibilities, undoing choices when necessary.
Example: Solving a maze.
Brute Force: Check every possible solution to find the correct one.
Example: Password cracking by trying all combinations.
3. Based on Input Data Structure:
4. Based on Output:
5. Based on Execution:
6. Based on Complexity:
Function FindMax(list)
max = list[0]
For each item in list
If item > max then
max = item
Return max
End Function
```
Function Factorial(n)
If n == 0 then
Return 1
Else
Return n * Factorial(n - 1)
End If
End Function
Comments
// Initialize x to 5
int x = 5;
Blocks
Use {} to group statements, often for loops, functions, or conditionals.
Example:
if (x > 0) {
print("Positive");
}
Identifiers
Names for variables, functions, etc. (start with a letter or _, followed by letters, digits, or _).
Example of simple data type:
node = record {
datatype data1;
datatype data2;
link next; // Points to the next node
}
1. Factorial Calculation
Problem: Find the factorial of a number n (n! = n × (n-1) × (n-2) × ... × 1).
Recursive Explanation: If you want to calculate n!, you multiply n by the factorial of n-1, and so on, until
you reach 0!, which equals 1.
Function Factorial(n)
If n == 0 then
Return 1 // Base case
Else
Return n * Factorial(n - 1) // Recursive case
End Function
2. Fibonacci Sequence
Problem: Find the nth number in the Fibonacci sequence, where each number is the sum of the two
preceding ones: 0, 1, 1, 2, 3, 5, 8, ...
Recursive Explanation: To find the nth Fibonacci number, add the (n-1)th and (n-2)th Fibonacci numbers.
Function Fibonacci(n)
If n == 0 then
Return 0 // Base case
Else if n == 1 then
Return 1 // Base case
Else
Return Fibonacci(n - 1) + Fibonacci(n - 2) // Recursive case
End Function
3. Sum of an Array
Function SumArray(arr)
If arr is empty then
Return 0 // Base case
Else
Return arr[0] + SumArray(arr[1...n-1]) // Recursive case
End Function
Problem: Calculate the factorial of n using tail recursion, which is a more efficient version of recursion.
Recursive Explanation: Keep track of the result as you go along.
#11. Write down short notes about space complexity and time complexity
Space Complexity
Definition: Space complexity refers to the amount of memory an algorithm needs to run,
depending on the input size. It tells us how the memory usage grows as the problem size
increases
Time Complexity
Definition: Time complexity describes how the running time of an algorithm increases with the
size of the input.
2. **Binary Search**:
- Time Complexity: O(log n)
- Space Complexity: O(1) (in-place search)
3. **Bubble Sort**:
- Time Complexity: O(n^2)
- Space Complexity: O(1) (in-place sorting)
4. **Merge Sort**:
- Time Complexity: O(n log n)
- Space Complexity: O(n) (requires additional space for merging)