[2022|With Resources]Essence of Programming | Data Structures and Algorithms

Error Commit
6 min readJul 21, 2022

--

Usually in the schools and colleges, both Data Structure and Algorithm are being taught as subjects. Most of the students including me tends to ignore the crux of the beautiful concepts of Data Structures and Algorithms, and focus on scoring in exam. But when I begin working on projects, I realized my mistake.

Feature Image

The reason I fall for this trap is the fact that in my college, the major focus of curriculum is to teach the technical know-how of the subject. How to build array? How to write bubble sort? How to create linked list? and so on. But the point which curriculum usually don’t touch or pay much attention to is, Why?

Why to learn Data Structures? How it can impact my proficiency as a programmer? What is the importance of Data Structure in a program? Why the ability to calculate the complexity of an algorithm is important? I fell for another misconception, what I call irony of High level Languages. The irony is that when most of the high level programming languages provide me pre-build libraries and packages for these Data Structures and Algorithms, than why do I even bother to learn to code them or understand them at all.

I this article, I am going to answer you all these questions, If you are a student who just begin learning programming or who already know programming but struggles to write an efficient and industry standard code. This is the most important read your career. Let’s jump straight to the topic.

What is a Program?

A program is a set of instructions to perform a particular task or reach a goal. eg:- I have to make place an order on amazon

Instructions to achieve our goal:-

  1. Open Amazon webpage or mobile app.
  2. Search for what you want to purchase.
  3. Click “Buy Now”.
  4. Add Delivery details and Payment Information
  5. Click “Place Order”.

Now, this is a fairly simple example of a program. Note that there can be other ways to perform same task, like someone may wants to add to cart first than click purchase.

In similar manner when I want, computer to do something for me. I write a set of instruction which computer can execute and give me desired result.

A program have 2 components:

  1. Logic of the program (or set of instructions itself)
  2. Efficient Storage of Data (or in our example it can be cart, because it stores everything in it until we click purchase)

However, there are more components like Database, Networking etc. But they are not part of building a program. We use database to store data which we want to keep even after termination of program eg:- Hard Disk. Networking is used to connect two devices.

Components of a Program

There are two major components of a program, the logic(or Algorithm) and the efficient runtime storage of data(or Data Structure). Let’s discuss them one by one:-

Data Structures

Data Structure is an efficient way of storing collection of data during runtime. The reason I urge you to pay special attention to Data Structures is that, it impacts the efficiency of a program to a great extent. Not using proper data structure will make your program slow and make it consume unnecessary space. It’s like building a beautiful house with little or no space management. It also makes you write unnecessary lines of code like in the example below.

Eg:- if I have to store marks of a student in 5 subjects and calculate sum, the first approach will come to mind is create 5 integers and add them. Like this:-

Sample Program without Data Structures

We can also take input from user, using cin, but that is not the point here.

The other way of doing this is using arrays, and standardize each index for a subject. If you don’t know arrays than, I suggest you to go through this.

Sample Program with Data Structures

Now if I have to add more students in program without data structure, I need to create 6(1 for roll number and 5 for subjects) new variables for every new entry.

But I choose to perform the same task using data structures all need to create an array of arrays or a 2-D array named student. In which each entry in student array is marks of a student. Roll Number can be denoted by the index of student array. This code will looks like this:

Multiple students with Data Structure

I can even shorten this code using loops for entering data into arrays and performing sum. Here, in this blog my purpose is tell the importance of data structures not teaching them.

Not only using data structure but choosing the correct data structures is also very important.

If I choose Linked List instead of array or array instead of Linked List both can be disastrous. So study and understand all fundamental data structure. It will help you a lot in your journey becoming an efficient programmer.

Algorithms

Let’s consider this as a recipe, while data structure is a container in which you store ingredients which are gonna used in the recipe.

If you have ever cooked, you might know this thing that one dish can have multiple recipes. Some can be a complicated restaurant type recipe, while others can be simple home made recipe, but the dish which comes out as result will more or less be the same. In same way you will find that there exists different algorithms for the same end result. Some algorithms will be simple while others can be complex. Some will be optimized for handheld devices while other for supercomputer and every device in between.

Though modern high level programming languages have pass through this base device barrier to some extent. The same code will work in windows device and Mac OS if you code in java, node, python or any other high level language. But again there can be different optimizations methodologies in different languages and same code will run everywhere but speed might be different in different devices. Like in python we have list comprehension while in java similar optimization is achieved by using iterators.

You should write an algorithm which is suitable for the language as well as base device. There are certain methodologies you might have heard of, like brute force, divide and conquer, backtracking etc.

Choosing the right approach or optimal approach depends on multiple factors including base device, type of input, task to be performed, what is the expected output, type of application etc.

Example searching in a sorted array, we have two ways brute-force methodology and divide-and-conquer technique, popularly known as linear search and binary search respectively.

In worst case scenario, linear or brute force approach have time complexity of n, where n is the number of elements in the sorted array. While with binary or divide and conquer approach same worse case complexity is log n. For your simple understanding for a sorted array of size 10000 elements, the maximum search in case of linear search is 10000 and in case of binary it is only 14.

For better understanding of algorithm, I recommend you to go through this CS50 lecture on YouTube linked down.

I also recommend you to go through the whole CS50 playlist on YouTube linked down.

Closing Thoughts

If you ever desire to become a programmer, than understanding of data structure and algorithm is your only holy grail. I recommend you to read my another article on 5 Hacks to Boost your Programming Skills. I wish you luck on the journey.

Reference Links and Books

  1. 5 Hacks to Boost your Programming Skills:- My another article.
  2. Algorithms + Data Structures = Programs by Niklaus Wirth, A book on Data Structures
  3. Leet Code :- One of my Favorite platform to practice Competitive Programming.
  4. CS50 Playlist
  5. Data Structures in C++ :- A Comprehensive tutorial in data structures on Udemy. I personally used this course (paid resource, non-affiliate)
  6. Geeks for Geeks :- You can use this blog for reference while learning and practicing

--

--

Error Commit

Graduate in Computer Science, with two years of working experience in Algorithms and Data Structures. I mainly code in Java and Python. Looking to excel in DSML