1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
melomori [17]
3 years ago
7

Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific

name using binary search. To do that, you need to write three functions: 1. void selSort (vector & v): sorts the vector using selection sort 2. void display (const vector & v): displays the vector contents 3. int binSearch (const vector & v, string key): searches the vector for a key, returns the index of the key if found and -1 if the index is not found
Computers and Technology
1 answer:
Natalka [10]3 years ago
7 0

Answer:

See Explaination

Explanation:

#include <iostream>

#include <iomanip>

#include <vector>

using namespace std;

// Function Declarations

void display(vector<string> names);

void selectionSort(vector<string>& names);

int binarySearch(vector<string> names, string name);

int main()

{

string search;

vector<string> names{ "Joe Garcia", "Amelia Perez", "Bob Haynes", "Tim Ducrest", "Kevin Garcia", "Suzy Walter", "Fang Yi", "Robert James", "Mary Andres" };

cout << "__Displaying names before sort__" << endl;

display(names);

selectionSort(names);

cout << "__Displaying names after sort__" << endl;

display(names);

search = "Kevin Garcia";

int b1 = binarySearch(names, search);

if (b1 != -1) {

cout << search << " is found in the vector" << endl;

}

else {

cout << search << " is not found in the vector" << endl;

}

search = "Joe James";

b1 = binarySearch(names, search);

if (b1 != -1) {

cout << search << " is found in the vector" << endl;

}

else {

cout << search << " is not found in the vector" << endl;

}

return 0;

}

//This function will the Vector elements

void display(vector<string> names)

{

for (int i = 0; i < names.size(); i++) {

cout << names[i] << endl;

}

}

//This function will sort the Vector in ascending order

void selectionSort(vector<string>& names)

{

int start;

string temp;

for (int i = names.size() - 1; i > 0; --i) {

start = 0;

for (int j = 1; j <= i; ++j) {

if (names[j].compare(names[start]) > 0) {

start = j;

}

temp = names[start];

names[start] = names[i];

names[i] = temp;

}

}

}

//This function will search for a string in vector

int binarySearch(vector<string> names, string name)

{

int first = 0,

last = names.size() - 1,

middle,

position = -1;

bool found = false;

while (!found && first <= last)

{

middle = (first + last) / 2;

if (names[middle] == name)

{

found = true;

position = middle;

}

else if (names[middle] > name)

last = middle - 1;

else

first = middle + 1;

}

return position;

}

You might be interested in
Why do i keep losing points in brainly.com and how do you lose it?
kati45 [8]

Answer:

because you are asking questions like this

Explanation:

when you ask questions you lose points

6 0
3 years ago
Read 2 more answers
Before you enter an intersection on a green light make sure
mr Goodwill [35]

Answer:

there is no pedestrians and everyone is stopped at their designated red light

5 0
3 years ago
Read 2 more answers
Why do we use console.log in javascript when changing variables?
Fiesta28 [93]
It helps to debug the code. Instead of going through every line of code to find an error, using console.log can tell us where to look.
8 0
3 years ago
Read 2 more answers
Computer virus is a<br> a. software<br> b. hardware<br> c. bacteria<br> d. none of these
Nimfa-mama [501]
The answer is software
8 0
4 years ago
How many times go you need to click the format painter button to apply copied formats to multiple paragraphs one right after the
Umnica [9.8K]
Double click would be enough
6 0
3 years ago
Read 2 more answers
Other questions:
  • Which layout manager constructor call would be best-suited to create a telephone handset GUI which has three rows of three keys
    12·1 answer
  • Damage to which portion of the limbic system results in loss of memory of recent events and difficulty committing anything new t
    13·1 answer
  • Hidden-surface removal is the process of:
    14·1 answer
  • What is an undirected graph?
    13·1 answer
  • Tightly.
    5·1 answer
  • How do you send friend requests?
    8·1 answer
  • True or False: the sky looks blue because it is reflecting the blue ocean.​
    12·2 answers
  • Which of the following You tubers uses the word "flip" as a curse word
    8·2 answers
  • Describing Work Styles for Farmworkers and Laborers, Crop
    6·2 answers
  • In java language I want the code
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!