Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm studying fundamental algorithms (quicksort, mergesort, binary trees, etc..) and I'm going to re-code every algorithm by scratch to learn how they work. Should I code them in C or C++? I think with C++ I should use STL, in C everything should probably be longer

Is there a reason why I should pick one over another?

share|improve this question

closed as off topic by Robert Harvey, ChrisF Aug 29 '12 at 16:43

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

If you are implementing data structures and algorithms from scratch, you probably won't use the STL. The primary reason to use STL is so that you don't have to reinvent the wheel. I assume that you are refering to learning how to use templates in general, rather than to using the STL specifically. –  Code-Guru Aug 29 '12 at 16:26
I've been following this site for a while and I think there's someone who closes too many questions as "off-topic" –  John Smith Aug 29 '12 at 17:11
Hmm...this doesn't seem to be off topic at all to me. Have you raised the issue in this site's meta forum? –  Code-Guru Aug 29 '12 at 17:35
Check out this thread: meta.programmers.stackexchange.com/questions/3953/…. In particular, Robert Harvey's answer has some suggestions about how to ask a question which is more in-line with what this community expects. –  Code-Guru Aug 29 '12 at 17:43

5 Answers 5

up vote 3 down vote accepted

I've found more references that teach this stuff in C as opposed to C++, and the college course I took on Data Structures and Algorithms was taught in C, so I would recommend C on that basis.

The purpose is to learn these data structures and algorithms correctly. Hell, you could go learn the principles in Java or Python if you really wanted to... the choice of language isn't a critical decision since you're learning concepts at this stage rather than being tied to a specific implementation.

I would focus on finding a good resource that teaches you the basics in a language with which you are comfortable.

share|improve this answer
I'd argue that it actually does make a little less sense to use Python or Java. I remember learning data structures with Java, and it was extremely confusing because I still didn't understand what pointers were and how references worked. Once I learned C++ and got exposure to C, it made a lot more sense. –  KChaloux Aug 29 '12 at 17:49

There is little difference between using C and using C++ in this context. The reason is that the differences between the two languages are not going to be very visible - a trivial implementation of an algorithm very rarely takes advantage of the features found in C++, rather, it works with the very basic foundations which are found in both C and C++.

Personally I would find using C much simpler because it simply works at lower level of abstraction. This means that the fundamental idea and inner working of the algorithm become more clear. A good example of this would be whether to use C-style array or STL container like std::vector. What benefits would std::vector give you in terms of learning about implementing an algorithm? Frankly, I can't name any.

This is not to say that in general you should implement algorithms in C or use some kind of "C-style". This is to say that in this case, when you want to implement algorithms to understand how they work, I argue that the best way to approach this is to use as basic and fundamental facilities possible - exactly those which C offers and perhaps even avoid the high-level concepts introduced by C++.

share|improve this answer

I think C++ is probably the way to go. I suggest starting simple and implement data structures and algorithms using simple ints as your data. Once you get that working, then using templates seems like the next logical step to me.

share|improve this answer
Going from hard-coded types to templates is an interesting exercise if you plan on doing it properly, but it has little to do with learning algorithms and data structures. –  delnan Aug 29 '12 at 16:21
@delnan Good point. I guess my answer is also about learning how to use templates, which was only implied in the OP with the reference to the "STL". –  Code-Guru Aug 29 '12 at 16:25
Thanks for posting your first answer on Stack Exchange Programmers. Please read programmers.stackexchange.com/questions/how-to-answer carefully to get suggestions on how to craft great answers that will win you up votes and increased reputation on this site. –  DeveloperDon Sep 24 '12 at 23:19

The problem with both c and c++ is that one has to dump all data structures by hand, and this is boring. Compiling is also frustrating. It is much better to use some language with repl and that has good support for printing datastructures (haskell, scheme, python. Haskell is better for some algorithms, but in-place sorting in it sucks and is quite long).

If you have to choose between c and c++, then c++ is better - it allows to avoid some boilerplate and better with abstractions.

share|improve this answer

Your question is confusing.

You say you plan to re-implement fundamental algorithms "from scratch". Then go on to say that if you go the C++ route, you "should" use the STL. Those are contradicting statements.

Assuming that you are going to start from scratch, it really doesn't matter what language you use since you wouldn't be using anything but the built in facilities of C and C++, and the superfluous facilities of the latter aren't necessary for your task.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.