Pseudocode Examples
1. Binary search Pseudocode:
Binary search is a searching algorithm that works only for sorted search space. It repeatedly divides the search space into half by using the fact that the search space is sorted and checking if the desired search result will be found in the left or right half.
Example: Given a sorted array Arr[] and a value X, The task is to find the index at which X is present in Arr[].
Below is the pseudocode for Binary search.
BinarySearch(ARR, X, LOW, HIGH)
repeat till LOW = HIGH
MID = (LOW + HIGH)/2
if (X == ARR[mid])
return MID
else if (x > ARR[MID])
LOW = MID + 1
else
HIGH = MID – 1
2. Quick sort Pseudocode:
QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and partitions the given array around the picked pivot.
Say last element of array is picked as pivot then all elements smaller than pivot element are shifted on the left side of pivot and elements greater than pivot are shifted towards the right of pivot by swapping, the same algorithm is repeatedly followed for the left and right side of pivot until the whole array is sorted.
Below is the pseudocode for Quick sort
QUICKSORT(Arr[], LOW, HIGH) {
if (LOW < HIGH) {
PIVOT = PARTITION(Arr, LOW, HIGH);
QUICKSORT(ARR, LOW, PIVOT – 1);
QUICKSORT(ARR, PIVOT + 1, HIGH);
}
}
Here, LOW is the starting index and HIGH is the ending index.
What is PseudoCode: A Complete Tutorial
A Pseudocode is defined as a step-by-step description of an algorithm. Pseudocode does not use any programming language in its representation instead it uses the simple English language text as it is intended for human understanding rather than machine reading.
Pseudocode is the intermediate state between an idea and its implementation(code) in a high-level language.