A binary search is much more efficient than a selection sear
A binary search is much more efficient than a selection search. In the worst case, a selection sort will have to compare the query value to all other values in the array. For an array with 256 elements, the worst case is 256 comparisons. This is referred to as an complexity because the number of comparisons grows linearly with the data. This worst case occurs if the query value matches the last element in the array or if the query value is not in the array. A binary search repeatedly cuts the elements in the array in half on a middle element, calculated as (end-begin)/2, where end is the last element under consideration and begin is the first.
Because the data in the array are sorted, there are 3 cases: the query value is equal to the middle value (match found), the query value is less than the middle value (check values before mid) or the query value is greater than the middle value (check values after mid). In the worst case this search will require log comparisons or only 8 comparisons for an array containing 256 elements (log 256 = 8). This is a log complexity. First start by writing a function that checks to see if an array is sorted. Then write a function that checks, and if the array is not sorted, sort it. Thhe last step is to code the binary search as described below.
// begin is mid+1
// If query value is less than mid value
This is C. Thank you so much
Solution
// C code to sort the array and find element in array using binary search
#include <stdio.h>
#include <stdbool.h>
void swap(int *x, int *y)
{
int t = *x;
*x = *y;
*y = t;
}
// sort the array
void sort(int array[], int size)
{
int i, j;
for (i = 0; i < size-1; i++)
for (j = 0; j < size-i-1; j++)
if (array[j] > array[j+1])
swap(&array[j], &array[j+1]);
}
// check if array is sorted
bool issorted(int array[], int size)
{
int i, j;
for (i = 0; i < size-1; i++)
for (j = 0; j < size-i-1; j++)
if (array[j] > array[j+1])
return false;
return true;
}
int binarySearch(int array[], int begin, int end, int target)
{
// While begin is less than end
while (begin <= end)
{
int m = begin + (end-begin)/2;
// If query value is greater than mid value
if ( target < array[m])
end = m - 1;
// If query value is greater than mid value
if ( target > array[m])
begin = m + 1;
// If query value is equal to mid value
if (array[m] == target)
return m;
}
// if we reach here, then element was not present
return -1;
}
int main()
{
int size;
printf(\"Enter size of array: \");
scanf(\"%d\",&size);
int array[size];
int i;
for (i = 0; i < size; ++i)
{
printf(\"Enter array[%d]: \",i);
scanf(\"%d\",&array[i]);
}
// check if sorted or not, sort if needed
if(issorted(array,size) == false)
sort(array,size);
printf(\"\ Sorted array: \");
for (i = 0; i < size; ++i)
{
printf(\"%d \", array[i]);
}
printf(\"\ \");
int search;
printf(\"\ Enter an element to search: \");
scanf(\"%d\",&search);
int result = binarySearch(array,0,size-1,search);
if(result == -1)
printf(\"Not found in array\ \");
else
printf(\"Found, Position: %d\ \",(result+1));
return 0;
}
/*
output:
Enter size of array: 5
Enter array[0]: 3
Enter array[1]: 1
Enter array[2]: 5
Enter array[3]: 4
Enter array[4]: 2
Sorted array: 1 2 3 4 5
Enter an element to search: -1
Not found in array
Enter size of array: 5
Enter array[0]: 3
Enter array[1]: 9
Enter array[2]: 0
Enter array[3]: 1
Enter array[4]: 2
Sorted array: 0 1 2 3 9
Enter an element to search: 3
Found, Position: 4
*/


