WAP to perform binary and linear search on a list of integers given below,to search for an element input by the user,if it is found display the element along with its position, otherwise display the msg "search element not found".
Binary search elements in sorted order - 5,7,9,11,15,20,30,45,89,97
linear search elements in unsorted order - 45,5,97,11,30,7,20,7,15,5
Answers
Answered by
5
#include <stdio.h>
int main() {
int array[100], search, c, n;
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
printf("Enter a number to search\n"); scanf("%d", &search);
for (c = 0; c < n; c++) {
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1); break;
}
}
return 0;
}
OUTPUT
Enter the number of elements in array.
5
Enter 5 integers.
32 34 56 10 76
Enter a number to search.
10
10 is present at location 4.
Thanks
int main() {
int array[100], search, c, n;
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
printf("Enter a number to search\n"); scanf("%d", &search);
for (c = 0; c < n; c++) {
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1); break;
}
}
return 0;
}
OUTPUT
Enter the number of elements in array.
5
Enter 5 integers.
32 34 56 10 76
Enter a number to search.
10
10 is present at location 4.
Thanks
shrujana1630:
thank you
Similar questions