# Algorithms

Algorithm – step-by-step instructions for how to solve a problem. You can use well-known algorithms, such as for searching or sorting, but sometimes, you will need to write your own algorithms for your software. Computer science majors will have to take classes about data structures and algorithms, and there are a lot of common ones you will use, but that’s not the be-all and end-all of algorithms.

Examples of algorithms include greedy backtracking, hill climbing, divide-and-conquer, A* pathfinding, flood fill, branch-and-bound, Monte Carlo simulations, and consensus algorithms. In-depth information about data structures and algorithms is beyond the scope of this introductory book. I am just helping you become aware of their existence so you can use them later in your programming career.

Searching – searching is useful for a multitude of situations in programming. Maybe you have an array or a dictionary, and you want to see if a specific string is within it. Python makes it easy to search without having to make a search algorithm manually.

Sorting – when you have a lot of data, such as items in a grocery list, you might want to sort it, such as alphabetically, by price, ascending or descending, and so on. Things can get out of order quickly, such as if you keep appending things to a grocery list.

There are many algorithms for sorting, which aren’t all equal. Some examples of sorting algorithms include selection sort, bubble sort, and quick sort. You will only need to know the ins and outs of specific sorting algorithms if you want to get a computer science degree. If you’re programming on your own, you will only need to know of the existence of sorting algorithms, and how to use features built into your language, that can sort things for you without you having to code your own sorting algorithm from scratch. If you have an unsorted list, a language like Python features a sort() method. For example, my_list.sort() will sort it. You don’t have to know about the implementation details of the sorting algorithm, just that it sorts it.

Some examples of sorting algorithms include bubble sort, selection sort, and radix sort. The sorting algorithms that are very easy to implement are often very inefficient.

Some jokes related to sorting algorithms include bogosort and quantum bogosort. Bogosort means bogus sort, and it involves randomly shuffling the entire list if it’s out of order. Needless to say, it doesn’t work very well, as the odds of a random shuffle putting everything in order are slim. Another silly sorting algorithm is quantum bogosort. Check if a list is in order or not. If it’s out of order, destroy the universe. If there are multiple universes, then eventually the only universes left will be the ones that contain sorted lists.

Optimization – making something faster or use fewer resources. When people refer to a computer being fast or slow, sometimes they are referring to the hardware. But software can be fast or slow too. Sometimes software can be very inefficient and make even a speedy computer take a long time to do something. Depending on how you implement something, if you do it poorly, you can make it take orders of magnitude longer than if you thought about your algorithm a little more carefully.

Other algorithms – some other algorithms worth knowing about include flood fill and pathfinding. Imagine using MS Paint or Photoshop, and you have many lines drawn that separates the image into sections of the same color, divided by pixels of a different color. How does the fill tool know when to stop filling in color? It uses a flood fill algorithm. A pathfinding algorithm, such as A*, can be used for many different things. If you make a video game with point-and-click controls, how does the character move from their current position to the desired position? Using a pathfinding algorithm.

At a hackathon in St. Louis called eHacks, I teamed up with someone to make a simple video game called Road Warrior. The goal is to connect a road from one side of the screen to the other, though there are randomly-generated obstacles that you have to build around. Once you think your path is right, you can click to verify if it’s good or not, and the program runs a path traversal algorithm to check it.