Let’s Talk About Data Structures & Algorithms (1) — Introduction

黃翌軒
Ian 的摳頂小學堂
6 min readMay 17, 2021

This series of articles will focus on some of the commonly used data structures in computer science and their related algorithms or use cases. Contents of this series come from many sources, including “Data Structures & Algorithms”, which is a course I took in National Taiwan University, CSIE department. If you have any questions regarding the contents or sources, you’re welcome to comment below or send me an email!

這篇文章為英文版,如果要閱讀中文版文章的話,請點以下連結。

👉 淺談資料結構與演算法 (1) — Introduction

What is Data Structures

Before we talk about data structures, we first need to ask a question “What is data structure?” This is an era of information technology. Many awesome applications have been invented over the last decade. Think about your daily life for a second. You may check posts or stories on Facebook or Instagram when you go to work / school in the morning. You may get your lunch through food delivery apps such as UberEats, FoodPanda, or GrabFood. It’s also very likely you need to check your Gmail to see if someone tries to contact you.

In order for these applications to work, they need to store and organize a huge amount of data. Have you ever wondered how the people designing these services manage so many data?

This is where data structures come into play. As the name implies, data structures are the knowledge of organizing data into various forms. Why do we need to do that? That’s because we often have different requirements in different scenarios. For example

  • Find one item among many others like finding a book given its name.
  • Data has a hierarchical relation, such as files and folders in your computer
  • Make sure the people who use data access them in a specific order. For example, many data are in a line waiting to be processed and we need to process them in first-come-first-served order.

What is Algorithm?

Artificial intelligence has been so popular these years that I assume we are all familiar with the word “algorithms”. But what exactly is an algorithm? First, let’s take a look at wikipedia’s explanation.

In mathematics and computer science, an algorithm is a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of problems or to perform a computation.

Simply put, an algorithm is a sequence of instructions. As long as a computer follows these instructions and executes related commands, there will be some outcomes.

Still confused? Then let’s take a look at another thing that’s very similar to algorithm. That is … recipe!

In the photo of this recipe, we could see the name of this dish, the ingredients it needs, and the steps that we need to take to cook it. Actually, algorithms are not so different from recipes. To solve a problem (like making a dish), we need to give computers the parameters (ingredients) they needs and tell them how to compute these figures (steps of cooking) to derive the answer (the final dish).

Algorithms are all about decoupling problems into many smaller steps of processing. After computers follow these steps, they can give us the answer of that question. Sounds easy right? However, it may not be as easy as you think. There are some points that we may find quite challenging.

  1. The instructions in algorithms need to be computer-understandable. We humans tend to use heuristics to solve problems. Unfortunately, computers aren’t that smart. Things that computer can do are limited by its instruction set in its CPU. So, we need to divide problems into small pieces and make sure all these pieces are correct and work perfectly with each other, which is not an easy task for humans.
    Think about walking a maze by looking a map. You’ll probably just go wherever you like. Of course you probably would somehow check if a route ends up to be a dead end before taking it. But basically you just walks it. If you really bump into a dead end, you might just go back to the previous fork and choose another route.
    However, computers cannot take the route it likes! If you don’t specify a route, it would just stand there. So you need to tell them how to choose when there are multiple routes and choose wisely (i.e. not to choose a route that’s actually a dead end). That is not a simple task.
  2. The parameters of an algorithm have to be well-defined and this is something to do with data structures. If we’re talking about recipes, it’s not so much a big deal since the dish is unlikely to be totally ruined just because of a slight change of the ingredients (e.g. you put 2 tomatoes into a soup instead of 3). But for computers, there are no rooms for vagueness. If parameters are not defined strictly, there’s a great chance that the algorithm will not work as it should be.

Flowchart

Flowchart is a graphical way that can let us grasp the whole picture of an algorithm quickly and easily. By showing the graph, we could understand the architecture and flows of an algorithm in a short period of time. Below is an image of a flowchart showing the algorithm of checking if a student passes or fails.

source: https://www.edrawsoft.com/explain-algorithm-flowchart.html

You could see that there are many shapes of components. This picture shows the meaning of them.

source: https://www.edrawsoft.com/explain-algorithm-flowchart.html

Pseudocode

We’ve been constantly referring the word “instructions”. Instructions are written by codes. The codes we write are usually high-level computer languages. Computers cannot understand them. The only codes that computers can understand are machine codes (composed of 0 and 1). After humans finish writing the high-level codes, these codes will be transformed into machine codes by layers of compilers and interpreters.

Even if we use high-level languages, there are a dozen of commonly used languages, such as C, C++, C#, Java, Python, JavaScript, Swift … . Different languages have different implementations and features. If algorithms are written in a specific high-level language, there would be a few problems. First, only the people who know that language can understand without the help of others. Second, it is costly to transform into other languages.

Therefore, we also tend to use pseudocode to express an algorithm. Pseudocode is written in a way similar to other high-level languages but only define some commonly used operations in the layer of logic, such as arithmetic, comparison, logic operations. In fact, they are more like norms instead of definitions. But after many years of developing, some norms are followed by most users in most cases.

Below is an example of pseudocode.

source: https://shantoroy.com/latex/how-to-write-algorithm-in-latex/

Alright, that’s all for today! We briefly talked about what data structures and algorithms are. Next article will talk about the first data structure we’ll learn, Arrays, and its related algorithms and use cases. If you like my articles, feel free to give me some claps. I’ll really appreciate it! Thank you.

--

--