Time Complexity Analysis of Recursive Functions (original) (raw)

Last Updated : 7 Mar, 2026

The analysis of a recursive function involves finding an asymptotic upper bound on the running time.

Consider the following code for example.

C++ `

void fun(int n) {

if (n <= 1)
    return;

fun(n / 2);
fun(n / 2);

for (int i = 0; i < n; i++) {
    cout << "GFG ";
}

}

C

#include <stdio.h>

void fun(int n) { if (n <= 1) return; fun(n / 2); fun(n / 2); for (int i = 0; i < n; i++) { printf("GFG "); } }

Java

public class Main { public static void fun(int n) { if (n <= 1) return; fun(n / 2); fun(n / 2); for (int i = 0; i < n; i++) { System.out.print("GFG "); } } }

Python

def fun(n): if n <= 1: return fun(n // 2) fun(n // 2) for i in range(n): print('GFG', end=' ')

C#

using System;

class Program { static void fun(int n) { if (n <= 1) return; fun(n / 2); fun(n / 2); for (int i = 0; i < n; i++) { Console.Write("GFG "); } } }

JavaScript

function fun(n) { if (n <= 1) return; fun(Math.floor(n / 2)); fun(Math.floor(n / 2)); for (let i = 0; i < n; i++) { process.stdout.write('GFG '); } } fun(8);

`

The time complexity of this function is written as **T(n) = 2T(n/2) + O(n). Now to find the time complexity, we need to solve this recurrence. There are mainly three common methods used to solve recurrence relations that arise in the analysis of recursive algorithms.