Learning to Write a Recursive Algorithm in Pseudocode

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Algorithm
In summary, the conversation covers the topics of writing a recursive algorithm as pseudocode, defining the function structure, avoiding pitfalls, writing algorithms in pseudo code, using the floor function, and using logical operators. Pseudo code is a free style language used to convey the idea of an algorithm and does not have to be syntactically correct according to a specific programming language. Both $\lfloor x \rfloor$ and $\text{ floor }(x)$ can be used for the floor function, and the use of the and operator in pseudo code is flexible.
  • #1
evinda
Gold Member
MHB
3,836
0
Hi! (Wave)

I want to write a recursive algorithm as a pseudocode.

How can I define the function? Which has to be its structure?
 
Technology news on Phys.org
  • #2
evinda said:
Hi! (Wave)

I want to write a recursive algorithm as a pseudocode.

How can I define the function? Which has to be its structure?

Hey! (Smile)

The standard example is:

Code:
//INPUT: n is an Integer such that n >= 0
int fact(int n)
{
   if (n == 0)
      return 1;
   else
      return n * fact(n - 1);
}

The structure is:
  1. a final condition that makes the recursion stop,
  2. a recursive call.

The standard pitfall is that the final condition is not properly set, causing an infinite recursion. :eek:
 
  • #3
I like Serena said:
The standard example is:

Code:
//INPUT: n is an Integer such that n >= 0
int fact(int n)
{
   if (n == 0)
      return 1;
   else
      return n * fact(n - 1);
}

The structure is:
  1. a final condition that makes the recursion stop,
  2. a recursive call.

The standard pitfall is that the final condition is not properly set, causing an infinite recursion. :eek:

I am asked to find the maximum of an array.

Do I have to define the function as int or something else? (Thinking)
 
  • #4
evinda said:
I am asked to find the maximum of an array.

Do I have to define the function as int or something else? (Thinking)

You can leave out the types, since they won't be relevant for the algorithm. (Mmm)
 
  • #5
I like Serena said:
You can leave out the types, since they won't be relevant for the algorithm. (Mmm)

In my notes, there is this algorithm:

Code:
Index BinarySearch(Type A[0...N-1], Type value, Index low, Index high){
  if (high<low) return -1
  mid=low+(high-low)/2;
  if (A[mid]>value) return  BinarySearch(A, value, low,mid-1);
  else if (A[mid]<value) return  BinarySearch(A, value, mid+1,high);
  else return mid
}

What does Index represent? (Thinking)
 
  • #6
evinda said:
In my notes, there is this algorithm:

Code:
Index BinarySearch(Type A[0...N-1], Type value, Index low, Index high){
  if (high<low) return -1
  mid=low+(high-low)/2;
  if (A[mid]>value) return  BinarySearch(A, value, low,mid-1);
  else if (A[mid]<value) return  BinarySearch(A, value, mid+1,high);
  else return mid
}

What does Index represent? (Thinking)

"Index" is an unspecified type that can be used as an index.
For practical purposes you can assume its an [m]int[/m]. (Nerd)

Btw, it looks more like real C code than pseudo code (except for a couple of syntax errors). (Wait)
 
  • #7
I like Serena said:
"Index" is an unspecified type that can be used as an index.
For practical purposes you can assume its an [m]int[/m]. (Nerd)

What is the difference between Index and Type? What does the latter represent? (Worried)

I like Serena said:
Btw, it looks more like real C code than pseudo code (except for a couple of syntax errors). (Wait)

How could it be otherwise? (Thinking)
 
  • #8
Also, at the algorithm, I want to use the floor function.

Can I write it like that: $\lfloor x \rfloor$ or do I have to write it like that: $\text{ floor }(x)$ ? (Thinking)
 
  • #9
evinda said:
What is the difference between Index and Type? What does the latter represent? (Worried)

"Type" is an unspecified type for values that are in the array.
It might for instance be a [m]double[/m]. (Smile)
How could it be otherwise? (Thinking)

Well, the following is pseudo code for a binary search algorithm.
It is not C, nor any other actual programming language - it's pseudo code. (Angel)
Code:
function binarySearch(a, value, left, right)
    if right < left
        return not found
    mid := floor((right-left)/2)+left
    if a[mid] = value
        return mid
    if value < a[mid]
        return binarySearch(a, value, left, mid-1)
    else
        return binarySearch(a, value, mid+1, right)
evinda said:
Also, at the algorithm, I want to use the floor function.

Can I write it like that: $\lfloor x \rfloor$ or do I have to write it like that: $\text{ floor }(x)$ ? (Thinking)

Both are fine.

Pseudo code is only intended to get the idea of an algorithm across.
It does not have to be syntactically correct according to some formal programming language.
It's a sort of free style language. (Party)
 
  • #10
I like Serena said:
"Type" is an unspecified type for values that are in the array.
It might for instance be a [m]double[/m]. (Smile)

Well, the following is pseudo code for a binary search algorithm.
It is not C, nor any other actual programming language - it's pseudo code. (Angel)
Code:
function binarySearch(a, value, left, right)
    if right < left
        return not found
    mid := floor((right-left)/2)+left
    if a[mid] = value
        return mid
    if value < a[mid]
        return binarySearch(a, value, left, mid-1)
    else
        return binarySearch(a, value, mid+1, right)

Both are fine.

Pseudo code is only intended to get the idea of an algorithm across.
It does not have to be syntactically correct according to some formal programming language.
It's a sort of free style language. (Party)

I understand! Could I also use the and operator like that:

Code:
if (condition1 && condition2)

or is it too C-like? (Wasntme)
 
  • #11
evinda said:
I understand! Could I also use the and operator like that:

Code:
if (condition1 && condition2)

or is it too C-like? (Wasntme)

Up to you! (Nod)

Alternatively, you could write:
Code:
if condition1 and condition2

Which do you think is easier to read? (Wondering)
 
  • #12
I like Serena said:
Up to you! (Nod)

Alternatively, you could write:
Code:
if condition1 and condition2

Which do you think is easier to read? (Wondering)

I think the second one is easier to read.. Do you agree? (Blush)
 
  • #13
evinda said:
I think the second one is easier to read.. Do you agree? (Blush)

Yes. (Angel)
 

Related to Learning to Write a Recursive Algorithm in Pseudocode

1. What is a recursive algorithm?

A recursive algorithm is an algorithm that solves a problem by breaking it down into smaller subproblems of the same type, and then solving each subproblem until the original problem is solved.

2. What is pseudocode?

Pseudocode is a simplified, high-level description of a computer program or algorithm, written in plain English with some programming language-like syntax. It is used to plan and design algorithms before writing actual code.

3. Why is it important to learn how to write recursive algorithms in pseudocode?

Learning how to write recursive algorithms in pseudocode helps to develop critical thinking and problem-solving skills. It also allows you to plan and design efficient algorithms before actually implementing them in a programming language.

4. What are the key components of a recursive algorithm?

The key components of a recursive algorithm are the base case, which is the simplest version of the problem to solve, and the recursive case, which is when the algorithm calls itself to solve a smaller version of the problem. Other important components include the parameters, input, and output of the algorithm.

5. What are some common mistakes to avoid when writing a recursive algorithm in pseudocode?

Some common mistakes to avoid when writing a recursive algorithm in pseudocode include forgetting to include a base case, not properly defining the parameters and input, and not considering the efficiency of the algorithm. It is also important to properly test and debug the algorithm to ensure it is working correctly.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
3K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
4
Replies
107
Views
5K
Back
Top