What is running time?
The application has a feature called running time. It is one of the parameters for measuring its performance. It is written in a particular language and is designed to perform a specific task. Applications run in a specific environment. The execution of the business will depend on the application’s running time. It must be optimized in order to get the maximum benefits out of the application.
Calculation of running time
The time complexity of an application is used for calculating or denoting the maximum time that is taken by the algorithm used in the application to produce the desired output. The time complexity of algorithms is generally denoted by the big O notation. It is one of the asymptotic notations used to represent the time complexity. The straightforward method for determining time complexity is to count the number of times each of the algorithm’s operations is executed. But this approach is difficult and unnecessary. The other possibility is to identify the most important operation of the algorithm called basic operation. It is the operation that takes the maximum share of the total running time. The time complexity can be easily determined by actually calculating the number of times the basic operation has been executed.
So, the steps are as follows:
1. Identify the basic operation, which is the one that executes the maximum times.
2. Calculate the greatest number of times the basic operation gets executed.
Recognizing the most time-consuming basic operation is easy. It is actually the most time-consuming operation in the inner-most loop,
Take an example of an insertion sort algorithm.
Consider applying insertion sort on an array a[8] = {4,3,2,10,12,1,5,6}. The pseudocode of the algorithm is given below:
INSERTION-SORT(A)
- for c = 1 to n do
- key ← A [c]
- q ← c – 1
- while q > = 0 and A[q] > key do
- A[q+1] ← A[q]
- q ← q – 1
- End while
- A[q+1] ← key
- End for
As you can see, the most time-consuming operation in this algorithm is in the innermost loop. It is inside the while loop, which is the key comparison A[q] > key. The maximum number of comparisons made depends on the nature of the input. In worst case, say for an array a[8] = {55,40,37,32,31,27,23,21}, the key comparison A[q] > key is executed the most number of times that is for every q = c-1,....,0 . Therefore, for the worst-case input of an array having strictly decreasing values, the number of key comparisons equals Cworst(n) = O(n^2). The best case input corresponds to an already sorted array, say for an array a[4] = {1,2,3,4}, the comparison A[q] > key is executed only once for every iteration of the outer loop. Therefore, for the best case input of strictly increasing values, the number of key comparisons equals Cbest(n) = O(n).
Asymptotic analysis
The asymptotic notations that are used to represent the time complexity or simply the running time of an algorithm are as follows.
- Big- O notation: This notation is used to describe the maximum running time of an algorithm. It denotes the upper bound, that is, it denotes the maximum time that an algorithm will take.
- Omega Notation: It is used to represent the minimum running time of an algorithm. It denotes the lower bound, that is, it is used to describe the minimum time an algorithm will take.
- Theta Notation: It is used to represent the range in which an algorithm's time complexity falls. It is used to describe both the upper and lower bound for running time of an algorithm.
Types of Time Complexities
The time complexity of the algorithms used in an application falls into any of the following categories:
- O(1) - Constant time: When an algorithm takes the same amount of time to give an output even when the input sizes vary, then it is said to have a constant time complexity.
- O(n) - Linear time: When an algorithm takes time which varies as the input size varies. If the time complexity increases linearly with the input size, then it falls under this category.
- O(n^2) - Quadratic time: When an algorithm faces a quadratic increase in the total running time as the input size increases, then it is said to have quadratic time complexity.
- O(n^c) - Polynomial time: When an algorithm’s running time is bounded by a polynomial, then it is said to have polynomial time complexity.
- O(log n) - Logarithmic time: When the running time of an algorithm grows logarithmically as the input size grows, then it is said to have logarithmic time complexity.
- O(2^n) - Exponential time: When for an algorithm the running time doubles with increase in the size of input, then it is said to have exponential time complexity.
Running Time Analysis of Algorithms
- Worst case analysis: It is used to calculate the time required by the algorithm in the worst case. The worst case of an algorithm is determined by giving the input that it will take maximum time for the algorithm to solve the problem and produce the result. For example, in case of a searching algorithm, which searches the element in an array linearly, the worst case is when the element is not present in the array. This results in the algorithm to search all the elements in the array and thus, taking the maximum time.
- Best case analysis: It is used to calculate the time required by an algorithm in the best case. The best case of an algorithm is when the input given to the algorithm is in such a way that it takes the minimum amount of time for the algorithm to produce results. For example, in the case of a searching algorithm, which searches an element linearly, the best case is when the element to be searched is present at the first position in the array.
- Average case analysis: It is used to describe the time taken by the algorithm on an average. This can be done by taking a wide variety of inputs and calculating the running time of the algorithm for each of the inputs. Then, an average of the values obtained is computed.
Context and Applications
This topic is important for postgraduate and undergraduate courses, particularly for, bachelor's in computer science engineering, and an associate of science in computer science.
Practice Problems
Question 1: Running time is determined by counting the number of times the _______ operation is executed.
- basic
- addition
- subtraction
- division
Answer: Option a is correct.
Explanation: Running time is determined by counting the number of times the basic operation is executed.
Question 2: If the time complexity increases linearly with the input size, then its time complexity belongs to the class ________
- O(n^2)
- O(log n)
- O(1)
- O(n)
Answer: Option d is correct.
Explanation: Linear complexity is given by O(n).
Question 3: When the time complexity doubles with respect to the size of input then the algorithm's time complexity belongs to _____________ class.
- Polynomial
- Quadratic
- Exponential
- Cubic
Answer: Option c is correct.
Explanation: If the growth gets doubled with the input size, then it is referred to as exponential.
Question 4: What are three asymptotic notations?
- alpha, gamma and delta
- big-oh, omega, and theta
- omega, theta, and delta
- alpha, omega, and delta
Answer: Option b is correct.
Explanation: Big-oh, omega, and theta are the notations used to represent the time complexity.
Question 5: Which notation is used to represent the lower bound on the time complexity of an algorithm?
- big-oh
- omega
- theta
- None of the above
Answer: Option b is correct.
Explanation: Omega is used to represent the lower bound on the time complexity of the algorithm.
Want more help with your computer science homework?
*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.