Why 100 lines of code is better than just 2?


Introduction to Data Structure and algorithms
DSA is a combination of Data Structure and Algorithms that is how data is efficiently stored and organized in structure format (Data Structure) like array , linked list , queue or stack and step-by-step methods that are used to perform operations on data like sorting , searching or pathfinding.
For example → We all get those 2 AM craving and my go-to-dish is Maggi. Let’s understand DSA concept through Maggi making process.
Water , Maggi etc are raw data like inputs , values etc (Raw ingredients = Data) and Pan holds everything like array or stack and spoon is also a type of data structure (Utensils + process = Data structure ) and the Steps or Procedure to cook is Algorithms . Finally, Maggi is ready that is the final output after processing data is ready !
Easy to use → Having all Maggi ingredients in one place and a fixed recipe .
Easy to access → Everything is right there in the pan. You don’t need to search the kitchen mid-way. So, there is easy access .
Easy to modify → You can add different vegetables , species and cheese that is you can update or modify the data .
Introduction to Time Complexity
How to Know which Software is best ? Can the following parameters define which software is best -:
Cost → We cannot judge a software by just looking at the price or cost as it depends on many factors . For example - if there are 4 people who are selling software at 100 Rs , 1000 Rs , 10,000 Rs and free. Can we tell which software is best ?
NO, because it depends on many factors like functionality , usability, use cases , Scalability , reliability , compatibility etc.
Line of code → Writing 100 lines of code can be better as compared to writing 2 lines . For example → There are 9 balls where 8 balls weight is same and 1 ball weight is heavy. Determine the ball .
Let’s consider 2 software , software 1 which consists of 100 lines of code and software 2 consists of 2 lines code .
Software 1 (100 lines of code ) -:
#include <iostream>
using namespace std;
int main() {
int ball[] = {1, 1, 1, 2, 1, 1, 1, 1, 1}; // Example: ball[3] is heavier
int A = ball[0] + ball[1] + ball[2];
int B = ball[3] + ball[4] + ball[5];
int C = ball[6] + ball[7] + ball[8];
if (A == B) {
// Heavy ball is in group C
if (ball[6] > ball[7])
cout << "Heavier ball is ball[6]" << endl;
else if (ball[7] > ball[6])
cout << "Heavier ball is ball[7]" << endl;
else
cout << "Heavier ball is ball[8]" << endl;
} else if (A > B) {
// Heavy ball is in group A
if (ball[0] > ball[1])
cout << "Heavier ball is ball[0]" << endl;
else if (ball[1] > ball[0])
cout << "Heavier ball is ball[1]" << endl;
else
cout << "Heavier ball is ball[2]" << endl;
} else {
// Heavy ball is in group B
if (ball[3] > ball[4])
cout << "Heavier ball is ball[3]" << endl;
else if (ball[4] > ball[3])
cout << "Heavier ball is ball[4]" << endl;
else
cout << "Heavier ball is ball[5]" << endl;
}
return 0;
}
Software 2 (2 line of code ) -:
#include <iostream>
using namespace std;
int main()
{
int ball[] = {1,1,1,2,1,1,1,1,1};
int wt = 1;
for (int i = 1; i<= 9 ; i++) {
if(ball[i] > wt)
wt = ball[i];
}
cout<<wt;
}
In Software 2 , the lines of code is less but the time complexity is O(n) and of software 1 time complexity is O(1). Hence , for this example software 1 is better as compared to software 2. So , we can say that the line of code is not the only factor that determines whether the software is good or not .
- Software execution time → Software execution time depends on size of input , type of input , edge cases in input etc . For example - if software A takes 1 sec and software B takes 5 sec which one is better? We cannot determine just by software execution time as it is possible there can be 1000 things going in background of the machine in software B and no background programs in software A . Hence , software A takes less time. So , there are many factors which determines which software is best.
So, How to know which software is good or which approach is best?
We can determine the best or better software by whose speed is good or that takes less time that is an optimized approach. For this we use the concept called Time Complexity and Space Complexity.
Time Complexity and Space Complexity :
Time Complexity → It can be defined as the amount of time taken by an algorithm to run, as a function of the size of the input ( performance (time or space) of an algorithm changes depending on how big the is the input ) . It shows how the runtime of an algorithm grows with the input size.
Space Complexity → It can be defined as the amount of memory space required by an algorithm to run, as a function of the size of the input. It shows how the memory usage of an algorithm grows with the input size.
Subscribe to my newsletter
Read articles from Khushi Shrivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
