This commit is contained in:
NaiJi ✨ 2024-04-22 00:14:07 +04:00
parent a8e225cbbb
commit 2a1c977954
1 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,44 @@
#include "stdio.h"
#define SIZE 4096
int binary_search(int array[], int size, int offset, int value)
{
int pivot = size / 2;
int current_value = array[offset + pivot];
printf("At %d\n", offset + pivot);
if (current_value == value)
{
return 1;
}
if (pivot > 0)
{
if (current_value > value)
return binary_search(array, pivot, offset, value);
else
return binary_search(array, pivot, offset + pivot, value);
}
return 0;
}
int main()
{
int array[SIZE];
for (int i = 0; i < SIZE; ++i)
array[i] = i;
printf("1 if found: %d\n", binary_search(array, SIZE, 0, 5243));
printf("1 if found: %d\n", binary_search(array, SIZE, 0, 1));
printf("1 if found: %d\n", binary_search(array, SIZE, 0, 1044));
// for (int i = 0; i < SIZE; ++i)
// if (!binary_search(array, SIZE, 0, i))
// printf("YOU FUCKED UP!!! at index %d\n", i);
printf("1 if found: %d\n", binary_search(array, SIZE, 0, SIZE));
array[14] = 13;
printf("1 if found: %d\n", binary_search(array, SIZE, 0, 14));
}