0

What Is Pseudocode And How To Write It?

 2 years ago
source link: https://www.journaldev.com/55797/pseudocode
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

In this article, we will discuss pseudocode and learn how to write it. We will also discuss why is this term so popular.

What is Pseudocode?

If you have ever worked with algorithms or data structures, you probably might have used pseudocode in some way or the other.

Pseudocode is the skeleton of a program design that contains all the logic which will be there in the program. It is an artificial and informal language that helps programmers to develop various algorithms. You can compare it with the human body and skeleton. Pseudocode to the final program is the same as the skeleton is to the human body.

It is a healthy practice to develop the pseudocode of an algorithm before actually coding it. Writing pseudocode not only reduce the chances of errors but also reduced the time needed to develop the program.

Tip: Investing your time writing pseudocode before directly jumping to code is probably the best decision you could have made while programming.

This is because writing pseudocode develops a clear picture of the algorithm into the mind. The mind truly knows the logic of the algorithm and thus really speeds up the development. Pseudocode doesn’t depend on the programming language, since it is the structure of the logic of the program. Hence, it is generic and can be implemented for any programming language with ease.

How to write Pseudocodes?

Okay so let’s now learn how to write the pseudocode while programming. However, there exists a very easy-to-learn rule which should be taken care of while writing it.

Rule for writing a Pseudocode

  • All statements showing “dependency” are to be indented. These include while, do, for, if, switch.

That’s it, and we are ready to rock. So let’s now jump to writing some actual algorithm by writing the pseudocode for it.

Writing Pseudocode for Binary Search algorithm

Make sure that while writing the pseudocode, you do exactly what is being done by the algorithm. Basically, make sure to implement the exact same logic and keep the variable constraints in mind.

Constraints: The array must be sorted

Step 1: Naming the algorithm and identifying the inputs.

//binary_search is the perfect name for this algorithm
//inputs: sorted_array, starting_point, ending_point, key
//you need not care about the data types of the inputs and other variables,
//as these can easily be identified during actual coding because of the
//simplicity of the algorithm
binary_search(A, start, end, key)

Step 2: Binary Search is a Divide and Conquer Algorithm. Hence we need to create a base case for the recursive calls.

//base case
if (start > end) --> return -1;

Step 3: So now are done with the base case, hence we move to the actual logic

//find the mid point
mid = (start + end)/2;
//compare the element present at mid with the key
//here there are 3 cases possible
//case 1: mid_element == key
if (arr[mid] == key)
//notice the indentation when moving inside "if" block
//element is found, simply return the index
return mid;
//case 2: mid_element < key
//since the array is sorted, element can not be in the left half of the array
else if (arr[mid] < key)
return binary_serch(mid + 1, end, key);
//case 3: mid_element > key
//element can not be in the right half of the array
else
return binary_search(start, mid - 1, key);

So this will be the logic of the program, let’s take a look at the final form of the pseudocode.

Binary Search PseudocodeBinary Search Pseudocode

Final Pseudocode

//code for binary search function
int binary_search(vector <int> arr, int start, int end, int key)
{
//base case
if(start > end)
return -1;
int mid = (start + end)/2;
if(arr[mid] == key)
index = mid;
else if(arr[mid] > key)
return binary_search(arr, start, mid - 1, key);
else
return binary_search(arr, mid + 1, end, key);
}
Binary Search FunctionBinary Search Function

Conclusion

Let’s conclude this article by discussing what we discussed. At first, we learned about pseudocode and its importance. Later we discussed the rule to write pseudocodes. And finally, we wrote the pseudocode for the binary search algorithm and developed the function code using this pseudocode.

References

To learn more about Pseudocode, you can refer to the following websites:

https://www.unf.edu/~broggio/cop2221/2221pseu.htm

https://en.wikipedia.org/wiki/Pseudocode

https://www.geeksforgeeks.org/how-to-write-a-pseudo-code/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK