Writing Fast Code: Introduction To Algorithms and Big-O
Ever wondered what makes code fast? Here we take a look at different algorithms, their running time, and Big-O.
Sorting - Case Study
Let's say I'm making an RTS, and I needed to sort out units by their health. As you can see here, the positions of our units are a mess! A human being can sort this out very easily - one can instantly 'see' that it should be arranged like so: The brain makes some pretty interesting calculations, and in a snap, it sees how the problem can be solved, as if in one step. In reality though, it actually does a series of steps, an algorithm, to sort out these pigs. Let's try to solve this problem programmatically. One very straight forward way to solve this problem is to walk through our input, and if they are not sorted, we swap them: Are the first two pigs sorted? Yes, so we leave them be. Are the next two pigs sorted? No, so we swap them. We will continually do this recursively, until we walk through our input and see that we have in fact sorted it out. To save you some bandwidth, here is our sorting algorithm in an animation, courtesy of Wikipedia: (The animation is pretty long, so you might want to refresh the page to start over) The algorithm we have described here is a Bubble Sort. Let's define its running time shall we? How many steps do we take to fully sort it out? First we walk through our input. If our input is n, that is a total of n steps. But how many times do we start over and walk again? Well, in the worst case scenario, that is, when the numbers are arranged largest to lowest, then we would have to walk through it n times too. So n steps per walk, and n walks, then that is a total of n[sup]2[/sup]. In the best case scenario, we would only need to walk through it once (and see it's already sorted). In that case, n steps per walk, and 1 walk, then that is a total of n. Now you might have noticed this, but n[sup]2[/sup] is a pretty bad number to have. If we had to sort 100 elements, then that means we have to take 100[sup]2[/sup] steps, that is 10,000 steps for a 100 elements!Selection Sort
Let's use another approach. This time, we walk through the input left to right, keeping track of the smallest number we find. After each walkthrough, we swap the smallest number with the left-most one that is not in the correct place yet. To again save you some bandwidth, here is an animation, again courtesy of Wikipedia: The red item is the current lowest number that we save, presumably in a variable. After the walk, we put this lowest number to the top of the list, and we know that this is sorted already (yellow). But what is the running time? In the worst case scenario, we would have to do n walks. The steps per walk decreases. Since we know that the first few numbers are already sorted, we can skip it. So our walks then become n, n-1, n-2... and so on. The total running time of this is (n(n-1))/2. However, computers are so fast that a division is negligible, so we can say that this is n[sup]2[/sup] still. But what about the best case scenario? If you think about it, we would still need to walk through the input, even if it is already arranged! So our best case scenario is also n[sup]2[/sup].Insertion sort
Okay, so all of our examples so far have been n[sup]2[/sup] which we know is bad. Let's take a look at another algorithm shall we? Imagine you're sorting a deck of cards. Now I don't know about you, but what most would do is walk through the deck, and insert the card in front of them to its right position in another deck. This might help you visualize it: This is awesome! We only need to walk through the list once! So the running time is n right? Not quite. What if we needed to insert 1 to our sorted list? We would have to move every single one of them to make room. If you consider this, the worst case running time is actually n[sup]2[/sup]. Let's table them shall we:[table][tr][td] [/td][td]?[/td][td]O[/td][/tr][tr][td]Bubble Sort[/td][td]n[/td][td]n[sup]2[/sup][/td][/tr][tr][td]Selection Sort [/td][td]n[sup]2[/sup][/td][td]n[sup]2[/sup][/td][/tr][tr][td]Insertion Sort[/td][td]n[/td][td]n[sup]2[/sup][/td][/tr][/table] Are we screwed? Do we have no choice but n[sup]2[/sup] running time for sorts? Of course not! If you've been vigilant, you'll realize that none of the algorithms introduced so far uses the same 'divide-and-conquer' approach of binary search. You might want to take a look at this visualization to see just how fast merge sort is to bubble, selection, and insertion sorts. Note: That little tidbit about computers doing repeated addition is a bit of a lie. Credits - Visualization Animations from Wikipedia.Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Arian Allenson M. Valdez
How To Reverse Time - Introduction to Git, Cloud Computing, and Version Control
Don't wait until it's too late! Learn how to use Git and cloud technology to save your files and easily use your previo…
Why your Games are Unfinished, and What To Do About It
Find out the most common pitfalls of beginning developers, and how successful game developers overcame them
















Discussion