In: Computer Science
C++ ONLY Binary Search and Selection Sort
A binary search first requires a sort.
Use a selection sort to organize an array, then find the value using a binary search.
Input the array, and a value to find.
Output the sorted array and the value if contained in the array.
/*
* File: main.cpp
* Author:
* Created on:
* Purpose: Binary Search
*/
//System Libraries
#include <iostream> //Input/Output Library
#include <cstdlib> //Random Functions
#include <ctime> //Time Library
using namespace std;
//User Libraries
//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e,
etc...
//Function Prototypes
void fillAry(int [],int);
void prntAry(int [],int,int);
void selSrt(int [],int);
bool binSrch(int [],int,int,int&);
//Execution Begins Here!
int main(int argc, char** argv) {
//Set the random number seed
srand(static_cast<unsigned int>(time(0)));
//Declare Variables
const int SIZE=100;
int array[SIZE];
int indx,val;
//Initialize or input i.e. set variable values
fillAry(array,SIZE);
//Sorted List
selSrt(array,SIZE);
//Display the outputs
prntAry(array,SIZE,10);
cout<<"Input the value to find in the
array"<<endl;
cin>>val;
if(binSrch(array,SIZE,val,indx))
cout<<val<<" was found at indx =
"<<indx<<endl;
//Exit stage right or left!
return 0;
}
/* * File: main.cpp * Author: * Created on: * Purpose: Binary Search */ //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> //Random Functions #include <ctime> //Time Library using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes void fillAry(int [], int); void prntAry(int [], int, int); void swap(int *, int *); void selSrt(int [], int); bool binSrch(int [], int, int, int&); //Execution Begins Here! int main(int argc, char** argv) { //Set the random number seed srand(static_cast <unsigned int> (time(0))); //Declare Variables const int SIZE = 100; int array[SIZE]; int indx = -1, val; //Initialize or input i.e. set variable values fillAry(array, SIZE); //Sorted List selSrt(array, SIZE); //Display the outputs prntAry(array, SIZE, 10); cout << "Input the value to find in the array"<< endl; cin >> val; if (binSrch(array, SIZE, val, indx)) cout << val << " was found at indx = " << indx << endl; //Exit stage right or left! return 0; } void fillAry(int arr[], int size) { for (int i = 0; i < size; i++) { arr[i] = rand()%1000; } } void prntAry(int arr[], int size, int N) { for(int i = 0; i < N; i++) { cout << arr[i] << " "; } cout << endl; } void swap(int *x, int *y) { if( *x != *y) { int temp = *x; *x = *y; *y = temp; } } bool binSrch(int arr[], int size, int val, int &indx) { int l = 0; int r = size - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == val) { indx = m; return true; } if (arr[m] < val) l = m + 1; else r = m - 1; } return false; } void selSrt(int arr[], int size) { int i, j, min_indx; for(i = 0; i < size; i++) { min_indx = i; for(j = i+1; j < size; j++) { if(arr[min_indx] > arr[j]) min_indx = j; } swap(&arr[i], &arr[min_indx]); } } /* Program ends Here*/