Pseudocode Examples – Programming, Pseudocode Example, C# Programming Example
General Pseudocode Examples

Pseudocode Examples

What is Pseudocode

Pseudocode is a method of describing computer algorithms using a combination of natural language and programming language constructs. It is not a formal programming language, but rather a way of expressing algorithms in a way that is more easily understood by humans. Pseudocode is often used as an intermediate step in the development of a program, before the final code is written in a specific programming language.

The main advantage of pseudocode is that it allows the programmer to focus on the logic of the algorithm, rather than the details of a particular programming language. This makes it easier to develop and communicate algorithms, and to understand and modify existing algorithms. Pseudocode is also a good way to test and debug an algorithm, as it can be easily translated into a programming language and run on a computer to see if it produces the desired results.

There is no standard syntax for pseudocode, as it is not a formal programming language. However, there are some general guidelines that are followed by most pseudocode writers. These include using simple, clear language and avoiding the use of specific programming language constructs whenever possible. Instead, pseudocode should use a combination of natural language and high-level programming concepts to describe the algorithm.

For example, the following is a simple algorithm written in pseudocode to add two numbers together:

In this example, the pseudocode uses a combination of natural language (PROCEDURE, SET, RETURN) and programming concepts (variables, assignment, function calls) to describe the algorithm. The algorithm itself is very simple and could be easily implemented in any programming language.

Pseudocode can be a very useful tool for communicating and developing algorithms, especially for those who are new to programming or unfamiliar with a particular programming language. By using pseudocode, programmers can focus on the logic of their algorithms without worrying about the specific syntax and semantics of a particular programming language.

Pseudocode Examples ( Algorithms Examples in Pseudocode )

There are 18 pseudocode tutorial in this post. The Pseudocode examples  go from beginner to advanced. You will find a lot of for loop, if else and basics examples. Pseudocode and flowchart examples are in following the post.

Add Two Numbers.(Simple Pseudocode  Example)

This pseudocode describes a program that takes two numbers as input from the user, adds them together and outputs the sum.

  1. The variables “s1”, “s2”, and “sum” are declared.
  2. The program outputs the message “Input number1:” and waits for the user to input a value, which is stored in “s1”.
  3. The program outputs the message “Input number2:” and waits for the user to input a value, which is stored in “s2”.
  4. The value of “sum” is calculated as the sum of “s1” and “s2”.
  5. The value of “sum” is outputted as the result of the program.

Calculate Area and Perimeter of Rectangle (Simple Pseudocode  Example)

This pseudocode describes a program that calculates the area and perimeter of a rectangle given its length and width.

  1. The variables “b1”, “b2”, “area”, and “perimeter” are declared.
  2. The program prompts the user to input the length of the rectangle and stores the value in “b1”.
  3. The program prompts the user to input the width of the rectangle and stores the value in “b2”.
  4. The area of the rectangle is calculated by multiplying “b1” and “b2” and stored in the “area” variable.
  5. The perimeter of the rectangle is calculated by multiplying 2 by the sum of “b1” and “b2” and stored in the “perimeter” variable.
  6. The program outputs the value stored in the “area” variable.
  7. The program outputs the value stored in the “perimeter” variable.

Find Area and Perimeter of a Square (Simple Pseudocode Example)

This pseudocode describes a program that calculates the area and perimeter of a square given its length of one side.

  1. The variables “len”, “area”, and “perimeter” are declared.
  2. The program prompts the user to input the length of one side of the square and stores the value in “len”.
  3. The area of the square is calculated by squaring the length of one side and storing the result in the “area” variable.
  4. The perimeter of the square is calculated by multiplying the length of one side by 4 and stored in the “perimeter” variable.
  5. The program outputs the value stored in the “area” variable.
  6. The program outputs the value stored in the “perimeter” variable.

Find Area Of Circle using Radius (Simple Pseudocode  Example)

This pseudocode describes a program that calculates the area of a circle given its radius.

  1. The variables “r” and “area” are declared.
  2. The program prompts the user to input the radius of the circle and stores the value in “r”.
  3. The area of the circle is calculated by multiplying pi (3.14) by the square of the radius and storing the result in the “area” variable.
  4. The program outputs the value stored in the “area” variable.

Find Perimeter Of Circle using Radius (Simple Pseudocode  Example)

This pseudocode describes a program that calculates the circumference of a circle given its radius.

  1. The variables “r” and “perimeter” are declared.
  2. The program prompts the user to input the radius of the circle and stores the value in “r”.
  3. The circumference of the circle is calculated by multiplying 2 by pi (3.14) and the radius and storing the result in the “perimeter” variable.
  4. The program outputs the value stored in the “perimeter” variable.

Calculate sales taxes (Simple Pseudocode  Example)

This pseudocode describes a program that calculates the total cost of a product, including tax, based on the price of the product and the tax rate.

  1. The variables “price”, “tax”, “taxRate”, and “total” are declared.
  2. The program outputs the message “Enter Product Price” and prompts the user to input the price of the product.
  3. The program outputs the message “Enter tax rate amoung 1 and 100” and prompts the user to input the tax rate as a percentage.
  4. The tax amount is calculated by multiplying the price of the product by the tax rate divided by 100 and stored in the “tax” variable.
  5. The total cost of the product, including tax, is calculated by adding the price of the product and the tax amount and stored in the “total” variable.
  6. The program outputs the message “Product tax=” followed by the value stored in the “tax” variable.
  7. The program outputs the message “Product total price =” followed by the value stored in the “total” variable.

Solve Quadratic Equation (Pseudocode If Else Example)

This pseudocode describes a program that solves the quadratic equation ax^2 + bx + c = 0 and outputs its real roots.

  1. The variables “a”, “b”, “c”, “d”, “x1”, and “x2” are declared.
  2. The program prompts the user to input the values of the coefficients “a”, “b”, and “c”.
  3. The value of “d”, the discriminant, is calculated as the square of “b” minus 4 times “a” times “c”.
  4. If “d” is greater than or equal to 0, then the program calculates the two real roots of the equation using the formula (-b±√d) / 2a and stores the values in “x1” and “x2”.
  5. If “d” is equal to 0, then the program calculates the one real root of the equation using the formula -b / 2a and stores the value in both “x1” and “x2”.
  6. If “d” is less than 0, then the program outputs the message “There is no real root”.
  7. The program outputs the message “ROOT 1:” followed by the value stored in “x1” and the message “ROOT 2:” followed by the value stored in “x2”.

Issue for driver licence (Pseudocode If Else Example)

This pseudocode describes a program that determines if a person is eligible for a driving license based on their age.

  1. The variable “age” is declared.
  2. The program prompts the user to enter their age and stores the value in “age”.
  3. The program checks if “age” is greater than or equal to 16.
  4. If “age” is greater than or equal to 16, the program outputs the message “You can take driving licence”.
  5. If “age” is less than 16, the program outputs the message “You can’t take driving licence”.

Check a Number is Positive or Negative (Pseudocode If Else Example)

This pseudocode describes a program that determines if a number entered by the user is positive, negative, or zero.

  1. The variable “num” is declared.
  2. The program prompts the user to enter a number and stores the value in “num”.
  3. The program checks if “num” is greater than zero.
  4. If “num” is greater than zero, the program outputs the message “Entered number is positive”.
  5. If “num” is less than zero, the program outputs the message “Entered number is negative”.
  6. If “num” is equal to zero, the program outputs the message “Entered number is zero”.

Find the biggest of three (3) Numbers (Pseudocode If Else Example)

This pseudocode describes a program that determines the highest of three numbers entered by the user.

  1. The variables “num1”, “num2”, and “num3” are declared.
  2. The program prompts the user to enter three numbers and stores the values in “num1”, “num2”, and “num3”.
  3. The program checks if “num1” is greater than both “num2” and “num3”.
  4. If “num1” is greater than both “num2” and “num3”, the program outputs the message “num1 is higher”.
  5. If “num1” is not greater than both “num2” and “num3”, the program checks if “num2” is greater than “num3”.
  6. If “num2” is greater than “num3”, the program outputs the message “num2 is higher”.
  7. If “num2” is not greater than “num3”, the program outputs the message “num3 is higher”.

Print Numbers from 1 to 100. (Pseudocode For Loop Example)

This pseudocode defines a loop that outputs the numbers from 1 to 100, incrementing the counter by 1 each time. The loop starts with the declaration of a variable “counter” as the loop index. The loop structure is then defined using the “FOR” keyword followed by the starting value of “counter” (1), the final value of “counter” (100), and the step size (1). The loop continues to execute as long as “counter” is less than or equal to 100. In each iteration, the value of “counter” is outputted using the “OUTPUT” keyword. The loop ends with the “ENDFOR” keyword.

Find Sum of Natural Numbers (1 to 100). (Pseudocode For Loop Example)

This pseudocode defines a simple program to calculate the sum of numbers from 1 to 100. The program uses a “FOR” loop to iterate through each number from 1 to 100 and adds each number to a running sum. The final result, the sum of all the numbers from 1 to 100, is output at the end of the program.

Read 50 numbers and find their sum and average. (Pseudocode For Loop Example)

This pseudocode initializes a variable called counter and sum with a value of 0. Then it starts a loop, FOR loop that runs 50 times with a step of counter, meaning it increases the counter by itself at each iteration. In each iteration, the code prompts the user to enter a number, and stores it in the variable num. The num is then added to the sum and the loop continues until it runs 50 times. Finally, the code outputs the final value of sum.

Read 10 numbers and find sum of even numbers. (Pseudocode For Loop Example)

This pseudocode calculates the sum of only even numbers from 10 numbers entered by the user. It starts with declaring two variables: counter and sum, and sets the value of sum to 0. The code then enters a for loop that runs 10 times (i.e., from 1 to 10). In each iteration of the loop, the code prompts the user to “Enter a Number”. The user input is stored in the variable num. Then, the code checks whether num is an even number by checking if num divided by 2 has a remainder of 0. If the result of this check is true (i.e., num is even), the code adds num to sum. After 10 iterations of the loop, the code outputs the final value of sum.

Find the sum of all elements of array. (Pseudocode For Loop Example)

This pseudocode is a program that calculates the sum of the numbers in an array. The array “numbers” has 5 elements, 65, 45, 10, 7, and 125. The program uses a for loop to iterate through the elements of the array, starting from the first element (index 0) to the last element (index n-1) with a step of 1. At each iteration, the current element of the array is added to the “sum” variable. Finally, the program outputs the message “Sum of numbers in the array” followed by the value of the “sum” variable.

Calculate square of a number (Simple Pseudocode  Example)

Alternative 1:

This pseudocode calculates the power of a number by squaring it.

The steps of the pseudocode are:

  1. It declares a NUMBER variable named “num” and “result”.
  2. It prompts the user to “Enter a number for calculate the power of the number”.
  3. The user inputs a value for “num”.
  4. The value of “result” is calculated as the square of “num” by multiplying “num” by itself.
  5. The final result “Power of the Number” is displayed along with its value.

Alternative 2:

This pseudocode calculates the power of a given number by repeatedly adding the number to a running sum.

  • A variable num is defined to capture the input number from the user.
  • A variable result is initialized to zero.
  • The pseudocode then uses a FOR loop to iterate num times.
  • On each iteration of the loop, num is added to result, resulting in the running sum being equal to num multiplied by itself after num iterations.
  • Finally, the power of the number is output as result.

Calculate the Square Root of a Number (Pseudocode For Loop Example)

This pseudocode describes a program to calculate the square root of a number.

The variables root, counter, and num are declared and initialized as follows:

  • root is initialized to 1
  • counter is initialized to 0
  • num is input by the user

The program then enters a while loop where counter is compared to num + 1. In each iteration of the loop:

  • counter is incremented by 1
  • root is updated using the formula root = (num/root + root)/2

Once counter is greater than or equal to num + 1, the while loop terminates and the final value of root is output as the result.

Swap two variables with using a temporary variable (Simple Pseudocode  Example)

This pseudocode implements a simple swapping of two variables, a and b.

  • First, the values of a and b are initialized to 10 and 20, respectively.
  • Then, the original values of a and b are displayed on the screen.
  • After that, a temporary variable c is assigned the value of a.
  • The value of a is then updated to be equal to b and the value of b is updated to be equal to the value of c.
  • Finally, the updated values of a and b are displayed on the screen.

Swap two variables without using a temporary variable (Simple Pseudocode  Example)

This pseudocode block performs the task of swapping two numbers without using a temporary variable. It declares two variables a and b and assigns values 10 and 20 to them respectively. Then it outputs their values. Then it performs swapping of values by using the mathematical expression a = a + b and b = a - b and a = a - b. At the end, it outputs the swapped values of a and b.

Print Numbers from 1 to n. (Pseudocode For Loop Example)

This pseudocode defines a simple program that takes an integer input n and outputs the numbers from 1 to n in sequence, incrementing by 1 each time. The program starts by declaring the variables counter and n. The value of n is then read as input from the user.

Next, a for loop is initiated with the counter starting at 1, ending at n, and incrementing by 1 each iteration. Inside the loop, the value of counter is output on each iteration.

Finally, the program ends.

Calculate the Sum and Average of n Number. (Pseudocode For Loop Example)

This pseudocode initializes a counter, counter, a sum, sum, and an average, avg, as well as an input n. Then, using a FOR loop, it iterates from 1 to n (inclusive) by incrementing the counter by 1 on each iteration. At each iteration, the current value of counter is added to sum. After the loop, it calculates the average of the numbers by dividing sum by n. Finally, it outputs the sum and average of the numbers.

Design the algorithm and flowchart that finds and display the larger of the two numbers given different from each other.

This pseudocode takes two inputs, n1 and n2, and then it compares their values to determine which one is higher. If n1 is greater than n2, it outputs the message n1 is higher. If n2 is greater than n1, it outputs the message n2 is higher. If both n1 and n2 are equal, it outputs the message n1=n2.

Perform the application that calculates the area of the triangle whose height and base length entered by the keyboard.

This pseudocode defines a program that calculates the area of a triangle. It takes two inputs, “b” and “h”, which represent the base and height of the triangle respectively. It then calculates the area using the formula (b * h) / 2, and outputs the result as “Area of Triangle is” followed by the value of the area. The program uses the input values of “b” and “h” to perform the calculation, and the result is stored in the “area” variable.

The voltage (V) between the poles of a conductor is equal to the product of the current (I) passing through the conductor and the resistance (R) present on the conductor. It’s demonstrated by the V = I * R formula.

What is the algorithm of the program that calculates the voltage between the poles of the conductor by using the formula according to the current and resistance values entered by the user.

This pseudocode calculates the voltage between the poles of a conductor using Ohm’s law, which states that the voltage between the poles of a conductor is equal to the product of the current passing through the conductor and the resistance present on the conductor. The code takes two inputs, the current (I) passing through the conductor and the resistance (R) present on the conductor. Then, the voltage (V) is calculated as the product of the current and the resistance (V = I * R). Finally, the calculated voltage is outputted.

What is the algorithm that the number entered by the user from the keyboard fully divided if it is divided into 3 and 5 .

This algorithm takes a number as input from the user and checks if it is fully divisible by both 3 and 5. If the number is fully divisible by both 3 and 5, the algorithm outputs a message saying that the number is divided into 3 and 5. The algorithm uses the modulo operator % to determine if there is a remainder when the number is divided by 3 and 5. If the result of num % 3 and num % 5 is 0, it means that the number is fully divisible by both 3 and 5.

What is the algorithm that finds and shows on the screen that the water is in the form of solid, liquid or gaseous according to the temperature?

This is a simple pseudocode program that checks the temperature (inputted as “temp”) and outputs whether the substance is in a solid, liquid, or gaseous state based on the temperature. If the temperature is less than or equal to 0, the output will be “solid state.” If the temperature is less than 100 but greater than 0, the output will be “liquid state.” If the temperature is greater than or equal to 100, the output will be “gaseous state.”

While calculating the wage of a worker at a factory, these criterias are complied;

If the worker has worked less than 40 hours, the wage is calculated by multiplying the hours worked and the hourly wage, if the employee has worked for 40 hours or more, the hours worked calculated as 2 hours. What is the algorithm that prints the amount to be paid according to this information?

This is a simple pseudocode program that calculates the total fee for working a certain number of hours, with the number of hours and hourly fee as inputs. If the number of hours worked is less than 40, the total fee is calculated by multiplying the number of hours by the hourly fee. If the number of hours worked is equal to or greater than 40, the total fee is calculated by multiplying the number of hours by twice the hourly fee. The final total fee is then outputted.

Program to Find GCD of Two Numbers

This is a pseudocode program that calculates the greatest common divisor (GCD) of two numbers, n1 and n2, which are input by the user. The program starts by initializing variables n1, n2, gcd, and i. Then, it prompts the user to enter the two numbers. Next, it uses a for loop to iterate over the range of numbers from 1 to the minimum of n1 and n2. For each iteration, the program checks if the current number i evenly divides both n1 and n2. If this condition is met, i is stored as the new gcd. This continues until all numbers in the range have been checked. Finally, the program outputs the result by concatenating the values of n1, n2, and gcd in a string and outputting the string.

The LCM of two integers n1 and n2 is the smallest positive integer that is perfectly divisible by both n1 and n2 (without a remainder)

This is a pseudocode program that calculates the least common multiple (LCM) of two numbers, n1 and n2, which are input by the user. The program starts by initializing variables n1, n2, and lcm. Then, it prompts the user to enter the two numbers. Next, it sets the value of lcm to the larger of n1 and n2. The program then enters an infinite loop. Within the loop, it checks if the current value of lcm is evenly divisible by both n1 and n2. If this condition is met, the program outputs the result by concatenating the values of n1, n2, and lcm in a string and outputting the string. Finally, it uses the “break” statement to exit the loop. If the condition is not met, the program increments the value of lcm and continues the loop.

Pass/Fail Grade Checker

The pseudocode above represents a simple program that checks if a student’s grade is greater than or equal to 60 and prints the result. The program has two branches of execution, each represented by an IF statement and an accompanying ELSE clause.

If the student’s grade is greater than or equal to 60, the first branch of the program is executed and the message “passed” is printed. If the student’s grade is less than 60, the second branch of the program is executed and the message “failed” is printed.

This pseudocode provides a clear and concise representation of the program’s logic, making it easy to understand and implement. The use of the IF statement and the ELSE clause makes the program easy to read and understand, and the simple use of the PRINT statement makes the output of the program easy to see.

Calculating Class Average Using While Loop Pseudocode

This pseudocode represents a program that calculates the average of ten grades. The program starts by initializing two variables: total and grade counter. Total is set to zero and grade counter is set to one.

The program then enters a WHILE loop that continues as long as grade counter is less than or equal to ten. The loop contains two statements: an INPUT statement that inputs the next grade, and an ADD statement that adds the input grade to the running total. After each iteration of the loop, the value of grade counter is incremented by one.

Once the loop has completed, the program calculates the class average by dividing the total of all grades by ten. Finally, the program prints the class average by using the PRINT statement.

This pseudocode provides a clear and concise representation of the program’s logic, making it easy to understand and implement. The use of a WHILE loop allows the program to handle an arbitrary number of grades, and the clear and concise statements make the program’s logic easy to follow.

Calculating Average of User-Entered Grades with Sentinel Value Pseudocode

This pseudocode represents a program that calculates the average of a set of grades entered by a user, using a sentinel value to signal the end of the data. The program starts by initializing two variables: total and counter. Total is set to zero and counter is set to zero.

The program then inputs the first grade and enters a WHILE loop that continues as long as the user has not entered the sentinel value. The loop contains three statements: an ADD statement that adds the input grade to the running total, an ADD statement that increments the grade counter by one, and an INPUT statement that inputs the next grade (possibly the sentinel).

Once the loop has completed, the program checks if the counter is not equal to zero. If it is not, the program calculates the average by dividing the total of all grades by the number of grades, and then prints the average. If the counter is equal to zero, the program prints the message “no grades were entered”.

This pseudocode provides a clear and concise representation of the program’s logic, making it easy to understand and implement. The use of a WHILE loop allows the program to handle an arbitrary number of grades, and the clear and concise statements make the program’s logic easy to follow. The use of a sentinel value ensures that the program can continue processing grades until the user signals that they are done, making the program more flexible and user-friendly. Additionally, the program handles the case where no grades were entered by checking the value of the counter variable and printing an appropriate message.

Student passed and failured in pseudocode

This pseudocode represents a program that tracks the results of ten exams and outputs the number of passed and failed exams. The program starts by initializing three variables: passes, failures, and student counter. Passes and failures are set to zero, and student counter is set to one.

The program then enters a WHILE loop that continues as long as the student counter is less than or equal to ten. The loop contains three statements: an INPUT statement that inputs the result of the next exam, an IF statement that increments the number of passes by one if the student passed the exam, and an ELSE statement that increments the number of failures by one if the student failed the exam. The loop also contains an ADD statement that increments the student counter by one.

Once the loop has completed, the program outputs the number of passes and failures, and then checks if eight or more students passed. If eight or more students passed, the program outputs the message “raise tuition”.

This pseudocode provides a clear and concise representation of the program’s logic, making it easy to understand and implement. The use of a WHILE loop allows the program to handle an arbitrary number of students and exams, and the clear and concise statements make the program’s logic easy to follow. The program also implements a simple check for whether the school should raise tuition based on the number of passed exams.

Pseudocode of Bubble sort technique

This pseudocode represents a program that implements a sorting algorithm known as the “Bubble Sort” method. The program sorts an array of records represented by the key variable.

The program starts by setting the n variable to the number of records to be sorted. The program then enters a REPEAT loop that continues until the flag variable is set to false or n is equal to one.

The REPEAT loop contains a nested FOR loop that iterates over the array of records. The FOR loop starts with counter equal to one and continues until counter is less than n-1. The FOR loop contains an IF statement that checks whether the current record (key[counter]) is greater than the next record (key[counter+1]). If this condition is true, the records are swapped using the swap statement, and the flag variable is set to true.

At the end of each iteration of the REPEAT loop, the n variable is decremented by one. This means that the next iteration of the REPEAT loop will consider one fewer record, since the largest record will have “bubbled up” to the end of the array.

This pseudocode provides a clear and concise representation of the Bubble Sort algorithm, making it easy to understand and implement. The use of a REPEAT loop and a FOR loop allow the program to sort an arbitrary number of records, and the clear and concise statements make the program’s logic easy to follow.

OR the same can be expressed more concisely in words as below

This pseudocode represents a program that implements a sorting algorithm known as the “Bubble Sort” method. The program sorts an array of keys.

The program starts with a REPEAT loop that continues until the flag variable is not set. The REPEAT loop contains a FOR loop that iterates over each pair of keys in the array. The FOR loop contains an IF statement that checks whether the current pair of keys are in the wrong order. If this condition is true, the keys are swapped using the swap statement, and the flag variable is set to True.

The program continues to repeat the REPEAT loop until the flag variable is not set, indicating that all the keys have been sorted into the correct order.

This pseudocode provides a clear and concise representation of the Bubble Sort algorithm, making it easy to understand and implement. The use of a REPEAT loop and a FOR loop allow the program to sort an arbitrary number of keys, and the clear and concise statements make the program’s logic easy to follow.

Pseudocode Example: Defining a Function with FOR Loop and IF Statement

Explanation:

  • The structure of the function is similar to the previous example.
  • The FOR loop is used to repeat the statements within it n times. i is the loop variable that starts at 1 and goes up to n.
  • The IF statement is used to perform a different set of statements depending on the value of condition. If condition is true, the statements in the THEN clause are executed. If condition is false, the statements in the ELSE clause are executed.

QuickSort Algorithm using Function Declaration in Pseudocode

  1. FUNCTION QuickSort(array, startIndex, endIndex): declares the start of the function QuickSort which takes an array, the starting index and the ending index of the portion of the array that needs to be sorted.
  2. DECLARE pivotIndex: declares a variable pivotIndex which will be used to store the pivot index.
  3. IF startIndex < endIndex THEN: checks if the starting index is less than the ending index, if it is true then the function continues to execute.
  4. pivotIndex = Partition(array, startIndex, endIndex): assigns the value returned by the function Partition to the pivotIndex variable. The Partition function is called with the array, startIndex and endIndex as input parameters.
  5. QuickSort(array, startIndex, pivotIndex - 1): calls the QuickSort function with the portion of the array from startIndex to pivotIndex - 1 as input parameters.
  6. QuickSort(array, pivotIndex + 1, endIndex): calls the QuickSort function with the portion of the array from pivotIndex + 1 to endIndex as input parameters.
  7. END IF: marks the end of the IF statement.
  8. END FUNCTION: marks the end of the QuickSort function.
  9. FUNCTION Partition(array, startIndex, endIndex): declares the start of the function Partition which takes an array, the startIndex and the endIndex as input parameters.
  10. DECLARE pivotValue, storeIndex: declares two variables pivotValue and storeIndex which will be used in the function.
  11. pivotValue = array[endIndex]: assigns the value at the endIndex of the array to the pivotValue variable.
  12. storeIndex = startIndex: assigns the value of startIndex to the storeIndex variable.
  13. FOR i = startIndex TO endIndex - 1: starts a FOR loop to iterate from startIndex to endIndex - 1.
  14. IF array[i] <= pivotValue THEN: checks if the value of the current iteration is less than or equal to the pivotValue.
  15. SWAP array[i], array[storeIndex]: if the above condition is true, then the value at the ith index of the array is swapped with the value at the storeIndex of the array.
  16. storeIndex = storeIndex + 1: the value of storeIndex is incremented by 1.
  17. NEXT i: marks the end of the FOR loop.
  18. SWAP array[storeIndex], array[endIndex]: swaps the value at the storeIndex of the array with the value at the endIndex of the array.
  19. RETURN storeIndex: returns the value of storeIndex from the Partition function.
  20. END FUNCTION: marks the end of the Partition function.

10 Comments

Leave a Reply to admin X

This site uses Akismet to reduce spam. Learn how your comment data is processed.