Introduction to Data Structures and Algorithms
Most computer programs follow the same general process. They take in an input, process it, then generate an output. To make this process as efficient as possible, you need to consider two important details:
- How do you store and retrieve your input and output data.
- How do you process the input so that it matches the desired output.
To answer these two questions, you need to understand the different techniques for representing and processing data. These techniques, as well as the methods to analyze and evaluate them, is the main focus of data structures and algorithms.
What is a Data Structure?
A data structure is a way to store and organize data. The goal of a data structure is to allow you to access and modify data as efficiently as possible. Often, when you develop programs, your first task will be to determine the data structure that fits a problem best.
What is an Algorithm?
We usually look at an algorithm as a set of well-defined steps that solve a problem. In the case of computer science, solving a problem usually means turning an input into a desired output.
There are two important details related to algorithms, the first being the idea of well-defined steps. The idea of well-defined means that the steps should not be ambiguous in any way. For example, suppose I provided you with a list of numbers and a value to search for in the list. If I told you to search the list for the provided value, you could take various approaches to achieve this. The instruction of searching the list is ambiguous, it’s not clear how you should approach doing this.
Now, consider instead if I told you to do the following:
- Start at the beginning of the list
- Compare the current value to the one you are searching for
- If you find the value, return the location of the value
- If you reach the end of the list without finding the value, return -1
- If you don’t find the value and more values exist in the list, proceed to the next item in the list and repeat step 2
In this case, the instructions for searching the list are clear. You can follow each step and always complete the step the same way. This would consist of well-defined instructions for searching the list.When we evaluate algorithms, we will often measure the speed of the algorithm, specifically, how long does it take an algorithm to produce a result. It turns out that different algorithms can have substantially different runtimes depending on the approach they take. In addition to this, it’s possible to create an algorithm that takes an extremely long or infinite amount of time to finish.
In modern computing, it may seem that you can solve almost any problem quickly. Although this is often true, it’s also the case that our dataset sizes continue to grow significantly in size. As dataset sizes increase, the requirement to write algorithms that can process the data efficiently becomes more essential.
To learn more about DSA in Python, check out our free Python DSA course.
This article is part of a series. The next article in this series is about correctness and efficiency of algorithms.