The searching technique in Problem 2 is called as Sequential Search or Linear Se
ID: 3849831 • Letter: T
Question
The searching technique in Problem 2 is called as Sequential Search or Linear Search. It works well for small or unsorted arrays. However, for large arrays linear searching is inefficient especially if the array is sorted. For this we use a different searching technique to find a number. This search algorithm considers a sorted array. The algorithm locates the middle element of the array and compares it to the search key. If they’re equal, the search key is found and the array subscript of that element is returned. If they’re not equal, the problem is reduced to searching onehalf of the array. If the search key is less than the middle element of the array, the first half of the array is searched, otherwise the second half is searched. If the search key is not found in the specified subarray (piece of the original array), the algorithm is repeated on one-quarter of the original array. The search continues until the search key is equal to the middle element of a subarray, or until the subarray consists of one element that’s not equal to the search key (i.e., the search key is not found).
Write a program to find whether a given number is present using a sorted array using the searching technique as mentioned above.(C PROGRAM PLEASE)
Explanation / Answer
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100];
int i, j, num, temp, findNum;
int low, mid, high;
printf("Enter the value of num of elements of array:- ");
scanf("%d", &num);
printf("Enter the elements of array ");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is... ");
for (i = 0; i < num; i++)
{
printf("%d ", array[i]);
}
printf(" Enter the element to be searched ");
scanf("%d", &findNum);
/* Binary searching begins */
low = 1;
high = num;
do
{
mid = (low + high) / 2;
if (findNum < array[mid])
high = mid - 1;
else if (findNum > array[mid])
low = mid + 1;
} while (findNum != array[mid] && low <= high);
if (findNum == array[mid])
{
printf("The search key element is found. ");
}
else
{
printf("The search key element is not found. ");
}
return 0;
}
so here is the binary search which is applied to only sorted elements.
So in the above program first i am asking user to enter the num of elements of array. then i am entering the elements of array.
After that i am using bubble sort to sort the elements of array in ascending order then i am applying the algorithm for binary search for the particular element to be searched and it follows the algorithm as
The algorithm locates the middle element of the array and compares it to the search key. If they’re equal, the search key is found and the array subscript of that element is returned. If they’re not equal, the problem is reduced to searching onehalf of the array. If the search key is less than the middle element of the array, the first half of the array is searched, otherwise the second half is searched. If the search key is not found in the specified subarray (piece of the original array), the algorithm is repeated on one-quarter of the original array. The search continues until the search key is equal to the middle element of a subarray, or until the subarray consists of one element that’s not equal to the search key (i.e., the search key is not found).