-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mahmoud
committed
Apr 11, 2024
1 parent
f024b5c
commit 0af22bc
Showing
15 changed files
with
349 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
O(sqrt(n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
O(log(n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
O(nm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
searsh_algos_by_c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
#ifndef SEARCH_ALGOS_H | ||
#define SEARCH_ALGOS_H | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stddef.h> | ||
#include <math.h> | ||
|
||
/** | ||
* 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 */ |