CCDSALG Reviewer
CCDSALG Reviewer
Contents:
I. Analysis of Algorithms
II. Sorting
- Midterm Exam -
V. Searching
VI. Graphs
- Final Exam -
Algorithm Analysis
Algorithm (Definition)
- Defined step-by-step process that operates on certain input values and generates specific output values.
- Serves as a computational procedure with clear instructions on how to transform the given inputs
into the desired outputs.
- It is like a recipe that guides a computer or a person in performing a task by providing a series of well-
defined actions to follow.
- There can be more than one algorithm to solve a given problem.
- An algorithm can be implemented using different programming languages on different platforms.
Example:
Both a priori and a posteriori analysis play important roles in understanding and evaluating algorithm
performance.
Priori analysis helps in predicting and comparing algorithms' efficiency based on theoretical bounds.
Posteriori analysis provides concrete measurements through practical experiments.
In order to determine the exact execution time of a command, several factors need to be known:
LINEAR RELATIONSHIP: execution time of an algorithm increases at the same rate as the input size
increases.
BETTER TIME COMPLEXITY: if an algorithm does not have a linear relationship, it means that the execution
time does not increase at the same rate as the input size.
1. Sublinear: the execution time grows at a slower rate than the input size. For example, if the input size
doubles, the execution time might increase by a smaller factor, like logarithmically.
2. Logarithmic: the execution time increases logarithmically as the input size increases. Logarithmic
growth is slower than linear growth but faster than sublinear growth.
For n = 100, Algorithm 1 takes 10 seconds, while Algorithm 2 takes 15 seconds. Again, Algorithm 1 is
faster.
Answer: Algorithm 1
- Algorithm 1 has a linear relationship between input size and execution time, suggesting O(n) time
complexity.
- Algorithm 2 does not have a linear relationship, indicating potentially better time complexity, such as
sublinear or logarithmic.
- Based on the given execution times, Algorithm 2 appears more efficient, with lower times for the same
input sizes.
Arrays
- Simple way to store similar data in a list
- The data is placed one after another in the array
- You can find and access each item in the array by using a number called an index or subscript
Problem: Identify the index of the array containing one specific number.
Solution:
1. Start from the beginning of the array and look at each element, one by one.
2. At each element, check if it matches the value you are searching for.
a. If it matches = you have found the value and can stop the search
b. If it does not match = continue to the next element
3. Keep repeating steps 1 and 2 until you reach the end of the array.
4. If you reach the end of the array without finding a match, then the value is not present in the array.
5. In the worst-case scenario, the value you are searching for is located at the very end of the array, so you
must go through every element in the array.
The searching algorithm is known as linear search because it checks each element in a linear manner, one
after another, until the desired value is found, or the end of the array is reached.
LINEAR SEARCH