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

Discussion Overview

The discussion revolves around writing recursive algorithms in pseudocode, focusing on defining functions, their structures, and specific examples such as calculating factorials and performing binary searches. Participants explore various aspects of pseudocode, including types, syntax, and readability.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants inquire about the structure of recursive functions and the importance of a base case to prevent infinite recursion.
  • There is a discussion about whether to define function return types in pseudocode, with some suggesting that types can be omitted.
  • Participants present a binary search algorithm and question the meaning of "Index" and "Type" within the context of the pseudocode.
  • Some participants express uncertainty about the appropriate representation of the floor function in pseudocode.
  • There is a debate about the readability of different logical operators in pseudocode, with varying preferences expressed by participants.

Areas of Agreement / Disagreement

Participants have differing views on the necessity of types in pseudocode, the representation of the floor function, and the readability of logical operators. No consensus is reached on these issues.

Contextual Notes

There are unresolved questions regarding the definitions of "Index" and "Type" in the context of pseudocode, as well as the syntactical correctness of the presented algorithms.

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
5K
  • · Replies 15 ·
Replies
15
Views
6K