Compare commits

...

4 Commits

Author SHA1 Message Date
f7ffa91c11 fixed formatting errors 2022-05-29 21:17:44 +01:00
2c9ce86b76 added linear search 2022-05-29 20:01:58 +01:00
8b200c099f switch from size_t to int for arr sizes 2022-05-29 18:23:10 +01:00
063a229f3a binary search: remove superfluous variable 2022-05-26 16:51:14 +01:00
7 changed files with 59 additions and 12 deletions

View File

@@ -11,19 +11,18 @@
#include <stddef.h>
int bsrch(int target, int* arr, size_t n)
int bsrch(int target, int* arr, int n)
{
// TODO: Find a way to error if list is out of order
bool found = false;
int start = 0, end = n - 1;
if (arr[start] > target || arr[end] < target) return -1;
int mid = (start + end) / 2;
while (!found)
while (true)
{
if (arr[mid] == target) found = true;
if (start == end && !found) return -1;
if (mid == end && !found) return -1;
if (arr[mid] == target) break;
if (start == end) return -1;
if (mid == end) return -1;
else if (arr[mid] < target)
{
start = mid + 1;

View File

@@ -9,4 +9,4 @@
#include <stddef.h>
int bsrch(int target, int* arr, size_t n);
int bsrch(int target, int* arr, int n);

View File

@@ -10,7 +10,7 @@
#include <stdbool.h>
void bubblesort(int* arr, size_t n)
void bubblesort(int* arr, int n)
{
bool swapped = true;

View File

@@ -9,4 +9,4 @@
#include <stddef.h>
void bubblesort(int* arr, size_t n);
void bubblesort(int* arr, int n);

View File

@@ -7,6 +7,7 @@
#include "bubble.h"
#include "bsrch.h"
#include "lsrch.h"
#include "ccolour/colour.h"
#include <ctype.h>
@@ -16,7 +17,7 @@
#include <string.h>
inline void quit_err(const char* msg)
static inline void quit_err(const char* msg)
{
ChangeColour(msg, RED_FOREGROUND, DEFAULT_COLOR, true);
exit(EXIT_FAILURE);
@@ -62,6 +63,7 @@ int main(int argc, char** argv)
printf(
"1. Binary search\n"
"2. Bubble sort\n"
"3. Liniar search\n"
"Please pick a mode of operation: "
);
fgets(choice_str, 16, stdin);
@@ -70,7 +72,7 @@ int main(int argc, char** argv)
// TODO: Add checking that number is in range
while (!isint(choice_str))
{
printf("Sorry but you must enter a number between 1 and 2\n");
printf("Sorry but you must enter a number between 1 and 3\n");
printf("Try again: ");
fgets(choice_str, 16, stdin);
choice_str[strcspn(choice_str, "\n")] = 0;
@@ -108,7 +110,26 @@ int main(int argc, char** argv)
}
putchar('\n');
}
else if (choice == 3)
{
char target_str[16];
printf("Enter number to search for: ");
fgets(target_str, 16, stdin);
target_str[strcspn(target_str, "\n")] = 0;
while (!isint(target_str))
{
printf("Sorry but you must enter a whole number, try again: ");
fgets(target_str, 16, stdin);
}
int target = atoi(target_str);
int result = lsrch(arr, n, target);
if (result == -1)
puts("Value not found");
else
printf("Value found at index: %d\n", result);
}
free(arr);
return 0;
}

16
src/lsrch.c Normal file
View File

@@ -0,0 +1,16 @@
/*
* Filename: lsrch.c
* Author(s): Roland (r.weirhowell@gmail.com)
* Description: Perform linear search on an integer array
* License: MIT (https://spdx.org/licenses/MIT.html)
*/
#include "lsrch.h"
int lsrch(int* arr, int n, int target)
{
for (int i = 0; i < n; i++)
if (arr[i] == target) return i;
return -1;
}

11
src/lsrch.h Normal file
View File

@@ -0,0 +1,11 @@
/*
* Filename: lsrch.h
* Author(s): Roland (r.weirhowell@gmail.com)
* Description: Header file for lsrch.c
* License: MIT (https://spdx.org/licenses/MIT.html)
*/
#pragma once
int lsrch(int* arr, int n, int target);