- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Substitution Method in Data Structure

Here we will see how to use substitution method to solve recurrence relations. We will take two examples to understand it in better way.

Suppose we are using the binary search technique. In this technique, we check whether the element is present at the end or not. If that is present at middle, then the algorithm terminates, otherwise we take either the left and right subarray from the actual array again and again. So in each step the size of the array decreases by n / 2. Suppose the binary search algorithm takes T(n) amount of time to execute. The base condition takes O(1) amount of time. So the recurrence equation will be like below −

$$T(n)=\begin{cases}T(1) & for\:n \leq 1\2T(\frac{n}{2})+c & for\:n > 1\end{cases}$$

Solve − We will substitute the formula in each step to get the result −

$$T(n)=T(\frac{n}{2})+c$$

By substituting T(N/2) we can write,

$$T(n)=(T(\frac{n}{4})+c)+c$$

$$T(n)=T(\frac{n}{4})+2c$$

$$T(n)=T(\frac{n}{8})+3c$$

$$T(n)=T(\frac{n}{2^{k}})+kc$$

Now if $$(\frac{n}{2^{k}})$$ reaches to 1, it indicates that 2^{k} is n. So the value of k is log_{2}𝑛

The complexity of T(n) = ϴ(log n)

Similarly, if we choose another example like merge sort, then in that case we divide the list into two parts. This division is taking place until the list size is only 1. After that we merge them in sorted order. The merging algorithm takes O(n) amount of time. So if the merge sort algorithm is taking T(n) amount of time, then by dividing it into two halves, and do the same task for each of them, they will take T(n/2) and so on. So the recurrence relation will be like below −

$$T(n)=\begin{cases}T(1) & for\:n = 1\2T(\frac{n}{2})+cn & for\:n > 1\end{cases}$$

Solve − We will substitute the formula in each step to get the result −

$$T(n)=2T(\frac{n}{2})+cn$$

By substituting T(N/2) we can write,

$$T(n)=2(2T(\frac{n}{4})+\frac{cn}{2}) +cn$$

$$T(n)=4T(\frac{n}{4}) +2cn$$

$$T(n)=8T(\frac{n}{8}) +3cn$$

$$T(n)=2^{k}T(\frac{n}{2^{k}}) +kcn$$

Now if $$(\frac{n}{2^{k}})$$ reaches to 1, it indicates that 2^{k} is n. So the value of k is log_{2}𝑛. And T(n) will be −

𝑇(𝑛) = 𝑛𝑇(1) + 𝑐𝑛 log_{2}𝑛

The complexity is θ(n log n)

- Related Questions & Answers
- Brent’s Method in Data Structure
- Rectangle Data in Data Structure
- Halfedge data structure
- Deaps in Data Structure
- Quadtrees in Data Structure
- The Substitution Operator in Perl
- Arrays Data Structure in Javascript
- Stack Data Structure in Javascript
- Queue Data Structure in Javascript
- Set Data Structure in Javascript
- Dictionary Data Structure in Javascript
- Tree Data Structure in Javascript
- Graph Data Structure in Javascript
- Finger Searching in Data Structure
- Skip Lists in Data Structure