Skip to content

Commit

Permalink
Format C code using CLANG_FORMAT
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvins committed Oct 23, 2024
1 parent b5c0e1f commit 2a55493
Show file tree
Hide file tree
Showing 46 changed files with 2,589 additions and 2,584 deletions.
56 changes: 28 additions & 28 deletions src/c/BinarySearch.c
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
#include<stdio.h>
#include <stdio.h>

int BinarySearch(int array[], int size, int value) {
int start = 0;
int end = size - 1;
int middle = end / 2;
int start = 0;
int end = size - 1;
int middle = end / 2;

while (start < end && array[middle] != value) {
// new start
if (value > array[middle])
start = middle + 1;
while (start < end && array[middle] != value) {
// new start
if (value > array[middle])
start = middle + 1;

// new end
if (value < array[middle])
end = middle - 1;
// new end
if (value < array[middle])
end = middle - 1;

// new middle
middle = (start + end) / 2;
}
// new middle
middle = (start + end) / 2;
}

if (array[middle] == value)
return middle;
if (array[middle] == value)
return middle;

return -1;
return -1;
}

int main() {
int value;
int array[] = {1, 5, 10, 12, 18, 22, 87, 90, 112, 129};
size_t size = sizeof(array) / sizeof(array[0]);
int value;
int array[] = {1, 5, 10, 12, 18, 22, 87, 90, 112, 129};
size_t size = sizeof(array) / sizeof(array[0]);

printf("Please provide the number you want to value for: ");
scanf("%d", &value);
printf("Please provide the number you want to value for: ");
scanf("%d", &value);

int pos = BinarySearch(array, size, value);
int pos = BinarySearch(array, size, value);

if (pos != -1)
printf("Found in position = %d.\nValue = %d\n", pos, array[pos]);
else
printf("Not found\n");
if (pos != -1)
printf("Found in position = %d.\nValue = %d\n", pos, array[pos]);
else
printf("Not found\n");

return 0;
return 0;
}
Loading

0 comments on commit 2a55493

Please sign in to comment.