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

Sequential

The document discusses four sorting algorithms: sequential search, binary search, selection sort, and bubble sort. Code examples are provided for each algorithm to demonstrate how they work by sorting sample arrays of integers.

Uploaded by

ron02naadat
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)
11 views5 pages

Sequential

The document discusses four sorting algorithms: sequential search, binary search, selection sort, and bubble sort. Code examples are provided for each algorithm to demonstrate how they work by sorting sample arrays of integers.

Uploaded by

ron02naadat
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/ 5

GARCIA, MARICAR B.

BSIT 1-B

CC103 INTERMEDIATE PROGRAMMING

#include <iostream>

using namespace std;

int sequentialSearch(int arr[], int size, int target) {

for (int i = 0; i < size; ++i) {

if (arr[i] == target) {

return i;

return -1;

int main() {

int arr[] = {10, 6, 8, 7, 9};

int size = sizeof(arr) / sizeof(arr[0]);

int target = 10;

int result = sequentialSearch(arr, size, target);

if (result != -1) {

cout << "Element found at index " << result << "." << endl;

} else {

cout << "Element not found." << endl;

return 0;

#include <iostream>
using namespace std;

int binarySearch(int arr[], int size, int target) {

int start = 0;

int mid;

int end = size - 1;

while (start <= end) {

mid = start + (end - start) / 2;

if (arr[mid] == target) {

return mid;

} else if (arr[mid] < target) {

start = mid + 1;

} else {

end = mid - 1;

return -1;

int main() {

int arr[] = {10, 20, 30, 40, 50};

int size = sizeof(arr) / sizeof(arr[0]);

int target;

cout << "Enter the element to search for: ";

cin >> target;

int result = binarySearch(arr, size, target);

if (result != -1) {

cout << "Element found at index " << result << "." << endl;

} else {
cout << "Element not found." << endl;

return 0;

#include <iostream>

using namespace std;


void selectionSort(int arr[], int size) {

for (int i = 0; i < size - 1; ++i) {

int minIndex = i;

for (int j = i + 1; j < size; ++j)

if (arr[j] < arr[minIndex])

minIndex = j;

swap(arr[i], arr[minIndex]);

int main() {

int arr[] = {8, 9, 4, 1, 6};

int size = sizeof(arr) / sizeof(arr[0]);

selectionSort(arr, size);

for (int i = 0; i < size; ++i)

cout << arr[i] << " ";

cout << endl;

return 0;

#include <iostream>

using namespace std;

void bubbleSort(int arr[], int size) {


for (bool swapped = true; swapped;) {

swapped = false;

for (int j = 0; j < size - 1; ++j) {

if (arr[j] > arr[j + 1]) {

swap(arr[j], arr[j + 1]);

swapped = true;

int main() {

int arr[] = {20, 10, 60, 30, 90};

int size = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, size);

for (int i = 0; i < size; ++i)

cout << arr[i] << " ";

cout << endl;

return 0;

You might also like