Answer:
Explanation:
The following code is written in C++ and automatically implements the 15 element array. Then sorts it using selection sort. And finally implements a function that takes an element as a parameter and searches/returns that element using binary search.
#include<iostream>
using namespace std;
int binarySearch(int arr[], int p, int r, int num) {
if (p <= r) {
int mid = (p + r)/2;
if (arr[mid] == num)
return mid ;
if (arr[mid] > num)
return binarySearch(arr, p, mid-1, num);
if (arr[mid] > num)
return binarySearch(arr, mid+1, r, num);
}
return -1;
}
int main() {
int array[15] = {10, 20, 30, 31, 35, 15, 5, 9, 17, 22, 51, 55, 96, 42, 63};
int size = 15;
int i, j, imin;
for(i = 0; i<size-1; i++) {
imin = i;
for(j = i+1; j<size; j++)
if(array[j] < array[imin])
imin = j;
swap(array[i], array[imin]);
}
int chosenNumber = 31; //Any number that you want to search for
int numberIndex = binarySearch (array, 0, n-1, num);
}