Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Big-O For The Non-CS Degree - Part 1
Travis Ramos
Travis Ramos

Posted on • Edited on • Originally published attravislramos.com

     

Big-O For The Non-CS Degree - Part 1

Ever wonder why some algorithms are faster than others? Yeah me neither, but Big-O Notation is the likely source of explanation, and in this two-part series you will learn why!

So what the heck is Big-O Notation?

It's a way of measuring how long an algorithm will take to execute, and how well it scales based on the size of the dataset. Basically, it measures algorithmic efficiency.

Let's say for example we have a list of 15 people, and we would like to sort through these 15 people to find every person whose first name starts with the letter T. Well, there are different algorithms you could use to sort through this list all ranging in different levels of complexity, with some performing better than others.

Now let us pretend that list just jumped up to 1 million names. How do you think this will affect the performance and complexity?

The answers to these questions can be found using Big-O notation.

Many Flavors

Big-O comes in different forms:
- O(1)
- O(log n)
- O(n)
- O(n log n)
- O(n^2)
- O(2^n)
- O(n!)
In this post, I will be discussing the first three variations with the last four discussed in the next post, so stay tuned for that!

O(1) - Constant Time

Constant time complexity doesn’t care about the size of the data being passed in. The execution time will remain the same regardless of the dataset. Whether our list contained 5 items or 1,000 items, it doesn't matter. This means that this notation is very scalable and independent on time.

Let’s say for example we have an array of numbers and we want to find the second number in that list. No matter what the size of the list is, finding the second number will always take the same amount of time.

letsmallList=[0,1,2]letlargeList=[0,1,2,3,4,5,6,7,8,9,10]letlogSecondNumber=(list)=>{console.log(list[1]);}logSecondNumber(smallList)logSecondNumber(largeList)
Enter fullscreen modeExit fullscreen mode

Both calls to the function will execute in the same amount of time even though one list is larger than the other.

O(log n) - Logarithmic Time

Logarithmic time complexity is the time it takes to execute depending on the logarithm of the input size. A good example of this would be a binary search. You divide the dataset continuously until you get to the point you want.

In our example below I am looping through the list of numbers and checking whether our middle position in the array is equal to our target value. If it isn’t we divide the list of numbers accordingly, calculate our new middle position, and check again. This will continue until either we find the number we are looking for, or we run out of numbers in our array.

functionbinarySearch(array,targetValue){letminIndex=0;letmaxIndex=array.length-1;letmiddleIndex=Math.floor((maxIndex+minIndex)/2);while(array[middleIndex]!=targetValue&&minIndex<maxIndex){if(targetValue<array[middleIndex]){maxIndex=middleIndex-1;}elseif(targetValue>array[middleIndex]){minIndex=middleIndex+1;}middleIndex=Math.floor((maxIndex+minIndex)/2);}return(array[middleIndex]!=targetValue)?-1:middleIndex;};letnumbers=[1,2,3,4,5,6,7,8,9,10];binarySearch(numbers,7);
Enter fullscreen modeExit fullscreen mode

O(n) - Linear Time

Linear time complexity means that the time to execute the algorithm has a direct relationship with the size of n. As more items are added to the dataset, the time to execute will scale up proportionally.

Looking at our example below, we are using a for loop to print out each item in our array. For each item added to this array, it will increase the time it takes to execute by n.

letjunkFood=['pizza','cookie','candy','icecream']loopThroughOurJunkFood(junkFood){for(leti=0;i>junkFood.length;i++){console.log(junkFood[i]);}}
Enter fullscreen modeExit fullscreen mode

If we were to add another item to our junkFood array, the time it takes to execute our function will increase linearly.

More to come…

In the next post in this series, we will go over the rest of our Big-O notation flavors so stay tuned for that!

If you like what you see and want to read more, head over to myblog where I write more about software development, along with personal development!

If you found this article helpful,subscribe to my newsletter where I'll be sending more content like this directly to your inbox on a weekly basis!

Top comments(9)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
rakshakannu profile image
Raksha Kannusami
Software engineer | Coffee Lover
  • Email
  • Location
    Tamil Nadu, India
  • Education
    Btech computer science
  • Work
    Software Engineer
  • Joined

Beautifully written! 👏

CollapseExpand
 
travislramos profile image
Travis Ramos
Code + Coffee = ❤️Let's make you the best developer you can be!
  • Location
    Houston
  • Work
    Frontend Developer at WaterlooData & Full Stack Immersive Web Instructor at DigitalCrafts
  • Joined

Thank you, I'm glad you enjoyed it! I will be working on the second part soon!

CollapseExpand
 
rakshakannu profile image
Raksha Kannusami
Software engineer | Coffee Lover
  • Email
  • Location
    Tamil Nadu, India
  • Education
    Btech computer science
  • Work
    Software Engineer
  • Joined

Waiting for it!

CollapseExpand
 
gcasa profile image
Gregory Casamento
I am the lead developer of the GNUstep project.
  • Location
    Laurel, MD
  • Work
    Software Engineer at Open Logic Corporation
  • Joined

Every developer. Every single one should care about algorithmic complexity. There is no "me neither"

CollapseExpand
 
travislramos profile image
Travis Ramos
Code + Coffee = ❤️Let's make you the best developer you can be!
  • Location
    Houston
  • Work
    Frontend Developer at WaterlooData & Full Stack Immersive Web Instructor at DigitalCrafts
  • Joined

It was more of a joke, but glad you caught it!

CollapseExpand
 
detzam profile image
webstuff
  • Work
    Fullstack webdeveloper
  • Joined

Usefull article. Thank you, sir 🙇

CollapseExpand
 
travislramos profile image
Travis Ramos
Code + Coffee = ❤️Let's make you the best developer you can be!
  • Location
    Houston
  • Work
    Frontend Developer at WaterlooData & Full Stack Immersive Web Instructor at DigitalCrafts
  • Joined

Thank you, I'm glad you enjoyed it!

CollapseExpand
 
akilarasan profile image
Akila Arasan
FullStack Engineer || Bibilophile || Botanist || Generalist || compounding skills in data science
  • Email
  • Location
    coimbatore
  • Education
    Bachelor of engineering in EEE
  • Work
    software engineer at Squashapps
  • Joined

Clean examples

CollapseExpand
 
travislramos profile image
Travis Ramos
Code + Coffee = ❤️Let's make you the best developer you can be!
  • Location
    Houston
  • Work
    Frontend Developer at WaterlooData & Full Stack Immersive Web Instructor at DigitalCrafts
  • Joined

Thank you! Stay tuned for the second half coming soon!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Code + Coffee = ❤️Let's make you the best developer you can be!
  • Location
    Houston
  • Work
    Frontend Developer at WaterlooData & Full Stack Immersive Web Instructor at DigitalCrafts
  • Joined

More fromTravis Ramos

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp