r/compsci • • Aug 14 '13

Algorithims Everyone Should Know?

What are some of you're favourite algoritms or concepts that you think everyone should know, whether they solve problems that crop up frequently, or are just beautiful in their construction?

378 Upvotes

118 comments sorted by

View all comments

416

u/blexim Aug 14 '13 edited Aug 14 '13

Here's a random selection, approximately ordered from most basic to more advanced. I may add more later...

Edit: this is definitely not meant to be an exhaustive list, it's just a random selection of things I use a lot/think are cool.

Numerical:

Data structures:

Sorting & searching arrays:

Tree search:

Graphs:

Automata and parsing:

Numerical optimization:

Combinatorial optimization:

Graphics:

Compilers:

Machine learning:

Cryptography:

Miscellaneous:

Edit: Thanks for the gold!

37

u/[deleted] Aug 14 '13

Bubblesort? Why? Put mergesort there instead. Not only is it one of the most beautiful and simple algorithms known, it's extremely useful.

72

u/asimian Aug 14 '13

Bubble sort is useful if the data is almost completely sorted to begin with. In that case it can actually be the fastest algorithm.

62

u/TarMil Aug 14 '13

This is the best answer. It's a prime example of why knowing the properties of your data set is very important in choosing the most suitable algorithm.

34

u/zzing Aug 14 '13

I prefer the quantum bogosort. It is a O(n) algorithm on the right hardware.

12

u/Crandom Aug 15 '13

Or O(infinity) if not :p

→ More replies (5)

11

u/[deleted] Aug 14 '13

How is it more advantageous than insertion sort in that case?

2

u/[deleted] Aug 17 '13

I am curious as well, as even the bubble sort wiki page submits to insertion sort being more efficient in this context.

→ More replies (1)

2

u/[deleted] Aug 14 '13

Depends in which way it's "almost" sorted. Some almost sorted data can also bring about bubblesort's worst case (cocktail sort can help). I suspect that if there's a chance of data being almost sorted, a linear check to see if it is sorted followed by a proper sort if necessary will be better.

2

u/SeanNoxious Aug 15 '13

cocktail sort

I really like Comb Sort http://en.wikipedia.org/wiki/Comb_sort the wiki animation is so cool.

2

u/asimian Aug 14 '13

Yes of course. Often if data is "almost sorted" it's more likely that an element or two is transposed than there is one all the way at the end. Bubble sort is very fast in these cases.

Also, if the data is sorted, bubble sorting it is exactly the same as doing a linear check.

→ More replies (2)

14

u/slapnuttz Aug 14 '13

I love me some merge sort. It is always the same O time. However, bubble sort beats out merge, quick, shell, insertion, and a few other sorts for very small data sets, which is why some 'sorting books' teach hybrid approaches where you do X sort until you are sorting a data set of 10 or less then bubble sort it.

More importantly however, you should know bubble sort for 2 reasons. 1 is that it is the most natural of sorts, so you know it regardless. 2 You should know what NOT to do in regular practice and know that you need to question your instincts given reason 1.

18

u/[deleted] Aug 14 '13 edited Aug 14 '13

If we're going to talk implementation then I'm pretty sure insertion sort is going to be quicker than bubblesort since you can keep the value to be inserted in a register. Bubblesort doesn't keep anything around for long so it's bad for caching. This is also the reason quicksort itself is fast, of course, keeping stuff in registers and having tight loops makes fast code. But these are implementation details and they distract from the beauty of algorithms.

3

u/mcvoid1 Aug 14 '13

Agreed. On my test bed, insertion and gnome sort outperform bubble sort on all data sets, even small ones.

5

u/anchoa Aug 14 '13

This.

In grad school was an instructor for an intro to programming course. I taught Bubble sort because it was a very easy to understand algorithm and it got the students warmed up to learn better sorts. It also gave me a point of comparison, showing them how the other, less intuitive algorithms, were significantly better.

It may not be that useful to know on its own, but it's a good teaching tool.

→ More replies (1)

2

u/blexim Aug 14 '13

I wanted an O(n2) sorting algorithm to compare to quicksort. Mergesort is a fine alternative O(n log n) algorithm, but I didn't want a super long list of sorting algorithms.

8

u/zzopp Aug 14 '13

I would also include radix sort, since it's fundamentally different from the others.

5

u/[deleted] Aug 14 '13

Quicksort is an O(n2) algorithm! It's also a good example of why big-O isn't always "right".

Personally I think quicksort is overused in teaching. Mergesort is a much nicer way to illustrate recursion and divide and conquer and it's really O(n log n) with a proof that can be understood by undergrads. Proving quicksort is logarithmic in the average case is much more difficult. Quicksort gets used because of its popularity and the poorly named qsort function in C.

10

u/blexim Aug 14 '13

Quicksort's worst case complexity is affected in a big why by your pivot selection strategy. You can make it O(n log n) by using the median element as your pivot (which takes O(n) time to find). In practice you wouldn't want to do this because you end up slower for pretty much any practical case. Mergesort is indeed easier to analyse, but it's not used in practice as much as quicksort. This isn't because it has a fancy name, it's because it's an in place sort (so uses less memory) and is empirically faster for most applications.

→ More replies (2)
→ More replies (4)

10

u/MyNameIsFuchs Aug 14 '13

You forgot a very crucial one: Select/Quickselect

Also:

http://www.siam.org/pdf/news/637.pdf

→ More replies (1)

3

u/yafsho Sep 03 '13

I know I am late to this party, but I saved this to look at later.

If anyone is like me and has limited internet, you might be interested in this pdf-downloadable wikipedia book that I created from blexim's links: https://en.wikipedia.org/wiki/User:Yafsho/Books/bleximbook

5

u/eigenman Aug 14 '13

Great list. I would add a section for

Unconstrained Optimization:

BFGS and Conjugate Gradient

→ More replies (1)

2

u/TopcatTomki Aug 14 '13

That is one hell of a list! Thanks

→ More replies (1)

2

u/shamen_uk Aug 14 '13 edited Aug 14 '13

Nice list. I would add EA/GA to the optimisation list. Also perhaps K-means too?

2

u/Porges Aug 14 '13

Under Automata and parsing, I'd suggest Brzozowski's minimization algorithm, solely because it's easy to remember, and way cool.

You can obtain a reversed minimal DFA from an input by doing

reverseAndMinimize = powerset ∘ reverse

So Brzozowski's algorithm is... just do this twice to obtain a minimal DFA from the original input:

minimize = powerset ∘ reverse ∘ powerset ∘ reverse
→ More replies (30)