Question

In: Computer Science

C++ ONLY Binary Search and Selection Sort A binary search first requires a sort. Use a...

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;
}

Solutions

Expert Solution


/*
* 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*/


Related Solutions

This question has three parts: a) binary search, b) selection sort, and c) merge sort. a)...
This question has three parts: a) binary search, b) selection sort, and c) merge sort. a) Suppose we are performing a binary search on a sorted list called numbers initialized as follows: #index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 numbers = [-2, 0, 1, 7, 9, 16, 19, 28, 31, 40, 52, 68, 85, 99] #search for the value 5 index = binary_search(numbers, 5) Write the indexes of the elements that would...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
I would like to integrate a bubble sort into this binary search in c ++ Thank...
I would like to integrate a bubble sort into this binary search in c ++ Thank you! #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) {         while (f <= l)         {             int m = f + (l - l) / 2;                 // Check if...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND BUBBLE SORT FUNCTION TO THIS PROGRAM #include <iostream> #include<vector> #include <algorithm >   #include <chrono>    #include <ctime> using namespace std; void bubblesSort() { // Please create Bubble Sort function// Make another for Selection Sort and  Insertion Sort } int main() { // empty vector vector<int> data; // data [0], data [1]... data[N-1] <-- end(data) // set of values to test N for (auto N :...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations,...
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations, Insertion Sort – implement algorithms Permutations (Johnson Trotter): {1, 2, 3, 4, 5}; Insertion Sort: {45, 24, 16, 92, 71, 69, 28} develop count of # data “insertions” and develop # of key compares against the following: 16, 77, 24, 92, 44
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Binary Search Tree (Part 1): Note: This is the first part of a 2-part lab. Only...
Binary Search Tree (Part 1): Note: This is the first part of a 2-part lab. Only this part is due on Oct 29. But please get it working or you will struggle with the second part. For the second part, the data type will be changing. For now, it is a string. Contents Explanation: 2 BST.cpp: 2 Code you must write: 2 bool insert(string s) (7): 2 TNode *find(string s) (4): 2 void printTreeIO(Tnode *n)(3): 2 void printTreePre(Tnode *n) (3):...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort,...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort, or bubble sort) and one faster sort of Big O(n * log n) (mergesort or quicksort). Count the number of moves (a swap counts as one move). With mergesort, you can count the range of the part of the array you are sorting (i.e. last-first+1). Use the code from the textbook (copy from the lecture notes) and put in an extra reference parameter for...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree sort), will take the following command line arguments: % bstsort [-c] [-o output_file_name] [input_file_name] If -c is present, the program needs to compare the strings case sensitive; otherwise, it's case insensitive. If the output_file_name is given with the -o option,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT