How good your code

How good your code

Big O explained in simple language.

·

3 min read

2 factors responsible for knowing how good your code is.

Readability

How clean your code is ?
How easily is someone able to read your code ?
Well there is not much to explain in this but code readability is what you learn as you code more and more.

I'll link to blog if I write someday on increasing code readability

Scalability

How scalable your code depends on 2 aspects.

  1. Time (speed).
  2. Space (memory).

Let me take an example before going jumping on concept.
Assume that you want to go from location A to location B. You have 2 cars first being Ford Mustang and second is India's national car Wagon R(inside jokes OP). Which one will you choose ?
Ford Mustang will have more speed but will consume more petrol. On other hand Wagon R will consume less petrol but it will have less speed.

Sometimes there's a tradeoff between saving time and saving space, so you have to decide which one you're optimizing for.

It depends on condition weather you want your algorithm your algorithm to run in less time or less space.

Time complexity.

First let's talk about time complexity(speed).
In simple words how efficient your code is as your input grows.
You might have guessed the topic, yeah you are right. Its It's like math except it's an awesome, not-boring kind of math.
Ladies and gentlemen please welcome one and only.

bigo.webp

With big O notation we express the runtime in terms of—brace yourself—how quickly it grows relative to the input, as the input gets arbitrarily large.

Let's break it down.
It might happen that multiple processes are running on system, or we run the same algorithm on an Intel i7 powered laptop and other guys runs it on Intel i3.
So it's hard to pin point exact runtime of an algorithm, but we can measure how quickly it grows with respect to input given to it.
I hope you got it.

And another point is that you are supposed to measure it for large inputs. Its not like you measure it for 5 elements as input then 10 and lastly 20. You measure as input gets arbitrary large. Like 100 as input then maybe 1000 or 100000.

Like take for example. it takes X amount to run for 100 input and X^2 for 100000 inputs. Now this X can be different for a i5 or i7 powered processor but it will grow at same rate for both laptops respectively.

How do I measure Big O of any algorithm ?

Space complexity

In simple words it is amount of extra space taken by algorithm excluding the input.
Suppose a bus has capacity of carrying 20 people at once and if we stuff 40 people in that bus then the space complexity is O(n).
😂 😂 😂 I know this is very bad example but hold on.

public static void sayHiNTimes(int n) {
    for (int i = 0; i < n; i++) {
        System.out.println("hi");
    }
}

space complexity for this program is O(1), because it is not taking any extra space.
Space here can be variable, data structures, function call.

Another example.

public static String[] arrayOfHiNTimes(int n) {
    String[] hiArray = new String[n];
    for (int i = 0; i < n; i++) {
        hiArray[i] = "hi";
    }
    return hiArray;
}

In this example we are making an additional array and storing 'hi'. We are storing 'hi' for n times.
Space complexity is O(n).

* Important - please note that we do not include space taken by input variables.