If you are a Computer Science student or graduate, it is 100% safe to assume this is a subject you absolutely know about.
But if you are currently...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
This is really good.... really really nice thanks so much...
Sharing right away
Buh please can anyone recommend a site for me to learn algorithm and Data structure.. like a toturial Pls
Am a newbie to computer science 🙃🤠
I would really appreciate
Shalom
Hey Dada David, thanks!
There are plenty of resources for learning Data structure and Algorithms. I think choosing a learning platform or teacher is a personal matter. Personally I don't settle down with only one resource, I like to check through different ones to see different angles. My recommendation would be to research further until you find a resource (or resources) that speaks to you with the way of explaining the subjects.
I wrote a book about it! technicalinterviews.dev/
Check algoexpert of Clement it's a good one
Great article. But you are, like most authors writing in this topic, missing one important point. All the complexity analysis is based on selection of a basic operation. And depending on this basic operation preference, resulting complexity might greatly differ. See my old answer and the discussion on SO: stackoverflow.com/a/34749542/1079908
Hey Ferit,
Thanks for your feedback and interesting input! I'd like to point out my main idea of writing this article. I had 2 primary type of readers in mind: either someone encounters Big O for the first time, or someone needs to refresh their memory. I wanted to put comprehensive emphasis on the main parts while keeping it as simple as possible.
I have also considered diving into the details of complexity analysis as well. But I have realized something. If I also include them, it won't be an article for beginners first ride or a memory refresher anymore. It would be unfair if I expect a beginner to understand Big O and it's complexities, then be able to make detailed analysis at the end of a single article.
That's why I decided to spare complexity analysis part for another article instead. I will be updating this article with a link to it when it is ready.
I'm quite certain that was obvious from the tone of the article, but thanks for clarifying...
Oh, that explains. Thanks for the answer and looking forward to the next article!
Very great article and explanation. One of the best so far.Quick question how are shift() and unshift() linear runtime complexities?
Hey Oluwaseun, thanks!
Great question. To be able to make a comparison, let's take a look at
push()
andpop()
methods first:push()
- Adds an item at the end of an arraypop()
- Removes an item from the end of an arrayThey don't need to iterate whole array to make these changes, they only need to operate with one element with a known index - last one.
Previous indexes inside the array does not getting effected by this change.
That's why both are these having
O(1)
- Constant time complexity.Now let's think about how
shift()
andunshift()
operates:shift()
- Removes an item from the beginning of an arrayunshift()
- Adds an item or items from the beginning of an array.Even they also target a known index, they work different. Because mutation takes place at the beginning of an array, which causes an index change for all other elements coming after. Let's take
shift()
for an example:You see, before
shift()
, 'b' was at index[1] and 'c' was at index[2].Now their indexes have changed to index[0] and index[1] respectively.
Therefore we can say runtime to operate an index shift is directly linked to the length of array, so these methods are having Linear runtime complexity.
But again, you will find the perfect accurate answer by reading the source code of Javascript Engine you are dealing with, which is often V8:
github.com/v8/v8/blob/master/src/o...
And this is where the distinction in memory between an array and a linked list comes in, I suppose, because sticking something on the front of a linked list should be constant like the push & pop. (Meanwhile, accessing within a linked list is linear.)
Oh so what makes shift() and unshift() O(n) is because of the change in the index they cause for elements in the array and not because they affect a single element at a known index?
Yes, main reason is mutation place: beginning of an array. Adding or removing an element from start changes index order of all other elements coming after it.
Thanks Sahin
this is a super important point :-) thanks!
Nice article, but one criticism. Unless the intent is to confuse the non-mathematical do not mix "38% faster" and "40 times faster" in the same sentence. Indeed, you have confused yourself. You did not mean "38% faster" you meant "ran in 38% of the time" which I would interpret as 263% of the speed or 163% faster.
Hi Ashley,
Thanks! I really appreciate your feedback, you have a solid point. Now I have updated the part accordingly. I will be sticking with the "times faster" way of explaining, I believe we can safely interpret 263% of the speed as 2,6 times faster here.
This is great. Sharing this on linkedIn right away. :)
Thanks for this. Occasionally self-taught devs ask me for resources on formal CS stuff, and I'm bookmarking this for next time.
Hey Mackenzie, your welcome. Glad to hear!
Thank you very much.
Nice guide! If you're going to use images and graphics from other sources though you should credit them.
Hey Emma!
Thanks for your feedback and also letting me know about the missing credit. I usually include the credits via using image alt tags, seems like the "Big O Graph" escaped my notice. Now the post is up to date with a visible link to it's resource 👍
bigocheatsheet.com is a great resource and definitely worth to keep it bookmarked!
This makes me wonder; is there a complete list of js array functions with their corresponding complexity, so we can implement the best way of doing something?
I would suggest you to check this stackoverflow thread:
stackoverflow.com/questions/115143...
If you really really want to dive deeper and can read C++ code, reading the Javascript V8 Engine source code can give you more accurate picture:
github.com/v8/v8/blob/master/src/o...
Congrats! Excellent article.
Thank you Dercilio!
the greatest programmer in the world is the one who can turn O(n!) into O(1)
Great article. I have an okayish understanding of Big O, this article gave me better clarity on some areas. Thank you.
Hey Vignesh, thank you. Glad to hear!
Great article! One of the best explanations about the topic that i've seen! thanks!!
Thank you Pablo!