Learning to Write a Recursive Algorithm in Pseudocode

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary
SUMMARY

This discussion focuses on writing recursive algorithms in pseudocode, specifically using examples like the factorial function and binary search. Key elements of a recursive function include a base case to terminate recursion and a recursive call to continue the process. The conversation highlights common pitfalls, such as improperly defined base conditions leading to infinite recursion. Additionally, it clarifies the use of unspecified types like "Index" and "Type" in pseudocode, emphasizing that pseudocode is not bound by the syntax of any specific programming language.

PREREQUISITES
  • Understanding of recursive algorithms
  • Familiarity with pseudocode conventions
  • Basic knowledge of data types and their usage
  • Concept of base cases in recursion
NEXT STEPS
  • Study the implementation of recursive algorithms in Python
  • Learn about the differences between pseudocode and actual programming languages
  • Explore common recursion pitfalls and how to avoid them
  • Research the use of the floor function in algorithms
USEFUL FOR

Students learning algorithms, software developers interested in algorithm design, and educators teaching programming concepts.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
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
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:
 
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)
 
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)
 
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)
 
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)
 
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)
 
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)
 
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)
 

Similar threads

Replies
86
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 43 ·
2
Replies
43
Views
7K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 15 ·
Replies
15
Views
6K