Skip to content

Commit cefc46e

Browse files
committed
Searching Algorithms
1 parent 07a02a3 commit cefc46e

File tree

18 files changed

+474
-0
lines changed

18 files changed

+474
-0
lines changed

0x1E-search_algorithms/0-linear.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "search_algos.h"
2+
3+
/**
4+
* linear_search - searches for a value in an array of
5+
* integers using the Linear search algorithm
6+
*
7+
* @array: input array
8+
* @size: size of the array
9+
* @value: value to search in
10+
* Return: Always EXIT_SUCCESS
11+
*/
12+
int linear_search(int *array, size_t size, int value)
13+
{
14+
int i;
15+
16+
if (array == NULL)
17+
return (-1);
18+
19+
for (i = 0; i < (int)size; i++)
20+
{
21+
printf("Value checked array[%u] = [%d]\n", i, array[i]);
22+
if (value == array[i])
23+
return (i);
24+
}
25+
return (-1);
26+
}

0x1E-search_algorithms/1-binary.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "search_algos.h"
2+
3+
/**
4+
* recursive_search - searches for a value in an array of
5+
* integers using the Binary search algorithm
6+
*
7+
*
8+
* @array: input array
9+
* @size: size of the array
10+
* @value: value to search in
11+
* Return: index of the number
12+
*/
13+
int recursive_search(int *array, size_t size, int value)
14+
{
15+
size_t half = size / 2;
16+
size_t i;
17+
18+
if (array == NULL || size == 0)
19+
return (-1);
20+
21+
printf("Searching in array");
22+
23+
for (i = 0; i < size; i++)
24+
printf("%s %d", (i == 0) ? ":" : ",", array[i]);
25+
26+
printf("\n");
27+
28+
if (half && size % 2 == 0)
29+
half--;
30+
31+
if (value == array[half])
32+
return ((int)half);
33+
34+
if (value < array[half])
35+
return (recursive_search(array, half, value));
36+
37+
half++;
38+
39+
return (recursive_search(array + half, size - half, value) + half);
40+
}
41+
42+
/**
43+
* binary_search - calls to binary_search to return
44+
* the index of the number
45+
*
46+
* @array: input array
47+
* @size: size of the array
48+
* @value: value to search in
49+
* Return: index of the number
50+
*/
51+
int binary_search(int *array, size_t size, int value)
52+
{
53+
int index;
54+
55+
index = recursive_search(array, size, value);
56+
57+
if (index >= 0 && array[index] != value)
58+
return (-1);
59+
60+
return (index);
61+
}

0x1E-search_algorithms/100-jump.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "search_algos.h"
2+
#include <math.h>
3+
4+
/**
5+
* jump_search - searches for a value in an array of
6+
* integers using the Jump search algorithm
7+
*
8+
* @array: input array
9+
* @size: size of the array
10+
* @value: value to search in
11+
* Return: index of the number
12+
*/
13+
int jump_search(int *array, size_t size, int value)
14+
{
15+
int index, m, k, prev;
16+
17+
if (array == NULL || size == 0)
18+
return (-1);
19+
20+
m = (int)sqrt((double)size);
21+
k = 0;
22+
prev = index = 0;
23+
24+
do {
25+
printf("Value checked array[%d] = [%d]\n", index, array[index]);
26+
27+
if (array[index] == value)
28+
return (index);
29+
k++;
30+
prev = index;
31+
index = k * m;
32+
} while (index < (int)size && array[index] < value);
33+
34+
printf("Value found between indexes [%d] and [%d]\n", prev, index);
35+
36+
for (; prev <= index && prev < (int)size; prev++)
37+
{
38+
printf("Value checked array[%d] = [%d]\n", prev, array[prev]);
39+
if (array[prev] == value)
40+
return (prev);
41+
}
42+
43+
return (-1);
44+
}

0x1E-search_algorithms/101-O

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
O(sqrt(n))
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "search_algos.h"
2+
3+
/**
4+
* interpolation_search - searches for a value in an array of
5+
* integers using the Interpolation search algorithm
6+
*
7+
* @array: input array
8+
* @size: size of the array
9+
* @value: value to search in
10+
* Return: index of the number
11+
*/
12+
int interpolation_search(int *array, size_t size, int value)
13+
{
14+
size_t pos, low, high;
15+
double f;
16+
17+
if (array == NULL)
18+
return (-1);
19+
20+
low = 0;
21+
high = size - 1;
22+
23+
while (size)
24+
{
25+
f = (double)(high - low) / (array[high] - array[low]) * (value - array[low]);
26+
pos = (size_t)(low + f);
27+
printf("Value checked array[%d]", (int)pos);
28+
29+
if (pos >= size)
30+
{
31+
printf(" is out of range\n");
32+
break;
33+
}
34+
else
35+
{
36+
printf(" = [%d]\n", array[pos]);
37+
}
38+
39+
if (array[pos] == value)
40+
return ((int)pos);
41+
42+
if (array[pos] < value)
43+
low = pos + 1;
44+
else
45+
high = pos - 1;
46+
47+
if (low == high)
48+
break;
49+
}
50+
51+
return (-1);
52+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "search_algos.h"
2+
3+
/**
4+
* _binary_search - Searches for a value in a sorted array
5+
* of integers using binary search.
6+
* @array: A pointer to the first element of the array to search.
7+
* @left: The starting index of the [sub]array to search.
8+
* @right: The ending index of the [sub]array to search.
9+
* @value: The value to search for.
10+
*
11+
* Return: If the value is not present or the array is NULL, -1.
12+
* Otherwise, the index where the value is located.
13+
*
14+
* Description: Prints the [sub]array being searched after each change.
15+
*/
16+
int _binary_search(int *array, size_t left, size_t right, int value)
17+
{
18+
size_t i;
19+
20+
if (array == NULL)
21+
return (-1);
22+
23+
while (right >= left)
24+
{
25+
printf("Searching in array: ");
26+
for (i = left; i < right; i++)
27+
printf("%d, ", array[i]);
28+
printf("%d\n", array[i]);
29+
30+
i = left + (right - left) / 2;
31+
if (array[i] == value)
32+
return (i);
33+
if (array[i] > value)
34+
right = i - 1;
35+
else
36+
left = i + 1;
37+
}
38+
39+
return (-1);
40+
}
41+
42+
/**
43+
* exponential_search - Searches for a value in a sorted array
44+
* of integers using exponential search.
45+
* @array: A pointer to the first element of the array to search.
46+
* @size: The number of elements in the array.
47+
* @value: The value to search for.
48+
*
49+
* Return: If the value is not present or the array is NULL, -1.
50+
* Otherwise, the index where the value is located.
51+
*
52+
* Description: Prints a value every time it is compared in the array.
53+
*/
54+
int exponential_search(int *array, size_t size, int value)
55+
{
56+
size_t i = 0, right;
57+
58+
if (array == NULL)
59+
return (-1);
60+
61+
if (array[0] != value)
62+
{
63+
for (i = 1; i < size && array[i] <= value; i = i * 2)
64+
printf("Value checked array[%ld] = [%d]\n", i, array[i]);
65+
}
66+
67+
right = i < size ? i : size - 1;
68+
printf("Value found between indexes [%ld] and [%ld]\n", i / 2, right);
69+
return (_binary_search(array, i / 2, right, value));
70+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "search_algos.h"
2+
3+
/**
4+
* rec_search - searches for a value in an array of
5+
* integers using the Binary search algorithm
6+
*
7+
*
8+
* @array: input array
9+
* @size: size of the array
10+
* @value: value to search in
11+
* Return: index of the number
12+
*/
13+
int rec_search(int *array, size_t size, int value)
14+
{
15+
size_t half = size / 2;
16+
size_t i;
17+
18+
if (array == NULL || size == 0)
19+
return (-1);
20+
21+
printf("Searching in array");
22+
23+
for (i = 0; i < size; i++)
24+
printf("%s %d", (i == 0) ? ":" : ",", array[i]);
25+
26+
printf("\n");
27+
28+
if (half && size % 2 == 0)
29+
half--;
30+
31+
if (value == array[half])
32+
{
33+
if (half > 0)
34+
return (rec_search(array, half + 1, value));
35+
return ((int)half);
36+
}
37+
38+
if (value < array[half])
39+
return (rec_search(array, half + 1, value));
40+
41+
half++;
42+
return (rec_search(array + half, size - half, value) + half);
43+
}
44+
45+
/**
46+
* advanced_binary - calls to rec_search to return
47+
* the index of the number
48+
*
49+
* @array: input array
50+
* @size: size of the array
51+
* @value: value to search in
52+
* Return: index of the number
53+
*/
54+
int advanced_binary(int *array, size_t size, int value)
55+
{
56+
int index;
57+
58+
index = rec_search(array, size, value);
59+
60+
if (index >= 0 && array[index] != value)
61+
return (-1);
62+
63+
return (index);
64+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "search_algos.h"
2+
#include <math.h>
3+
4+
/**
5+
* jump_list - searches for a value in an array of
6+
* integers using the Jump search algorithm
7+
*
8+
* @list: input list
9+
* @size: size of the array
10+
* @value: value to search in
11+
* Return: index of the number
12+
*/
13+
listint_t *jump_list(listint_t *list, size_t size, int value)
14+
{
15+
size_t index, k, m;
16+
listint_t *prev;
17+
18+
if (list == NULL || size == 0)
19+
return (NULL);
20+
21+
m = (size_t)sqrt((double)size);
22+
index = 0;
23+
k = 0;
24+
25+
do {
26+
prev = list;
27+
k++;
28+
index = k * m;
29+
30+
while (list->next && list->index < index)
31+
list = list->next;
32+
33+
if (list->next == NULL && index != list->index)
34+
index = list->index;
35+
36+
printf("Value checked at index [%d] = [%d]\n", (int)index, list->n);
37+
38+
} while (index < size && list->next && list->n < value);
39+
40+
printf("Value found between indexes ");
41+
printf("[%d] and [%d]\n", (int)prev->index, (int)list->index);
42+
43+
for (; prev && prev->index <= list->index; prev = prev->next)
44+
{
45+
printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n);
46+
if (prev->n == value)
47+
return (prev);
48+
}
49+
50+
return (NULL);
51+
}

0 commit comments

Comments
 (0)