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

Computers Programming

The document provides algorithms for sorting and searching in computer programming, specifically detailing Bubble Sort, Selection Sort, Linear Search, and Binary Search. Each algorithm is presented with a sample array and pseudocode for implementation. The document also includes user input prompts for target values in the search algorithms.

Uploaded by

govawam413
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)
6 views

Computers Programming

The document provides algorithms for sorting and searching in computer programming, specifically detailing Bubble Sort, Selection Sort, Linear Search, and Binary Search. Each algorithm is presented with a sample array and pseudocode for implementation. The document also includes user input prompts for target values in the search algorithms.

Uploaded by

govawam413
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/ 3

Computers Programming

2024-12-04
12:56

Status:
Baby
Tags:
Computer Science
Formative Assessments

Bubble Sort:
array ← [10, 7, 3, 2, 9, 1, 5, 6, 4, 8]
n ← LENGTH(array)

// Bubble Sort
i←0
WHILE i < n - 1 DO
j←0
WHILE j < n - 1 - i DO
IF array[j] > array[j + 1] THEN
temp ← array[j]
array[j] ← array[j + 1]
array[j + 1] ← temp
END IF
j←j+1
END WHILE
i←i+1
END WHILE
OUTPUT "Sorted Array:", array

Selection Sort:
array ← [10, 7, 3, 2, 9, 1, 5, 6, 4, 8]
n ← LENGTH(array)

// Selection Sort
i←0
WHILE i < n - 1 DO
minIndex ← i
j←i+1
WHILE j < n DO
IF array[j] < array[minIndex] THEN
minIndex ← j
END IF
j←j+1
END WHILE
temp ← array[i]
array[i] ← array[minIndex]
array[minIndex] ← temp
i←i+1
END WHILE
OUTPUT "Sorted Array:", array

Linear Search:
array ← [10, 7, 3, 2, 9, 1, 5, 6, 4, 8]
n ← LENGTH(array)

// User inputs target


OUTPUT "Enter target value:"
target ← INPUT()

// Linear Search
index ← -1
i←0
WHILE i < n DO
IF array[i] = target THEN
index ← i
BREAK
END IF
i←i+1
END WHILE

IF index ≠ -1 THEN
OUTPUT "Target found at index:", index
ELSE
OUTPUT "Target not found."
END IF

Binary Search:
array ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n ← LENGTH(array)

// User inputs target


OUTPUT "Enter target value:"
target ← INPUT()

// Binary Search
left ← 0
right ← n - 1
index ← -1

WHILE left ≤ right DO


mid ← FLOOR((left + right) / 2)
IF array[mid] = target THEN
index ← mid
BREAK
ELSE IF array[mid] < target THEN
left ← mid + 1
ELSE
right ← mid - 1
END IF
END WHILE

IF index ≠ -1 THEN
OUTPUT "Target found at index:", index
ELSE
OUTPUT "Target not found."
END IF

You might also like