0% found this document useful (0 votes)
5 views

cs

The document presents various pseudocode examples and explanations, including basic output, decision-making, and different sorting and searching algorithms. It covers algorithms like Bubble Sort, Selection Sort, Linear Search, and Binary Search, along with mathematical computations for summing numbers. Additionally, it includes checks for array symmetry and whether an array is sorted, providing a comprehensive overview of fundamental programming concepts.

Uploaded by

Ananmay Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

cs

The document presents various pseudocode examples and explanations, including basic output, decision-making, and different sorting and searching algorithms. It covers algorithms like Bubble Sort, Selection Sort, Linear Search, and Binary Search, along with mathematical computations for summing numbers. Additionally, it includes checks for array symmetry and whether an array is sorted, providing a comprehensive overview of fundamental programming concepts.

Uploaded by

Ananmay Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Extracted Pseudocode Examples and Explanations

1. Basic Output Example


Pseudocode:
loop A from 1 to 2
output "THIS WILL BE PRINTED TWICE"
end loop
C=1
loop while C < 5
output "THIS MESSAGE WILL BE PRINTED FOUR TIMES"
C=C+1
end loop
Explanation:
 This algorithm prints "THIS WILL BE PRINTED TWICE" two times using a for loop.
 Then, it uses a while loop to print "THIS MESSAGE WILL BE PRINTED FOUR TIMES" four times.
2. Simple Decision-Making (Password Check)
Pseudocode:
Set A = "MORGAN"
Input PASSWORD
If PASSWORD == A Then
Display "CORRECT"
Else
Display "WRONG"
End If
Explanation:
 Checks if the input password matches "MORGAN".
 If the condition is true, it displays "CORRECT". Otherwise, it prints "WRONG".
3. Checking If an Array is Sorted
Pseudocode:
I=0
SORTEDA = 1
SORTEDD = 1
SAMPLE = new Array()

loop I from 0 to 4
SAMPLE[I] = input("Enter the measurement")
end loop

loop I from 0 to 3
if SAMPLE[I] > SAMPLE[I+1] then
SORTEDA = 0
end if
if SAMPLE[I] < SAMPLE[I+1] then
SORTEDD = 0
end if
end loop
output "The array is:"

loop I from 0 to 4
output SAMPLE[I]
end loop

if SORTEDA = 1 then
output "The array is sorted in ASCENDING order"
else
output "The array is not sorted in ASCENDING order"
end if

if SORTEDD = 1 then
output "The array is sorted in DESCENDING order"
else
output "The array is not sorted in DESCENDING order"
end if
Explanation:
 Takes 5 user inputs and stores them in an array.
 Checks whether the array is sorted in ascending or descending order.
 Prints the appropriate message based on the detected order.
4. Linear Search (Finding a Value in an Array)
Pseudocode:
Declare ARRAY[10]
Declare Integer SEARCHVALUE
Input SEARCHVALUE
Found = False

loop I from 0 to 9
if ARRAY[I] = SEARCHVALUE then
Found = True
output "Value found at index ", I
end if
end loop

if Found = False then


output "Value not found"
end if
Explanation:
 Implements a linear search to find a specific value in an array.
 If found, prints the index; otherwise, prints "Value not found".
5. Binary Search (Searching in a Sorted Array)
Pseudocode:
Declare ARRAY[10]
Declare Integer SEARCHVALUE
Input SEARCHVALUE
Left = 0
Right = 9
Found = False

loop while Left <= Right


Mid = (Left + Right) div 2
if ARRAY[Mid] = SEARCHVALUE then
output "Value found at index ", Mid
Found = True
exit loop
else if ARRAY[Mid] < SEARCHVALUE then
Left = Mid + 1
else
Right = Mid - 1
end if
end loop

if Found = False then


output "Value not found"
end if
Explanation:
 A binary search algorithm for sorted arrays.
 Repeatedly divides the search space in half.
 Time Complexity: O(log n) (faster than linear search).
6. Bubble Sort (Sorting an Array)
Pseudocode:
Declare ARRAY[10]
loop I from 0 to 9
loop J from 0 to 8
if ARRAY[J] > ARRAY[J+1] then
TEMP = ARRAY[J]
ARRAY[J] = ARRAY[J+1]
ARRAY[J+1] = TEMP
end if
end loop
end loop
Explanation:
 Implements Bubble Sort.
 Compares adjacent elements and swaps them if necessary.
 Time Complexity: O(n²) (ine icient for large lists).
7. Selection Sort (Sorting an Array)
Pseudocode:
Declare ARRAY[10]
loop I from 0 to 9
MIN_INDEX = I
loop J from I+1 to 9
if ARRAY[J] < ARRAY[MIN_INDEX] then
MIN_INDEX = J
end if
end loop
TEMP = ARRAY[I]
ARRAY[I] = ARRAY[MIN_INDEX]
ARRAY[MIN_INDEX] = TEMP
end loop
Explanation:
 Implements Selection Sort.
 Finds the smallest element and moves it to the correct position.
 Time Complexity: O(n²).
8. Calculating the Sum of Numbers E iciently
Algorithm A (Most E icient - O(1))
SUM = n * (n + 1) / 2
output "The SUM is:", SUM
 Uses a mathematical formula to compute the sum instantly.
 Time Complexity: O(1) (Constant Time)
Algorithm B (Moderate E iciency - O(n))
SUM = 0
loop I from 1 to n
SUM = SUM + I
end loop
output "The SUM is:", SUM
 Uses a loop to compute the sum.
 Time Complexity: O(n) (Linear Time)
Algorithm C (Least E icient - O(n²))
SUM = 0
loop I from 1 to n
loop J from 1 to I
SUM = SUM + 1
end loop
end loop
output "The SUM is:", SUM
 Uses nested loops, making it highly ine icient.
 Time Complexity: O(n²) (Quadratic Time)
9. Checking If an Array is Symmetric
Pseudocode:
loop I from 0 to N/2 - 1
if ARRAY[I] != ARRAY[N - I - 1] then
output "Array is not symmetric"
exit loop
end if
end loop
output "Array is symmetric"
Explanation:
 Compares the first half of an array with the second half.
 If all elements match, the array is symmetric.
Final Thoughts
This document provides structured pseudocode examples covering:
 Input/Output
 Decision-making (IF-ELSE)
 Looping (FOR, WHILE)
 Sorting Algorithms (Bubble, Selection Sort)
 Searching Algorithms (Linear, Binary Search)
 Mathematical Computations
 Array Symmetry Check

You might also like