0% found this document useful (0 votes)
11 views

Binary Search F

The document describes binary search and linear search algorithms. It includes pseudocode for both algorithms and analyzes their time complexities. Binary search has a best, average, and worst case time complexity of O(1), O(log n), and O(log n), respectively, where n is the number of elements. Linear search has a time complexity of O(n) in all cases. The document also provides examples of output and compares the advantages and disadvantages of each search method.

Uploaded by

Aryan Shahin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Binary Search F

The document describes binary search and linear search algorithms. It includes pseudocode for both algorithms and analyzes their time complexities. Binary search has a best, average, and worst case time complexity of O(1), O(log n), and O(log n), respectively, where n is the number of elements. Linear search has a time complexity of O(n) in all cases. The document also provides examples of output and compares the advantages and disadvantages of each search method.

Uploaded by

Aryan Shahin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Report-1

Course code : CSE 232

Course Title : Algorithm

Submitted to:

Ms. Fatema Tuj Johora

Assistant Professor, Department of CSE

Daffodil International University

Submitted by:

Md. Shahin Miah

ID: 221-15-5646

Section : 61_L

Department of CSE

Daffodil International University


Binary Search Code:
int binary_search(int *list, int size, int value) {
int beg = 0;
int end = size - 1;
int pos = -1;
while (beg <= end) {
int mid = (beg + end) / 2;
if (list[mid] == value) {
pos = mid;
break;
} else if (list[mid] > value) {
end = mid - 1;
} else {
beg = mid + 1;
}
}
return pos;
}

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

Here are the advantages and disadvantages of binary search:

● 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.

Here is the time complexity analysis of binary search:

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 linear_search(int *list, int size, int value) {


int pos = -1;
for (int i = 0; i < size; i++) {
if (list[i] == value) {
pos = i;
break;
}
}
return pos;
}

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;
}

Here is the algorithm for linear search:

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:

Enter the size of the list: 5


Enter the value at index 0: 10
Enter the value at index 1: 20
Enter the value at index 2: 30
Enter the value at index 3: 40
Enter the value at index 4: 50
Enter the value to search for: 30
The value was found at index 2

Advantages of linear search:

● Simple to implement
● Efficient for small lists
● Can be used to search for any value

Disadvantages of linear search:

● Inefficient for large lists


● Can take a long time to find the value if it is not at the beginning of the list

Time complexity analysis:

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.

You might also like