Data Structures, Algorithms, And Big O Notation! A Beginners Guide.

I may or may not regret this decision, but I’m sticking to it.

Data Structures, what are they?

A data structure is a way of storing and organizing information (aka data) that allows it to be accessed and manipulated efficiently. Data structures are widely used throughout software engineering and computer science. Chances are if you are even just beginning your journey on learning to code, you’ve already worked with them a whole bunch. The scope and variety of all of the different data structures in regards to software development is vast, but here are two of the more common data structures:

  • Array: data is organized within brackets in sequential order. Each data entry within these brackets is called an element and is separated by a comma. These elements can be accessed via numbered indexes which start at 0 and increment per data entry. A Javascript example would be:
below is an example of how to extract a value from this array
> catchPhrase[0]
"Big"
  • Hash: data is organized within braces with a key pointing to a value. These key/value pairs are generally separated by a coma. The data (value) can be accessed by its key. This has the advantage of having a constant reference point to access said value. The syntax for this varies in different languages, and in Javascript this is called an object. Here’s a Javascript example:
below is an example of how to extract a value from this hash
> characters['Android']
"Dorothy"
A visual aid for different data structures, these can even be nested inside one another.

What is an Algorithm?

An algorithm is the procedure or process you use for solving a problem or performing a task. In the next section we will look at how algorithms have their efficiency classified using Big O notation. We can use an analogy for a common task as an example of an algorithm. The common task being building a gigantic crime fighting robot like in the show I’ve regrettably made the decision to continuously reference. Here are the theoretical steps:

  1. Be a Bruce Wayne esq. billionaire.
  2. Either go to giant robot building school or hire someone who has.
  3. Buy required tools and materials.
  4. Assemble Gundam Wing style robot or hire someone to do so.
below are some examples of different returns this function might have, this function uses the previously defined catchPhrase array by default.
> findIndexOfElement("Showtime!")
2
> findIndexOfElement("Not Present")
false

A Brief Description Of Big O Notation

Sadly this has absolutely nothing to do with Big O Notation, but I’m stubbornly keeping this analogy.
  • Constant Time or O(1) the algorithm has a constant run time relative to its input. Generally this is something that can be done in one “step”
  • Linear Time or O(n) the algorithms execution time increases relative to the size of its input. Generally this is some basic iteration
  • Quadratic Time or O(n²) the algorithms execution time increases relative to the size of its input squared. Generally this involves looped iteration
As you can imagine the runtime for this increases exponentially as the input increases because you are iterating through the entire array per element.

Conclusion

The data structure you use and algorithm you design can vary greatly but do matter. Some data structures are better suited for organizing a particular set of data than others, and it’s important to start thinking in terms of big o notation when coming up with algorithms to solve problems. Doing so will not only benefit you as a software developer, but will also keep your code succinct and efficient.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store