diff --git a/0x1E-search_algorithms/0-linear.c b/0x1E-search_algorithms/0-linear.c new file mode 100755 index 0000000..c7a4de1 --- /dev/null +++ b/0x1E-search_algorithms/0-linear.c @@ -0,0 +1,28 @@ + +#include "search_algos.h" + +/** + * linear_search - searches for a value in an array of integers + * using the Linear search algorithm. + * @array: pointer to the first element of the array to search in. + * @size: number of elements in array. + * @value: value to search for. + * + * Return: the first index where value is located, + * or -1 if value is not present in array or if array is NULL. +*/ +int linear_search(int *array, size_t size, int value) +{ + size_t i; + + if (array == NULL) + return (-1); + + for (i = 0; i < size; i++) + { + printf("Value checked array[%ld] = [%d]\n", i, array[i]); + if (array[i] == value) + return (i); + } + return (-1); +} diff --git a/0x1E-search_algorithms/1-binary.c b/0x1E-search_algorithms/1-binary.c new file mode 100755 index 0000000..85e1365 --- /dev/null +++ b/0x1E-search_algorithms/1-binary.c @@ -0,0 +1,36 @@ + +#include "search_algos.h" + +/** + * binary_search - searches for a value in a sorted array of integers. + * using the Binary search algorithm. + * @array: pointer to the first element of the array to search in. + * @size: number of elements in array. + * @value: value to search for. + * Return: the first index where value is located, + * or -1 if value is not present in array or if array is NULL. +*/ +int binary_search(int *array, size_t size, int value) +{ + size_t start = 0, end = size - 1, mid, i; + + if (array == NULL) + return (-1); + + while (start <= end) + { + printf("Searching in array: "); + for (i = start; i < end; i++) + printf("%d, ", array[i]); + printf("%d\n", array[i]); + + mid = (start + end) / 2; + if (array[mid] < value) + start = mid + 1; + else if (array[mid] > value) + end = mid - 1; + else + return (mid); + } + return (-1); +} diff --git a/0x1E-search_algorithms/100-jump.c b/0x1E-search_algorithms/100-jump.c new file mode 100755 index 0000000..cfba663 --- /dev/null +++ b/0x1E-search_algorithms/100-jump.c @@ -0,0 +1,40 @@ + +#include "search_algos.h" + +/** + * jump_search - searches for a value in a sorted array of integers + * using the Jump search algorithm. + * @array: pointer to the first element of the array to search in. + * @size: number of elements in array. + * @value: value to search for. + * Return: the first index where value is located, + * or -1 if value is not present in array or if array is NULL. +*/ +int jump_search(int *array, size_t size, int value) +{ + size_t jump = sqrt(size); + size_t i = 0, x = 0; + + if (array == NULL) + return (-1); + + while (x < size && array[x] < value) + { + printf("Value checked array[%lu] = [%d]\n", x, array[x]); + i = x; + x += jump; + } + + printf("Value found between indexes [%lu] and [%lu]\n", i, x); + + x = x < size - 1 ? x : size - 1; + + for (; i <= x; i++) + { + printf("Value checked array[%lu] = [%d]\n", i, array[i]); + if (array[i] == value) + return (i); + } + + return (-1); +} diff --git a/0x1E-search_algorithms/101-O b/0x1E-search_algorithms/101-O new file mode 100755 index 0000000..601354e --- /dev/null +++ b/0x1E-search_algorithms/101-O @@ -0,0 +1,2 @@ + +O(sqrt(n)) diff --git a/0x1E-search_algorithms/102-interpolation.c b/0x1E-search_algorithms/102-interpolation.c new file mode 100755 index 0000000..16226bb --- /dev/null +++ b/0x1E-search_algorithms/102-interpolation.c @@ -0,0 +1,40 @@ + +#include "search_algos.h" + +/** + * interpolation_search - searches for a value in a sorted array of integers + * using the Interpolation search algorithm. + * @array: pointer to the first element of the array to search in. + * @size: number of elements in array. + * @value: value to search for. + * Return: the first index where value is located, or -1 on failure. +*/ +int interpolation_search(int *array, size_t size, int value) +{ + size_t start = 0, end = size - 1, pos; + + if (array == NULL) + return (-1); + + while (start <= end) + { + pos = start + (((double)(end - start) / + (array[end] - array[start])) * (value - array[start])); + + if (pos < start || pos > end) + { + printf("Value checked array[%lu] is out of range\n", pos); + break; + } + + printf("Value checked array[%lu] = [%d]\n", pos, array[pos]); + + if (array[pos] < value) + start = pos + 1; + else if (array[pos] > value) + end = pos - 1; + else + return (pos); + } + return (-1); +} diff --git a/0x1E-search_algorithms/103-exponential.c b/0x1E-search_algorithms/103-exponential.c new file mode 100755 index 0000000..5c7784a --- /dev/null +++ b/0x1E-search_algorithms/103-exponential.c @@ -0,0 +1,50 @@ + +#include "search_algos.h" + +/** + * exponential_search - Searches for a value in a sorted array of + * integers using the exponential search algorithm. + * @array: A pointer to the first element of the array to search. + * @size: The number of elements in the array. + * @value: The value to search for. + * Return: If the value is not present or the array is NULL, -1. + * else the first index where the value is located. +*/ +int exponential_search(int *array, size_t size, int value) +{ + size_t start, end, mid, i, bound = 1; + + if (array == NULL || size == 0) + return (-1); + + /* Find the range for the binary search. */ + while (bound < size && array[bound] < value) + { + printf("Value checked array[%lu] = [%d]\n", bound, array[bound]); + bound *= 2; + } + + /* Calculate the start and end of the subarray. */ + start = bound / 2; + end = bound < size - 1 ? bound : size - 1; + printf("Value found between indexes [%lu] and [%lu]\n", start, end); + + /* Perform the binary search. */ + while (start <= end) + { + printf("Searching in array: "); + for (i = start; i < end; i++) + printf("%d, ", array[i]); + printf("%d\n", array[i]); + + mid = (start + end) / 2; + if (array[mid] == value) + return (mid); + else if (array[mid] > value) + end = mid - 1; + else + start = mid + 1; + + } + return (-1); +} diff --git a/0x1E-search_algorithms/104-advanced_binary.c b/0x1E-search_algorithms/104-advanced_binary.c new file mode 100755 index 0000000..de9aab8 --- /dev/null +++ b/0x1E-search_algorithms/104-advanced_binary.c @@ -0,0 +1,50 @@ + +#include "search_algos.h" + +/** + * recursive_binary - Helper function for advanced_binary function. + * @array: pointer to the first element of the array to search in. + * @start: index of the first element of the subarray to search in. + * @end: index of the last element of the subarray to search in. + * @value: value to search for. + * Return: The first index where value is located +*/ +int recursive_binary(int *array, int start, int end, int value) +{ + int i = start; + + if (start <= end) + { + int mid = start + (end - start) / 2; + + printf("Searching in array: "); + for (; i < end; i++) + printf("%d, ", array[i]); + printf("%d\n", array[i]); + + + if (array[mid] == value) + return (mid == start || array[mid - 1] != value ? mid : + recursive_binary(array, start, mid, value)); + + if (array[mid] < value) + return (recursive_binary(array, mid + 1, end, value)); + + if (array[mid] > value) + return (recursive_binary(array, start, mid - 1, value)); + } + return (-1); +} + +/** +* advanced_binary - searches for a value in a sorted array of integers. +* @array: pointer to the first element of the array to search in. +* @size: number of elements in array. +* @value: value to search for. +* Return: The first index where value is located +* or -1 if value is not present. +*/ +int advanced_binary(int *array, size_t size, int value) +{ + return (recursive_binary(array, 0, size - 1, value)); +} diff --git a/0x1E-search_algorithms/105-jump_list.c b/0x1E-search_algorithms/105-jump_list.c new file mode 100755 index 0000000..81d9aae --- /dev/null +++ b/0x1E-search_algorithms/105-jump_list.c @@ -0,0 +1,46 @@ + +#include "search_algos.h" + +/** + * jump_list - searches for a value in a sorted list of integers + * using the Jump search algorithm. + * @list: pointer to the head of the list to search in. + * @size: number of currents in list. + * @value: value to search for. + * Return: a pointer to the first node where value is located, + * or NULL if value is not present in list or if list is NULL. +*/ +listint_t *jump_list(listint_t *list, size_t size, int value) +{ + size_t jump = sqrt((double)size), i = 0, x = 0; + listint_t *current = list, *temp = list; + + if (list == NULL || size == 0) + return (NULL); + + while (x < size && temp->n < value && temp->next) + { + i = x; + x = temp->next && x + jump < size ? x + jump : size - 1; + + while (temp->next && temp->index < x) + temp = temp->next; + + printf("Value checked at index[%lu] = [%d]\n", x, temp->n); + } + + printf("Value found between indexes [%lu] and [%lu]\n", i, x); + + while (current->index < i) + current = current->next; + + for (; current && current->index <= x; (current = current->next), i++) + { + printf("Value checked at index[%lu] = [%d]\n", + current->index, current->n); + + if (current->n == value) + return (current); + } + return (NULL); +} diff --git a/0x1E-search_algorithms/2-O b/0x1E-search_algorithms/2-O new file mode 100755 index 0000000..66e9d35 --- /dev/null +++ b/0x1E-search_algorithms/2-O @@ -0,0 +1,2 @@ + +O(n) diff --git a/0x1E-search_algorithms/3-0 b/0x1E-search_algorithms/3-0 new file mode 100755 index 0000000..3c7b560 --- /dev/null +++ b/0x1E-search_algorithms/3-0 @@ -0,0 +1,2 @@ + +O(1) diff --git a/0x1E-search_algorithms/4-0 b/0x1E-search_algorithms/4-0 new file mode 100755 index 0000000..b6e1a5f --- /dev/null +++ b/0x1E-search_algorithms/4-0 @@ -0,0 +1,2 @@ + +O(log(n)) diff --git a/0x1E-search_algorithms/5-0 b/0x1E-search_algorithms/5-0 new file mode 100755 index 0000000..3c7b560 --- /dev/null +++ b/0x1E-search_algorithms/5-0 @@ -0,0 +1,2 @@ + +O(1) diff --git a/0x1E-search_algorithms/6-0 b/0x1E-search_algorithms/6-0 new file mode 100755 index 0000000..5f04353 --- /dev/null +++ b/0x1E-search_algorithms/6-0 @@ -0,0 +1,2 @@ + +O(nm) diff --git a/0x1E-search_algorithms/README.md b/0x1E-search_algorithms/README.md new file mode 100644 index 0000000..c4cf2c6 --- /dev/null +++ b/0x1E-search_algorithms/README.md @@ -0,0 +1 @@ +searsh_algos_by_c diff --git a/0x1E-search_algorithms/search_algos.h b/0x1E-search_algorithms/search_algos.h new file mode 100755 index 0000000..6039b17 --- /dev/null +++ b/0x1E-search_algorithms/search_algos.h @@ -0,0 +1,46 @@ + +#ifndef SEARCH_ALGOS_H +#define SEARCH_ALGOS_H + +#include +#include +#include +#include + +/** + * struct listint_s - singly linked list + * + * @n: Integer + * @index: Index of the node in the list + * @next: Pointer to the next node + * + * Description: singly linked list node structure + */ +typedef struct listint_s +{ + int n; + size_t index; + struct listint_s *next; +} listint_t; + + +/** + * linear_search - searches for a value in an array of integers + * using the Linear search algorithm. + * @array: pointer to the first element of the array to search in. + * @size: number of elements in array. + * @value: value to search for. + * Return: the first index where value is located, + * or -1 if value is not present in array or if array is NULL. + * +*/ + +int linear_search(int *array, size_t size, int value); +int binary_search(int *array, size_t size, int value); +int jump_search(int *array, size_t size, int value); +int interpolation_search(int *array, size_t size, int value); +int exponential_search(int *array, size_t size, int value); +int advanced_binary(int *array, size_t size, int value); +listint_t *jump_list(listint_t *list, size_t size, int value); + +#endif /* SEARCH_ALGOS_H */