You are here:

C/sorting

Advertisement


Question
QUESTION: is only if statement require in recursion?

ANSWER: Recursion occurs when you call the same function repeatedly with slightly differing input values, expecting to reach an end point that will cascade results back to every previous calling of the function.  An easy to understand example of recursion is the factorial operation of numbers.

The factorial of an integer (non-negative) is defined as the product of every positive integer less than or equal to the integer.  The symbol for a factorial is the exclamation point (!).  So, the factorial of A (represented as A!) would be the product of every integer from 1 to A.  5!, for example, is:

   1 * 2 * 3 * 4 * 5 = 120

The algorithm for this is really quite simple and makes for an easy recursive function:

   int factorial(int num)
   {
       if (num == 1)
         return num;
       else
         return factorial(num - 1) * num;
   }

You can see here how the function calls itself, decrementing num by one.  In this manner, there is a change in the input value and a defined end point (num == 1).  Once the end point is reached, the return values cascade back until the initial caller of the recursive function is reached.

If you have any further questions, or if I have raised any new questions for you, please do not hesitate to ask.

---------- FOLLOW-UP ----------

QUESTION: why bubble sort called?also name like insertion sort ,selection sort.

Answer
The sorting algorithm names are (theoretically) descriptive of how they work.

Bubble sort is so called because the items "bubble" to the top in sorted order.

Insertion sort works based on the premise that it's building a new array, inserting elements one at a time.

Selection sort is so called because it continuously selects the smallest unsorted value and moves it to its proper place.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.