An introduction to data structures and algorithms
Written by Kyle Josias, Developer at Saratoga.
So, what are data structures? And how do algorithms apply? And who’s idea was it to make it so complex and where can I find them? These are some of the questions I hope to answer with this article.
While learning about data structures and algorithms can be seen as the equivalent of ‘eating your vegetables’ for software developers, it’s certainly helpful to have an understanding of how the different algorithms work and important for developers to understand the theory behind the most popular algorithms.
What are data structures?
Simply put, a data structure is a predefined way of efficiently storing, accessing, manipulating and processing data.
In most Object Oriented Programming (OOP) languages, such as Java, there are predefined or built-in data structures with tons of libraries to help you manage and use each of these data structures. Some of the most commonly known and most widely used data structures include arrays, linked lists, stacks and queues. While more advanced forms include binary trees, heaps, graphs, hash tables and the list goes on.
Now that we have an idea of what data structures are, it’s important to note that simply knowing what they are isn’t enough. The real magic happens when you know which data structure to use in order to solve a particular problem. This usually comes with experience, so don’t fret if you find yourself feeling unsure — most seasoned developers still check with ‘uncle Google’ from time to time.
What are algorithms?
If you think about it from a computer science perspective, all programs are a set of instructions a computer executes in order to solve a particular problem. In computer programming, an algorithm is the step-by-step procedure used to achieve this.
One could go about solving the same problem in many different ways when writing code, which is why we use algorithms to try and get the desired result in the most efficient way possible. This is also why it is best practice to test your application in the ‘worst case scenario’ and to check how well it performs under pressure. This is usually measured by your programs execution speed relative to scale input.
For example, if you have an array of a given size and you have to loop through the entire array, this is what would most commonly be known as a linear algorithm. The reason for this is because the greater the size of the array, the more time it will take to execute and the time it takes to execute increases in a linear fashion. The graph below explains this concept visually:
Another example, using the same graph, would be if we had that same array (no matter how big or small) and returned the element we wanted using just the index position. This would significantly reduce the running time. In fact, it would give us a constant time. Because we will always get the element at the exact index position without having to loop through the entire array. In a constant time, our running time arrow on the graph would lay flat, regardless of how large our array is. In a perfect world, we would be able to apply constant time algorithms to everything, but sadly this is not possible as different problems require different algorithms.
Thanks to computer science specialists, we do however have some standards for efficiently measuring performance and ways to help us solve many different types of development problems. In computer science we use what’s called Asymptotic Notation. Don’t let the name scare you, it’s just a global, standardised way for computer scientists and developers to understand and represent some of the software development jargon in a way that transcends language and cultural barriers.
Our linear example from earlier of Asymptotic Notation will look like this: O(n). This is an algorithmic expression to determine the growth rate of our algorithms. A few more growth rates include:
Linear: O(n)
Quadratic: O(n2) — (If we had nested loops — a loop within a loop)
Constant: O(1)
As mentioned, there are many existing algorithms available to help developers sort, search and retrieve data. A fun and visual way of demonstrating running times of algorithms is the platform VisualGo, which provides users with access to many different algorithms you can play around with.
While we’ve only scratched the surface of what data structures and algorithms are, and how they can holistically improve the skills and deliveries of software developers, my goal with this article was to inspire curious minds to go further and develop your own deeper understanding and to practise what you learn in small increments. There are also plenty of websites and resources out there to help you practice coding more complex data structures and algorithms. Remember, Rome wasn’t built in a day and the only way to become better at anything in life is to practise a little bit every day. Practise, practise, practise!
Keep an eye out for Kyle’s second article, where he’ll share code examples of binary search tree data structures.
By combining our decades of software engineering, business analysis, project management and software testing expertise with our pool of phenomenally talented professionals we work with organisations to see great technology ideas successfully delivered.
Working with our global client base, we convert business ideas into quality technology solutions which create meaningful business benefits.