0% found this document useful (0 votes)
0 views17 pages

Algorithm

The document provides pseudocode for various mathematical and algorithmic operations including summing two numbers, summing a list of numbers, multiplying two numbers, and performing matrix operations. It also includes sorting algorithms such as insertion sort, bubble sort, quick sort, merge sort, and selection sort, along with traces of their execution. Additionally, it covers finding uniqueness in an array, linear search, factorial calculation, Fibonacci sequence generation, and counting binary digits.

Uploaded by

Gowtham Gowthu
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)
0 views17 pages

Algorithm

The document provides pseudocode for various mathematical and algorithmic operations including summing two numbers, summing a list of numbers, multiplying two numbers, and performing matrix operations. It also includes sorting algorithms such as insertion sort, bubble sort, quick sort, merge sort, and selection sort, along with traces of their execution. Additionally, it covers finding uniqueness in an array, linear search, factorial calculation, Fibonacci sequence generation, and counting binary digits.

Uploaded by

Gowtham Gowthu
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/ 17

sum of 2 numbers psuedo code

START

// Declare variables

INPUT num1

INPUT num2

// Perform addi on

sum ← num1 + num2

// Display the result

OUTPUT "The sum is: ", sum

END

sum of list of number psuedo code

START

// Ini alize variables

INPUT n // Number of elements in the list

sum ← 0

// Loop through the list

FOR i ← 1 TO n DO

INPUT number

sum ← sum + number

END FOR

// Display the total sum


OUTPUT "Total sum is: ", sum

END

product of two numbers psuedo code

START

// Declare variables

INPUT num1

INPUT num2

// Mul ply the numbers

product ← num1 × num2

// Display the result

OUTPUT "The product is: ", product

END

product of two matrices psuedocode

START

// Input dimensions

INPUT m, n, p // m = rows in A, n = cols in A / rows in B, p = cols in B

// Declare matrices A[m][n], B[n][p], and C[m][p]

DECLARE A[m][n], B[n][p], C[m][p]

// Input matrix A

FOR i ← 0 TO m-1 DO
FOR j ← 0 TO n-1 DO

INPUT A[i][j]

END FOR

END FOR

// Input matrix B

FOR i ← 0 TO n-1 DO

FOR j ← 0 TO p-1 DO

INPUT B[i][j]

END FOR

END FOR

// Ini alize matrix C with 0s

FOR i ← 0 TO m-1 DO

FOR j ← 0 TO p-1 DO

C[i][j] ← 0

END FOR

END FOR

// Matrix mul plica on logic

FOR i ← 0 TO m-1 DO

FOR j ← 0 TO p-1 DO

FOR k ← 0 TO n-1 DO

C[i][j] ← C[i][j] + A[i][k] × B[k][j]

END FOR

END FOR
END FOR

// Output the product matrix C

FOR i ← 0 TO m-1 DO

FOR j ← 0 TO p-1 DO

OUTPUT C[i][j]

END FOR

END FOR

END

sum of two matrices psuedocode

START

// Input matrix dimensions

INPUT m, n // m = rows, n = columns

// Declare matrices A[m][n], B[m][n], and C[m][n]

DECLARE A[m][n], B[m][n], C[m][n]

// Input elements for Matrix A

FOR i ← 0 TO m-1 DO

FOR j ← 0 TO n-1 DO

INPUT A[i][j]

END FOR

END FOR

// Input elements for Matrix B


FOR i ← 0 TO m-1 DO

FOR j ← 0 TO n-1 DO

INPUT B[i][j]

END FOR

END FOR

// Compute the sum of A and B → store in C

FOR i ← 0 TO m-1 DO

FOR j ← 0 TO n-1 DO

C[i][j] ← A[i][j] + B[i][j]

END FOR

END FOR

// Output the resul ng matrix C

FOR i ← 0 TO m-1 DO

FOR j ← 0 TO n-1 DO

OUTPUT C[i][j]

END FOR

END FOR

END

find the uniqness of an array psuedo code

START

// Input array size

INPUT n

DECLARE array[n]
// Input array elements

FOR i ← 0 TO n-1 DO

INPUT array[i]

END FOR

// Find uniqueness

FOR i ← 0 TO n-1 DO

count ← 0

FOR j ← 0 TO n-1 DO

IF array[i] = array[j] THEN

count ← count + 1

END IF

END FOR

IF count = 1 THEN

OUTPUT "Unique element: ", array[i]

END IF

END FOR

linear search psuedocode

START

// Input array size

INPUT n

DECLARE array[n]
// Input array elements

FOR i ← 0 TO n-1 DO

INPUT array[i]

END FOR

// Input the key to search

INPUT key

found ← false

// Linear search logic

FOR i ← 0 TO n-1 DO

IF array[i] = key THEN

OUTPUT "Element found at posi on: ", i

found ← true

BREAK

END IF

END FOR

IF NOT found THEN

OUTPUT "Element not found"

END IF

END

factorial of n number psuedocode

START

// Input the number

INPUT n
factorial ← 1

// Check for edge case

IF n = 0 THEN

OUTPUT "Factorial is: 1"

ELSE

// Loop from 1 to n

FOR i ← 1 TO n DO

factorial ← factorial × i

END FOR

OUTPUT "Factorial is: ", factorial

END IF

END

fibonacci of number psuedocode

START

// Input number of terms

INPUT n

// Declare and ini alize variables

a←0

b←1

OUTPUT "Fibonacci Sequence: "

IF n ≥ 1 THEN

OUTPUT a

END IF
IF n ≥ 2 THEN

OUTPUT b

END IF

// Generate Fibonacci terms

FOR i ← 3 TO n DO

next ← a + b

OUTPUT next

a←b

b ← next

END FOR

END

binary digits in n binary reprsenenta on

START

INPUT n

count ← 0

IF n = 0 THEN

count ← 1

ELSE

WHILE n > 0 DO

n ← n DIV 2

count ← count + 1

END WHILE

END IF
OUTPUT "Number of binary digits: ", count

END

m,y,s,o,r,e,c,i,t,y and e,x,a,m,p,l,e and a,l,g,o,r,i,t,h,m do sor ng Inser on Bubble Quick Merge
Selec on sort

Original Character Sets

Label Characters

1. Mysore ['m','y','s','o','r','e','c','i','t','y']

2. Example ['e','x','a','m','p','l','e']

3. Algorithm ['a','l','g','o','r','i','t','h','m']

Inser on Sort

Inser on Sort builds the sorted list one item at a me.

Input Sorted Output

Mysore ['c','e','i','m','o','r','s','t','y','y']

Example ['a','e','e','l','m','p','x']

Algorithm ['a','g','h','i','l','m','o','r','t']

Bubble Sort

Bubble Sort repeatedly swaps adjacent elements if they’re in the wrong order.

Input Sorted Output

Mysore ['c','e','i','m','o','r','s','t','y','y']

Example ['a','e','e','l','m','p','x']

Algorithm ['a','g','h','i','l','m','o','r','t']

Quick Sort

Quick Sort is a divide-and-conquer algorithm that par ons around a pivot.


Input Sorted Output

Mysore ['c','e','i','m','o','r','s','t','y','y']

Example ['a','e','e','l','m','p','x']

Algorithm ['a','g','h','i','l','m','o','r','t']

Merge Sort

Merge Sort divides the list into halves, recursively sorts, and merges.

Input Sorted Output

Mysore ['c','e','i','m','o','r','s','t','y','y']

Example ['a','e','e','l','m','p','x']

Algorithm ['a','g','h','i','l','m','o','r','t']

Selec on Sort

Selec on Sort selects the smallest element and swaps it into place.

Input Sorted Output

Mysore ['c','e','i','m','o','r','s','t','y','y']

Example ['a','e','e','l','m','p','x']

Algorithm ['a','g','h','i','l','m','o','r','t']

1. Inser on Sort – Trace

Inser on Sort builds the sorted array from le to right by shi ing elements.

Ini al: [m, y, s, o, r, e, c, i, t, y]

Pass 1: [m, y, s, o, r, e, c, i, t, y] // y > m → no shi

Pass 2: [m, s, y, o, r, e, c, i, t, y] // s < y → move y, insert s

Pass 3: [m, o, s, y, r, e, c, i, t, y]

Pass 4: [m, o, r, s, y, e, c, i, t, y]
Pass 5: [e, m, o, r, s, y, c, i, t, y]

Pass 6: [c, e, m, o, r, s, y, i, t, y]

Pass 7: [c, e, i, m, o, r, s, y, t, y]

Pass 8: [c, e, i, m, o, r, s, t, y, y]

Pass 9: [c, e, i, m, o, r, s, t, y, y]

2. Bubble Sort – Trace

Bubble Sort repeatedly swaps adjacent elements un l sorted.

Ini al: [m, y, s, o, r, e, c, i, t, y]

Pass 1: [m, s, o, r, e, c, i, t, y, y]

Pass 2: [m, o, r, e, c, i, s, t, y, y]

Pass 3: [m, o, e, c, i, r, s, t, y, y]

Pass 4: [m, e, c, i, o, r, s, t, y, y]

Pass 5: [e, c, i, m, o, r, s, t, y, y]

Pass 6: [c, e, i, m, o, r, s, t, y, y]

3. Quick Sort – Trace

Uses divide-and-conquer by selec ng a pivot and par oning the array.

Let’s assume last element as pivot.

Ini al: [m, y, s, o, r, e, c, i, t, y]

Choose pivot = y

Par on: [m, s, o, r, e, c, i, t, y, y] → [m, s, o, r, e, c, i, t] < y

Recurse on [m, s, o, r, e, c, i, t]

Pivot = t → [m, s, o, r, e, c, i], t

Pivot = i → [e, c], i, [m, s, o, r]


Sort [e, c] → [c, e]

Sort [m, s, o, r] → Pivot = r → [m, o], r, [s]

Final sorted: [c, e, i, m, o, r, s, t, y, y]

4. Merge Sort – Trace

Splits the list, sorts each half, and merges them back.

Split: [m, y, s, o, r] & [e, c, i, t, y]

→ [m, y] + [s, o, r] + [e, c] + [i, t, y]

Sort + merge:

→ [m, y] → [m, y]

→ [s, o, r] → [o, r, s]

→ [e, c] → [c, e]

→ [i, t, y] → [i, t, y]

Merge:

→ [m, o, r, s, y]

→ [c, e, i, t, y]

Final merge:

→ [c, e, i, m, o, r, s, t, y, y]

5. Selec on Sort – Trace

Finds the minimum element and places it at the beginning.

Ini al: [m, y, s, o, r, e, c, i, t, y]


Pass 1: [c, y, s, o, r, e, m, i, t, y]

Pass 2: [c, e, s, o, r, y, m, i, t, y]

Pass 3: [c, e, i, o, r, y, m, s, t, y]

Pass 4: [c, e, i, m, r, y, o, s, t, y]

Pass 5: [c, e, i, m, o, y, r, s, t, y]

Pass 6: [c, e, i, m, o, r, y, s, t, y]

Pass 7: [c, e, i, m, o, r, s, y, t, y]

Pass 8: [c, e, i, m, o, r, s, t, y, y]

Dataset: "e,x,a,m,p,l,e" → [e, x, a, m, p, l, e]

Inser on Sort – Trace

Ini al: [e, x, a, m, p, l, e]

Pass 1: [e, x, a, m, p, l, e]

Pass 2: [a, e, x, m, p, l, e]

Pass 3: [a, e, m, x, p, l, e]

Pass 4: [a, e, m, p, x, l, e]

Pass 5: [a, e, l, m, p, x, e]

Pass 6: [a, e, e, l, m, p, x]

Bubble Sort – Trace

Ini al: [e, x, a, m, p, l, e]

Pass 1: [e, a, m, p, l, e, x]

Pass 2: [a, e, m, l, e, p, x]

Pass 3: [a, e, l, e, m, p, x]

Pass 4: [a, e, e, l, m, p, x]

Pass 5: [a, e, e, l, m, p, x]
Quick Sort – Trace

 Pivot = e

 Par oned into: [a] e [x, m, p, l, e]

 Con nue sor ng right side recursively

 Final sorted: [a, e, e, l, m, p, x]

Merge Sort – Trace

 Split into: [e,x,a] & [m,p,l,e]

 Subsorts: [a,e,x], [e,l,m,p]

 Merge → [a, e, e, l, m, p, x]

Selec on Sort – Trace

Ini al: [e, x, a, m, p, l, e]

Pass 1: [a, x, e, m, p, l, e]

Pass 2: [a, e, x, m, p, l, e]

Pass 3: [a, e, e, m, p, l, x]

Pass 4: [a, e, e, l, p, m, x]

Pass 5: [a, e, e, l, m, p, x]

Pass 6: [a, e, e, l, m, p, x]

Dataset: "a,l,g,o,r,i,t,h,m" → [a, l, g, o, r, i, t, h, m]

Inser on Sort – Trace

Ini al: [a, l, g, o, r, i, t, h, m]

Pass 1: [a, l, g, o, r, i, t, h, m]

Pass 2: [a, g, l, o, r, i, t, h, m]

Pass 3: [a, g, l, o, r, i, t, h, m]

Pass 4: [a, g, l, o, r, i, t, h, m]
Pass 5: [a, g, i, l, o, r, t, h, m]

Pass 6: [a, g, i, l, o, r, t, h, m]

Pass 7: [a, g, h, i, l, o, r, t, m]

Pass 8: [a, g, h, i, l, m, o, r, t]

Bubble Sort – Trace

Ini al: [a, l, g, o, r, i, t, h, m]

Pass 1: [a, g, l, o, i, r, h, m, t]

Pass 2: [a, g, l, i, o, h, m, r, t]

Pass 3: [a, g, i, l, h, m, o, r, t]

Pass 4: [a, g, h, i, l, m, o, r, t]

Quick Sort – Trace

 Pivot = m

 Par oned: [a, l, g, i, h] m [o, r, t]

 Recursively sort both halves

 Final sorted: [a, g, h, i, l, m, o, r, t]

Merge Sort – Trace

 Split: [a,l,g,o], [r,i,t,h,m]

 Subsort and merge

 Final: [a, g, h, i, l, m, o, r, t]

Selec on Sort – Trace

Ini al: [a, l, g, o, r, i, t, h, m]

Pass 1: [a, l, g, o, r, i, t, h, m]

Pass 2: [a, g, l, o, r, i, t, h, m]

Pass 3: [a, g, h, o, r, i, t, l, m]

Pass 4: [a, g, h, i, r, o, t, l, m]
Pass 5: [a, g, h, i, l, o, t, r, m]

Pass 6: [a, g, h, i, l, m, t, r, o]

Pass 7: [a, g, h, i, l, m, o, r, t]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy