Choice of programming language for learning data structures and algorithms

AlgorithmData StructuresLanguage Agnostic

Algorithm Problem Overview


Which programming language would you recommend to learn about data structures and algorithms in?

Considering the following:

  • Personal experience
  • Language features (pointers, OO, etc)
  • Suitability for learning DS & A concepts

I ask because there are some books out there that are programming language-agnostic (written from a Mathematical perspective, and use pseudocode). If I learn from one of these, I would like to choose a programming language to code and run the algorithms in.

Then, there are other books which introduce DS & A concepts with examples written in a particular programming laguage - and I would like to code these algorithms as well - thus, to a certain extent, the language picks the book too.

Either way, I have to pick a language, and I would prefer to stick to one throughout. Setting aside personal language preferences, which one is best for this purpose?

Algorithm Solutions


Solution 1 - Algorithm

The answer to this question depends on exactly what you want to learn.

Python and Ruby

High-level languages like Python and Ruby are often suggested because they are high level and the syntax is quite readable. However, these languages all have abstractions for the common data structures. There's nothing stopping you implementing your own versions as a learning exercise but you may find that you're building high-level data structures on top of other high-level data structures, which isn't necessarily useful.

Also, Ruby and Python are dynamically-typed languages. This can be good but it can also be confusing for the beginner and it can be harder (initially) to catch errors since they typically won't be apparent until runtime.

C

C is at the other extreme. It's good if you want to learn really low-level details like how the memory is managed but memory management is suddenly an important consideration, as in, correct usage of malloc()/free(). That can be distracting. Also, C isn't object-oriented. That's not a bad thing but simply worth noting.

C++

C++ has been mentioned. As I said in the comment, I think this is a terrible choice. C++ is hideously complicated even in simple usage and has a ridiculous amount of "gotchas". Also, C++ has no common base class. This is important because data structures like hash tables rely on there being a common base class. You could implement a version for a nominal base class but it's a little less useful.

Java

Java has also been mentioned. Many people like to hate Java and it's true that the language is extremely verbose and lacking in some of the more modern language features (eg closures) but none of that really matters. Java is statically typed and has garbage collection. This means the Java compiler will catch many errors that dynamically typed languages won't (until runtime) and there's no dealing with segmentation faults (which isn't to say you can't leak memory in Java; obviously you can). I think Java is a fine choice.

C#

C# the language is like a more modern version of Java. Like Java, it is a managed (garbage collected) intermediate compiled language that runs on a virtual machine. Every other language listed here apart from C/C++ also run on a virtual machine but Python, Ruby, etc are interpreted directly rather than compiled to bytecode.

C# has the same pros and cons as Java, basically.

Haskell (etc)

Lastly, you have functional languages: Haskell, OCaml, Scheme/Lisp, Clojure, F#, etc. These think about all problems in a very different way and are worth learning at some point but again it comes down to what you want to learn: functional programming or data structures? I'd stick to learning one thing at a time rather than confusing the issue. If you do learn a functional language at some point (which I would recommend), Haskell is a safe and fine choice.

My Advice

Pick Java or C#. Both have free, excellent IDEs (Eclipse, Netbeans and IntelliJ Community Edition for Java, Visual Studio Express for C#, Visual studio community edition) that make writing and running code a snap. If you use no native data structure more complex than an array and any object you yourself write you'll learn basically the same thing as you would in C/C++ but without having to actually manage memory.

Let me explain: an extensible hash table needs to be resized if sufficient elements are added. In any implementation that will mean doing something like doubling the size of the backing data structure (typically an array) and copying in the existing elements. The implementation is basically the same in all imperative languages but in C/C++ you have to deal with segmentation faults when you don't allocate or deallocate something correctly.

Python or Ruby (it doesn't really matter which) would be my next choice (and very close to the other two) just because the dynamic typing could be problematic at first.

Solution 2 - Algorithm

I would recommend Java mainly because:

  • garbage collection
  • references
  • rich collections

EDIT: Down voters please explain.

Solution 3 - Algorithm

In my opinion, C would be the best language to learn data structures and algorithms because it will force you to write your own. It will force you to understand pointers, dynamic memory allocation, and the implementations behind the popular data structures like linked lists, hash tables, etc. Many of which are things you can take for granted in higher level languages (Java, C#, etc.).

Solution 4 - Algorithm

Python is great. Easy to read, fully featured. If you are going to work with pseudocode, Python will look pretty familiar.

Python is already the algorithms language of choice at UC Irvine, where it is described like so:
"Python represents an algorithm-oriented language that has been sorely needed in education. The advantages of Python include its textbook-like syntax and interactivity that encourages experimentation."

Python also works in a beginner friendly way with Gato, a graph making tool. Learning Algorithms and Data Structures is one top that can help by being made visual, something that Gato makes it easy to do (without learning any complex graphing libraries)

Solution 5 - Algorithm

If the purpose is to only learn about data structures and algorithms, I would say JavaScript. You can run your code in a browser. You have a very flexible object handling and you can focus entirely on the data structures and algorithms and not memory management, language constructs or other stuff that will take the focus away from the actual computer science you are learning.

The bonus is also that you can easily visualize various data structures by using the browser to render graphs and trees using DOM and Canvas.

CS courses over the years tend to change the language in which the subject is taught, simply because newer and better implementations of languages that ease learning has arrived which makes it easier to focus on the actual problem.

Solution 6 - Algorithm

If you want to take the path of least resistance, then Python. It'll have the minimum amount of unnecessary boiler plate and such like.

Ideally, I'd want to learn algorithms in C, so you can learn what's going on at the memory level; I'd also want to learn algorithms in a functional language, so you can see how similar algorithms work with persistent data structures.

Knuth's famous books contain large amounts of (invented platform) assembler code. This is recommended if you want to be super hardcore. Personally, though, I worked in C when I was working through my algorithms class (disclosure: this was only a couple of years ago). I'm sometimes work on some problems in Knuth, but I don't know if I'd go with MMIX entirely as my language of choice for learning algorithms. It's a bit overkill, I'd feel.

EDIT: It also depends on what you're familiar with. If you want to start working through an algorithms text right now, and you've never worked much with C, then Python is far and away the correct answer. You want the language not to be a huge hurdle to overcome, because you want to enjoy this. I know I did.

Last point: at least when I was learning algorithms, I spent a hell of a lot of time working on paper. I think that's important -- I mean you want to learn about asymptotics, etc. Spending all of your time implementing algorithms in whatever language is not the thing to do.

Solution 7 - Algorithm

Oberon-2 or Component Pascal. The last one is a superset of the first one.

Einstein once said "Make it as simple as possible, but not simpler." This phrase was chosen by Prof. Niklaus Wirth as epigraph to the original Oberon language report. And it's true for Oberon's descendants mentioned above.

When it comes to the perfection of programming language I like to quote Antoine de Saint-Exupéry: "A designer knows he has arrived at perfection not when there is no longer anything to add, but wen there is no longer anything to take away.". Wirth, even if not achieved this, is on the right path. In "Wirth programming languages line" (Algol -> Pascal -> Modula-2 -> Oberon -> Oberon-2) each subsequent language is simpler and at the same time more powerful than the previous one.

Powerful but simple languages following the principle of least surprise. Strong static typing, easy object-oriented facilities, garbage collection. The feature list is not big but it's enough to be productive and not to complicate things especially on the initial stages.

When you want to learn algorithms and data structures, you mean it. But if your language is "powerful" (has a lot of features like C++, C#, Java, Python, ...) you will waste a lot of time learning language, not algorithms and data structures. You will not see the forest for the trees. =) You can think of trees as syntax elements (and any other features) and of forest as important concept (any algorithm, data structure, may be OOP, whatever). The more features (trees) you have in your language the more complicated become the task to step back and to understand the concepts (to see the forest).

But if language is really powerful (has small set well proven features) the language itself goes to second place. There not so many trees so you can do a couple of steps back and ... Well I think that's enough analogies. =)

Also many books on algorithms and data structures use Algol/Pascal-like pseudocode and it will be easy to convert examples in this languages. And you can directly use examples from Wirth's "Algorithms and Data Structures" book. Oberon edition (2004), PDF (1.2 MB).

Some additional links:

Solution 8 - Algorithm

I would suggest Ada. It has features for data constructs not found in other languages, such as range checks type Day is range 1 .. 31; Also it has very strict compile-time and run-time checking (unless you choose to turn it off), making it easier to find bugs in your implementation.

Solution 9 - Algorithm

"If your only tool is a hammer then all of your problems will tend to look like nails"

Learn a least a few languages.

Also, your choice depends on your purpose.

Hobby? Job in Windows world? Linux/UNIX family?

Type of applications: business versus scientific; hardware drivers or applications?

Desktop applications or web applications?

I have several suggestions for you.

(a) definitely learn some J (free from jsoftware.com; successor to APL; both J and APL are creations of Ken Iverson, Turing winner ... Turing award is like Nobel prize in computing).

(b) if you are in Windows world, start with c# because so much in .NET runs on c#. If you can, get a copy of Tom Archer's "Inside c#" from Microsoft Press. You can get a free c# development system by downloading Microsoft's express version.

(c) learn to use TDD/BDD ... regardless of language, first you write a small test called a unit test; next you write the production code to pass the unit test; one small step at a time ... it's not just the language that you use, it's also the methodology.

(d) learn some assembler language ... assembler is low level, almost machine language, it will give you a good understanding of what is going on behind the scenes.

(e) outside of the Windows world, I'd recommend c++.

There is no best language.

If it were only about language, programming would be easier.

Not only do you want to learn algorithms which are very specific, you also want to learn patterns which are more general and can help you in selecting the approach to solving a given problem.

One thing is for certain: you will likely never run out of things to learn if you're going to become a programmer.

Solution 10 - Algorithm

You may appreciate a language with algebraic datatypes and pattern matching such as Standard ML, OCaml, F# or Haskell. For example, here is a function to rebalance a red-black binary search tree written in OCaml/F#:

let balance = function
  | R(R(a, x, b), y, c), z, d | R(a, x, R(b, y, c)), z, d
  | a, x, R(R(b, y, c), z, d) | a, x, R(b, y, R(c, z, d)) ->
      R(B(a, x, b), y, B(c, z, d))
  | a, x, b -> B(a, x, b)

Solution 11 - Algorithm

I think Lisp is worth looking into.

My first university programming course was in Lisp. Before that I had been writing programs in several languages for 10 years. I thought that the first programming course would be boring, but I was wrong.

Lisp is a very interesting language because it has a very simple syntax. Focus shifts from syntax to functionality. The functional programming style is also an extremely valuable thing to learn. After my Lisp course I found myself writing programs in C++ in a completely new, better way, thanks to the new concepts Lisp had taught me.

Lisp also uses the same representation for code and data, which opens up for interesting algorithm design with code generated on the fly and then executed.

Solution 12 - Algorithm

I may be wrong, but aren't data structures and algorithms independent of the programming languages?

In the end, data structures are just a way of organizing data; any high level language will support that. Sure, certain languages will have mechanisms implementing basic data structures (such as Collections Framework in Java or C++ STL), but it does not stop you from programming data structure in the programming language of your choice. Moreover, algorithms are written in pseudocode, making them language independent.

I realize it's not really answering your question, but I'm having trouble grasping what you are looking for; learning data structures/algorithms or learning a new language.

Solution 13 - Algorithm

Any language except the fugly C++ should do fine.

Solution 14 - Algorithm

I prefer C++ :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbguizView Question on Stackoverflow
Solution 1 - AlgorithmcletusView Answer on Stackoverflow
Solution 2 - AlgorithmcodaddictView Answer on Stackoverflow
Solution 3 - AlgorithmTaylor LeeseView Answer on Stackoverflow
Solution 4 - AlgorithmMantas VidutisView Answer on Stackoverflow
Solution 5 - AlgorithmErnelliView Answer on Stackoverflow
Solution 6 - AlgorithmRob LachlanView Answer on Stackoverflow
Solution 7 - AlgorithmWildcatView Answer on Stackoverflow
Solution 8 - AlgorithmDaniel RoseView Answer on Stackoverflow
Solution 9 - AlgorithmgerryLowryView Answer on Stackoverflow
Solution 10 - AlgorithmJ DView Answer on Stackoverflow
Solution 11 - AlgorithmAnders AbelView Answer on Stackoverflow
Solution 12 - AlgorithmPranView Answer on Stackoverflow
Solution 13 - AlgorithmJeremy JoseView Answer on Stackoverflow
Solution 14 - AlgorithmPrasoon SauravView Answer on Stackoverflow