# Search Algorithms in C
Table of Contents
Introduction
Big(O) is a mathmatical notation that describes the limiting behaviour of a function when an argument tends towards a certain value or infinity.
Characterising algorithms and defining their features; Definiteness, Finiteness, Effectiveness, and I/O, all come into what makes an algorithm. Requiring an input: 0 or more, Output: 1 or more, Definite: definite value that must terminate after method execution, Finite: allows concurrency with other running programs / holds break points, Effective: serves purpose in what’s being executed
Time Function
Time Complexity
Space Complexity
Here i’ve assorted a select few algorithms that i’ll briefly explain, so that they may hold as a baseline. A baseline to not only provide structure but something to improve upon. Understanding that there are many ways to implement search algorithms where the application may vary for each use.
Search Algorithms
#include <stdio.h>#include <stdbool.h>#include <math.h>
// Search algorithm for binary searchtypedef struct { int index; int size; int target;} binarySearchResult;
// Search algorithm for two crystal balltypedef struct { bool found; int index; int size; int target;} crystalballSearchResult;
// Table to store multiple algorithm resultstypedef struct { binarySearchResult binaryResult; crystalballSearchResult crystalballResult;} SearchResults;
// Function to perform binary search on a sorted arraybinarySearchResult binarySearch(int arr[], int size, int target) { binarySearchResult result = {-1, size, target}; // Default result: index = -1, size = passed size, target = passed target
// Example sorted array int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17}; int size = sizeof(arr) / sizeof(arr[0]); int target = 8;
int low = 0; int high = size - 1;
while (low <= high) { int mid = low + (high - low) / 2;
if (arr[mid] == target) { result.index = mid; // Update index if target is found return result; }
if (arr[mid] > target) { high = mid - 1; } else { low = mid + 1; } }
return result; // Return the struct, including index (-1 if not found), size, and target}
crystalballSearchResult crystalballSearch (bool arr[], int size, int target){ crystalballSearchResult result = {false, -1, size, target}; // Default result: not found, size, and target
for (int i = 0; i < size; i++) { if (arr[i] == target) { result.found = true; result.index = i; // Update index if target is found return result; } }
return result; // Return the struct, including found flag, index, size, and target}
// Function to initialize the data and return the search results for all algorithmsSearchResults generateSearchData() { static int int_arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17}; static bool bool_arr[] = {false, false, false, false, true, true, false, false, false, false}; // Example boolean array int int_size = sizeof(int_arr) / sizeof(int_arr[0]); int bool_size = sizeof(bool_arr) / sizeof(bool_arr[0]); int binary_target = 8; int crystalball_target = 5;
// Create a result structure to hold all algorithm results SearchResults results;
// Perform binary search and store the result results.binaryResult = binarySearch(int_arr, int_size, binary_target);
// Perform crystal ball search and store the result results.crystalballResult = crystalballSearch(bool_arr, bool_size, crystalball_target); // Using the boolean array
return results; // Return the structure holding all search results}
int main() { // Call generateSearchData to get the results of all algorithms SearchResults results = generateSearchData();
// Display the result of binary search if (results.binaryResult.index != -1) { printf("Binary Search: Element found at index %d, size of array: %d, target: %d\n", results.binaryResult.index, results.binaryResult.size, results.binaryResult.target); } else { printf("Binary Search: Element not found, size of array: %d, target: %d\n", results.binaryResult.size, results.binaryResult.target); }
// Display the result of the crystal ball search if (results.crystalballResult.found) { printf("Crystal Ball Search: Element found at index %d, size of array: %d, target: %d\n", results.crystalballResult.index, results.crystalballResult.size, results.crystalballResult.target); } else { printf("Crystal Ball Search: Element not found, size of array: %d, target: %d\n", results.crystalballResult.size, results.crystalballResult.target); }
return 0;}