mirror of
https://github.com/RolandWH/ceesort.git
synced 2025-09-05 05:21:28 +01:00
Compare commits
6 Commits
ecdcdc2821
...
master
Author | SHA1 | Date | |
---|---|---|---|
f7ffa91c11 | |||
2c9ce86b76 | |||
8b200c099f | |||
063a229f3a | |||
38deb15d4d | |||
e260cf38dc |
10
src/bsrch.c
10
src/bsrch.c
@@ -11,18 +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 (arr[mid] == target) break;
|
||||
if (start == end) return -1;
|
||||
if (mid == end) return -1;
|
||||
else if (arr[mid] < target)
|
||||
{
|
||||
start = mid + 1;
|
||||
|
@@ -9,4 +9,4 @@
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
int bsrch(int target, int* arr, size_t n);
|
||||
int bsrch(int target, int* arr, int n);
|
||||
|
@@ -10,7 +10,7 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
void bubblesort(int* arr, size_t n)
|
||||
void bubblesort(int* arr, int n)
|
||||
{
|
||||
bool swapped = true;
|
||||
|
||||
|
@@ -9,4 +9,4 @@
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
void bubblesort(int* arr, size_t n);
|
||||
void bubblesort(int* arr, int n);
|
||||
|
@@ -7,15 +7,17 @@
|
||||
|
||||
#include "bubble.h"
|
||||
#include "bsrch.h"
|
||||
#include "lsrch.h"
|
||||
#include "ccolour/colour.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
@@ -51,20 +53,29 @@ int main(int argc, char** argv)
|
||||
}
|
||||
int n = argc - 1;
|
||||
|
||||
/*
|
||||
* The line:
|
||||
* buffer[strcspn(buffer, "\n")] = 0;
|
||||
* Strips trailing newline, so "2\n" becomes "2"
|
||||
*/
|
||||
|
||||
char choice_str[16];
|
||||
printf(
|
||||
"1. Binary search\n"
|
||||
"2. Bubble sort\n"
|
||||
"3. Liniar search\n"
|
||||
"Please pick a mode of operation: "
|
||||
);
|
||||
scanf("%s", choice_str);
|
||||
fgets(choice_str, 16, stdin);
|
||||
choice_str[strcspn(choice_str, "\n")] = 0;
|
||||
|
||||
// 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: ");
|
||||
scanf("%s", choice_str);
|
||||
fgets(choice_str, 16, stdin);
|
||||
choice_str[strcspn(choice_str, "\n")] = 0;
|
||||
}
|
||||
int choice = atoi(choice_str);
|
||||
|
||||
@@ -72,11 +83,12 @@ int main(int argc, char** argv)
|
||||
{
|
||||
char target_str[16];
|
||||
printf("Enter number to search for: ");
|
||||
scanf("%s", target_str);
|
||||
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: ");
|
||||
scanf("%s", target_str);
|
||||
fgets(target_str, 16, stdin);
|
||||
}
|
||||
int target = atoi(target_str);
|
||||
|
||||
@@ -98,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
16
src/lsrch.c
Normal 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
11
src/lsrch.h
Normal 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);
|
Reference in New Issue
Block a user