Thread: Recursion question to practise

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    18

    Recursion question to practise

    Hi is there any books or websites that I can refer to for more practice on recursion for c programming? Seems like those I find is the standard gcd, tower of hanoi and etc. Want to get more practice!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Binary searches?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Las Vegas, NV
    Posts
    10
    Try to implement quicksort.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by RedRacer View Post
    Try to implement quicksort.
    Good idea, but I'd try mergesort first, 1) because it is easier, 2) because it is useful to understand how quick- and merge- work comparatively. I think a lot of people who skip straight to quicksort buy into the hype about it, and fail to understand that mergesort is often a stronger general purpose sort.

    They are both good for learning recursion.
    Last edited by MK27; 10-24-2011 at 09:32 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Good idea, but I'd try mergesort first, 1) because it is easier, 2) because it is useful to understand how quick- and merge- work comparatively. I think a lot of people who skip straight to quicksort buy into the hype about it, and fail to understand that mergesort is often a stronger general purpose sort.
    I support quicksort over mergesort because I've met Hoare, but I've never met von Neumann!

    Nah, in reality I use whatever sort is readily available in the standard and other libraries, and pick my algorithms more carefully when these don't suffice.

    Anyway, a somewhat more ambitious project would be to implement a recursive descent parser, because more than just practicing recursion by translating some recursive algorithm into code, you would need to learn about grammars.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2011
    Location
    Las Vegas, NV
    Posts
    10
    Quote Originally Posted by MK27 View Post
    Good idea, but I'd try mergesort first, 1) because it is easier, 2) because it is useful to understand how quick- and merge- work comparatively. I think a lot of people who skip straight to quicksort buy into the hype about it, and fail to understand that mergesort is often a stronger general purpose sort.

    They are both good for learning recursion.
    I suggested quicksort because it's a tougher than GCD or Towers or Hanoi, and -- more importantly to me -- it requires a good understanding of arrays in C, especially when performing the in-place version of the algo. However, merge sort might well be a good way to ease into recursive sorting algorithms.

    Fooling around with binary trees is also a good way to get a "feel" for recursion...maybe even be the best way, since one can visualize pretty easily, say, a preorder traversal algorithm in action. Plus, working with trees in C provides some extra practice with pointers and structs.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make mazes. Then solve mazes. BFS, DFS. Then try solving them without recursion.


    Quzah.
    Last edited by quzah; 10-24-2011 at 06:35 PM.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well this will help you understand one important aspect of recursion, namely the simple end condition which stops the recursion being infinite
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I never get tired of that one.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Both merge and quicksort will be good for looking at recursion; however, I have the following suggestion:

    When writing quicksort, sort with an array; quicksort requires (for best performance) a way to randomly access elements.
    When writing merge sort, sort with a linked list; the merge step with a linked list requires no extra storage or hoops to jump through.

    You can use quicksort on a linked list and merge sort on an array, but the extra work you'll have to do for them won't help you with learning recursion.

    You might also benefit from looking at a functional language like Haskell or OCaml, where recursion is more a fundamental part of programming than it is in C. Not that you should necessarily switch to such a language, but it could help you get a better understanding of recursion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best practise for passing arrays as parameters
    By ardavirus in forum C Programming
    Replies: 10
    Last Post: 06-18-2011, 04:50 PM
  2. How robust is malloc() in practise?
    By Richardcavell in forum C Programming
    Replies: 9
    Last Post: 04-10-2011, 12:19 PM
  3. Where to declare variables - Best Practise
    By darren78 in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2009, 03:18 AM
  4. Putting Programming Knowledge To Practise
    By DanMarionette in forum C++ Programming
    Replies: 5
    Last Post: 07-28-2007, 05:40 AM
  5. Question about recursion.
    By cdalten in forum C Programming
    Replies: 5
    Last Post: 07-23-2006, 03:06 AM