Binary Search F
Binary Search F
Submitted to:
Submitted by:
ID: 221-15-5646
Section : 61_L
Department of CSE
Here is the main function that uses the binary search function:
C
int main() {
int list[100];
int size;
int value;
printf("Enter the size of the list: ");
scanf("%d", &size);
for (int i = 0; i < size; i++) {
printf("Enter the value at index %d: ", i);
scanf("%d", &list[i]);
}
printf("Enter the value to search for: ");scanf("%d", &value);
int index = binary_search(list, size, value);
if (index != -1) {
printf("The value was found at index %d\n", index);
} else {
printf("The value was not found\n");
}
return 0;
}
Here is the output of the program when the following values are entered:
Enter the size of the list: 10
Enter the value at index 0: 1
Enter the value at index 1: 3
Enter the value at index 2: 5
Enter the value at index 3: 7
Enter the value at index 4: 9
Enter the value at index 5: 11
Enter the value at index 6: 13
Enter the value at index 7: 15
Enter the value at index 8: 17
Enter the value at index 9: 19
Enter the value to search for: 13
The value was found at index 6
● Advantages:
○ Binary search is a very efficient algorithm for searching for a value
in a sorted list.
○ The worst-case time complexity of binary search is O(log n), where
n is the number of elements in the list.
○ Binary search can be easily implemented in both recursive and
iterative form.
● Disadvantages:
○ Binary search can only be used to search for values in sorted lists.
○ If the list is not sorted, binary search will not work correctly.
○ Binary search can be slower than other algorithms for searching for
values in unsorted lists.
The worst-case time complexity of binary search is O(log n), where n is the
number of elements in the list. This is because, at each step of the algorithm, the
search space is halved. Therefore, the number of steps required to find the value
is logarithmic in the size of the list.
The best-case time complexity of binary search is O(1), which occurs when the
value to search for is the first or last element in the list. This is because, in these
cases, the algorithm will only need to make one comparison to find the value.
The average-case time complexity of binary search is O(log n). This is because,
on average, the value to search for will be located in the middle of the list.
Therefore, the algorithm will need to make log n comparisons to find the value.
Overall, binary search is a very efficient algorithm for searching for values
in sorted lists. It has a worst-case time complexity of O(log n), which is
much better than the O(n) time complexity of linear search. Binary search
can also be easily implemented in both recursive and iterative form.
Linear Search Code:
#include <stdio.h>
int main() {
int list[100];
int size;
int value;
printf("Enter the size of the list: ");
scanf("%d", &size);
for (int i = 0; i < size; i++) {
printf("Enter the value at index %d: ", i);
scanf("%d", &list[i]);
}
printf("Enter the value to search for: ");
scanf("%d", &value);
int index = linear_search(list, size, value);
if (index != -1) {
printf("The value was found at index %d\n", index);
} else {
printf("The value was not found\n");
}
return 0;
}
Linear_Search(a, n, val)
// 'a' is the given array, 'n' is the size of given array, 'val' is the val
1. set pos = -1
2. set i = 1
3. repeat step 4 while i <= n
4. if a[i] == val set pos = i print pos go to step 6 [end of if]
4. if a[i] == val set pos = i print pos go to step 6 [end of if]
5. set ii = i + 1 [end of loop]
6. if pos = -1 print "value is not present in the array " [end of if]
7. exit
Output:
● Simple to implement
● Efficient for small lists
● Can be used to search for any value
The time complexity of linear search is O(n), where n is the size of the list.
This is because the algorithm must iterate through the entire list to find the value.
In conclusion, linear search is a simple and efficient algorithm for searching for a
value in a small list. However, it is not as efficient for large lists.