Working with Data Structures and Algorithms
Let’s start with an everyday life example, assume you need to move from point A, say city square to point B, maybe Makerere University. There are many ways to do this: Take an uber, Take a safe boda or taxify, Take a regular boda-boda, Take a taxi, Take a pioneer bus, Walk, among others. Your choice of means would probably depend on time, cost or even convenience. Let us study two of these means.
- The regular boda-boda — stop a moving boda heading towards your direction or walk to the nearest boda-boda stage and get yourself one, inform the rider about your destination and your price, bargain if you have to, then set off.
- The pioneer bus — walk to the nearest pioneer bus stage (those going towards the direction of your destination of course), get a ticket, enter the bus, wait till it is fully occupied and then set off.
Both of those means will get you to your destination but one is most probably cheaper than the other, as one is faster. If I were in a hurry, I would go with the regular boda-boda to save on time, likewise if I wanted to save on money, I would take the pioneer bus. So in this case we have done a cost versus time assessment.
As there are different ways for us to accomplish our tasks, there are also different ways for a computer to do something and produce some kind of output, based on efficiency and effectiveness. It is our role to tell the computer what to do and how to do it. That is the purpose of algorithms, which according to wikipedia, are “unambiguous” specifications of how to solve a class of problems. But in simple terms, an algorithm is a step-by-step procedure of how to accomplish a specific task or solve a particular problem.
Consider this as our input array:
[222,5,0,7,43,99,1,65]
which we want to be sorted to give a new list
[0,1,5,7,43,65,99,222]
There are a number of different sorting algorithms that we could put into use. Examples include bubble sort, quick sort and a merge sort. All of these actually sort data but are unique in different ways, the choice of algorithm would depend on time, size, computational complexity, etc of the list.
We could also sort that list using a python method sort(). Here goes:
lista = [222,5,0,7,43,99,1,65]lista.sort()print(lista)
Running the above piece of code would yield us our desired output:
[0,1,5,7,43,65,99,222]
If you are wondering what an array or list are, these are simply data structures. Data structures are format for organizing and storing data (so as to perform operations in an efficient way). So the method sort() has been invoked by a list or data of type list. Other examples include string, integer/int, dictionary/dict and float.
If I wanted to perform some arithmetic computations, I would focus on numeric data types like integers and floats because they are suitable for my computation. If I wanted to capture peoples names as input, I would save them under the string data type.
We have looked at algorithms, which is our way of telling the computer what to do and how to do it and data structures which are formats of organizing and storing data. Algorithms make use of data stored in some format that is, data structures.