-
Notifications
You must be signed in to change notification settings - Fork 268
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
Showing
46 changed files
with
2,589 additions
and
2,584 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 |
---|---|---|
@@ -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; | ||
} |
Oops, something went wrong.