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

Linear Search Algorithm: Declare Integer I SRCH Declare Integer X For I To Length X Ifx I Is SRCH Then Print SRCH

The document describes four algorithms: 1) A linear search algorithm that searches an array for a target value. 2) A bubble sort algorithm that sorts an array in ascending order by swapping adjacent elements. 3) A binary search algorithm that searches a sorted array by comparing the target to the middle element and adjusting the search range. 4) A matrix multiplication algorithm that multiplies two matrices by iterating through the rows and columns and summing the products of corresponding elements.

Uploaded by

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

Linear Search Algorithm: Declare Integer I SRCH Declare Integer X For I To Length X Ifx I Is SRCH Then Print SRCH

The document describes four algorithms: 1) A linear search algorithm that searches an array for a target value. 2) A bubble sort algorithm that sorts an array in ascending order by swapping adjacent elements. 3) A binary search algorithm that searches a sorted array by comparing the target to the middle element and adjusting the search range. 4) A matrix multiplication algorithm that multiplies two matrices by iterating through the rows and columns and summing the products of corresponding elements.

Uploaded by

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

Linear Search Algorithm

DECLARE INTEGER i, srch = 72 DECLARE INTEGER x [] = [ 20, 15, 12, 30, -5, 72, 456 ] FOR i = 0 TO LENGTH (x) - 1 IF x [i] IS srch THEN PRINT "Found ", srch END END IF NEXT i PRINT "Did not find ", srch END

Bubble Sort Algorithm


DECLARE INTEGER i, pass DECLARE INTEGER x [] = [ 20, 15, 12, 30, -5, 72, 456 ] FOR pass = 0 TO LENGTH (x) - 2 FOR i = 0 TO LENGTH (x) - pass - 2 IF x [i] > x [i + 1] THEN SWAP x [i], x [i + 1] END IF NEXT i NEXT pass END

Binary Search Algorithm


DECLARE INTEGER x [] = [ -5, 12, 15, 20, 30, 72, 456 ] DECLARE INTEGER loIndex = 0 DECLARE INTEGER hiIndex = LENGTH (x) - 1 DECLARE INTEGER midIndex, srch = 72 WHILE loIndex <= hiIndex midIndex = (loIndex + hiIndex) / 2 IF srch > x [midIndex] THEN loIndex = midIndex + 1 ELSE IF srch < x [midIndex] THEN hiIndex = midIndex - 1 ELSE EXIT WHILE END IF END WHILE IF loIndex > hiIndex THEN PRINT srch, " not found" ELSE PRINT srch, " found" END IF END

Matrix-Multiplication Algorithm
// == // | 10 // | // | 20 // == == 30 | == == | 5 | == ==

| 10 x 5 + 30 x 7 (260) | |

| X | 40 | ==

| = |

| 7 | == ==

| 20 x 5 + 40 * 7 (380) | == ==

DECLARE INTEGER a [][] = [ 10, 30 ] [ 20, 40 ] DECLARE INTEGER b [][] = [ 5, 7 ]

DECLARE INTEGER m = 2 // Number of rows in left matrix (a) DECLARE INTEGER p = 2 // Number of columns in left matrix (a) // Number of rows in right matrix (b) DECLARE INTEGER n = 1 // Number of columns in right matrix (b) // Note: cs1 must equal rs2 DECLARE INTEGER c [m][n] // c holds 2 rows by 1 columns // All elements initialize to 0 FOR i = 0 TO m - 1 FOR j = 0 TO n - 1 FOR k = 0 TO p - 1 c [i][j] = c [i][j] + a [i][k] * b [k][j] NEXT k NEXT j NEXT i

You might also like