0% found this document useful (0 votes)
7 views5 pages

Computer Science Codes

The document provides algorithms for calculating factorials, generating Fibonacci sequences, and performing binary and linear searches, as well as bubble and selection sorts. Each algorithm includes input, processing steps, and output instructions. The examples illustrate the implementation of these algorithms in a structured format.

Uploaded by

roshanrungta95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Computer Science Codes

The document provides algorithms for calculating factorials, generating Fibonacci sequences, and performing binary and linear searches, as well as bubble and selection sorts. Each algorithm includes input, processing steps, and output instructions. The examples illustrate the implementation of these algorithms in a structured format.

Uploaded by

roshanrungta95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Factorial

// Factorial Calculation
// Example: 5! = 5 × 4 × 3 × 2 × 1 = 120

INPUT Number
SET Factorial = 1

FOR I FROM 1 TO Number


Factorial = Factorial × I
END FOR

OUTPUT "Factorial is", Factorial

FIBONACCI SEQUENCE
BEGIN
SET FIRST ← 0
SET SECOND ← 1
OUTPUT FIRST
OUTPUT SECOND
INPUT N // Number of Fibonacci terms to generate
LOOP I FROM 3 TO N
FIBONACCI ← FIRST + SECOND
OUTPUT FIBONACCI
SET FIRST ← SECOND
SET SECOND ← FIBONACCI
END LOOP
END
BINARY SEARCH
FUNCTION BinarySearch(array, target)
SET left = 0
SET right = length of array - 1
WHILE left ≤ right
SET mid = (left + right) / 2 // Find middle index
IF array[mid] = target THEN
RETURN mid // Target found at index mid
ELSE IF array[mid] < target THEN
SET left = mid + 1 // Search in the right half
ELSE
SET right = mid - 1 // Search in the left half
END WHILE
RETURN -1 // Target not found
END FUNCTION

Linear search
N = [2, 9, 5, 6, 7, 8] // Array elements
X=7 // Search value
Found = false // Boolean value
Counter = 0 // It will be used for the loop

loop Counter from 0 to 5 // Number of array elements - 1


if N[Counter] = X then
Found = true
output N[Counter], "found at position", Counter
end if
end loop

if Found = false then


output X, "not found"
end if

Output:
7 found at position​ 4
​ ​ ​ ​ ​ ​

BUBBLE SORT
Programming Example 21: Bubble sort

START
INPUT list of numbers in ARRAY
SET N = number of elements in ARRAY

FOR I FROM 0 TO N - 2
FOR J FROM 0 TO N - I - 2
IF ARRAY[J] > ARRAY[J + 1] THEN
TEMP = ARRAY[J]
ARRAY[J] = ARRAY[J + 1]
ARRAY[J + 1] = TEMP
END IF
END FOR
END FOR

OUTPUT "Sorted Array:"


FOR K FROM 0 TO N - 1
OUTPUT ARRAY[K]
END FOR
END

bubbleSort(array)
for i <- 1 to sizeOfArray - 1
swapped <- false
for j <- 1 to sizeOfArray - 1 - i
if leftElement > rightElement
swap leftElement and rightElement
swapped <- true
if swapped == false
break
end bubbleSort
SELECTION SORT
Programming Example 23: Selection sort

//==== Selection Sort ====

ELEMENTS = [1, 5, 3, 86, 256, 420, 9, 510, 51, 24, 60]


MIN = 0
I=0
TEMP = 0

loop MIN from 0 to 9


I = MIN
loop CURRENT from MIN + 1 to 10
if ELEMENTS[CURRENT] > ELEMENTS[I] then // for descending order
OR
if ELEMENTS[CURRENT] < ELEMENTS[I] then // for ascending order
I = CURRENT
end if
end loop
TEMP = ELEMENTS[I]
ELEMENTS[I] = ELEMENTS[MIN]
ELEMENTS[MIN] = TEMP
end loop

output "SORTED ARRAY"


loop C from 0 to 10
output ELEMENTS[C]
end loop
​ ​ ​

You might also like