#Big_O_notation
Explore tagged Tumblr posts
Text
In Data Structure and Algorithms in C programming, we have learned so many algorithms where we have understood different aspects and purposes of an algorithm.
1 note
·
View note
Photo

Sleep sort for lazies
Getting a computer to sort a list of numbers is surprisingly complicated, and there are a zillion different ways to do it. This means mathematicians and computer scientists think that it's super interesting and spend a lot of time studying, developing, and comparing sorting algorithms. You could spend a whole day just watching YouTube videos of people representing these algorithms in music and dance.
Some algorithms take a lot more time, power, and energy for computers to run than others. The "expensiveness" of an algorithm is expressed in "Big O notation", which looks like this:
O(n)
The "bigger" n is, the more expensive the algorithm is. O(n3) is more expensive than O(n2), which is more expensive than O(n). O(wtf) is a joke way of saying "this algorithm is really, really expensive."
Obviously, less expensive is "good," so algorithms try to be really efficient. They usually tell computers to do something like, "Randomly pick one of the numbers in the list. Put all the numbers smaller than it on the left and all the numbers bigger than it on the right. Then do the same thing in the left chunk. Then the right chunk. Lather, rinse, repeat." (This is the famous Quicksort algorithm.)
The JavaScript code in the image takes a totally different approach. It says, "Set a bunch of timers, one for each number in the list. Have them all start at the same time, and have each one go off when its number of miliseconds passes. When a timer goes off, spit out its number on the screen."
So, 1 milisecond after the program starts, the first timer will go off and the computer will spit out 1. 2 miliseconds after it starts the computer will spit out 2. 8 miliseconds after it starts it'll spit out 8, and so on.
The joke is programmers will find this really counterintuitive -- you rarely want to tell your computer to just wait, because you always want your program to go as fast as possible -- but it actually works! The catch is if one of the numbers is really big, you could be waiting around forever for its timer to go off.
8 notes
·
View notes