Question
in c++
Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector f
- 0 and 2 -8 and 0 Program 3: Write a recursive function mamChars that counts the number of times a specific character appear
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

//C++ program to sort vector of strings alphabetically using selection sort in ascending order

#include <iostream>

#include <vector>

#include <string>

using namespace std;

// function declaration

void selSort(vector<string> &v);

void display(const vector<string> &v);

int binSearch(const vector<string> &v, string key);

int main() {

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

               cout<<"Names : "<<endl;

               display(names);

               selSort(names); // sort the vector in ascending order

               cout<<"\nSorted Names : "<<endl;

               display(names);

               cout<<"\nSearch for Kevin Garcia : "<<binSearch(names,"Kevin Garcia")<<endl;

               cout<<"\nSearch for Joe James : "<<binSearch(names,"Joe James")<<endl;

               return 0;

}

// sorts the vector using selection sort

void selSort(vector<string> &v)

{

               size_t min;

               for(size_t i=0;i<v.size()-1;i++)

               {

                              min = i;

                              for(size_t j=i+1;j<v.size();j++)

                              {

                                             if(v[j].compare(v[min]) <0)

                                                            min = j;

                              }

                              if(min != i)

                              {

                                             string temp = v[i];

                                             v[i] = v[min];

                                             v[min] = temp;

                              }

               }

}

// displays the vector contents

void display(const vector<string> &v)

{

               for(size_t i=0;i<v.size();i++)

                              cout<<v[i]<<endl;

}

// searches the vector for a key returns the index of the key if found, -1 if the index is not found

int binSearch(const vector<string> &v, string key)

{

               int low = 0, high = v.size()-1;

               int mid;

               while(low <=high)

               {

                              mid = (low+high)/2;

                              if(v[mid].compare(key) == 0)

                                             return mid;

                              else if(v[mid].compare(key) > 0)

                                             high = mid-1;

                              else

                                             low = mid+1;

               }

               return -1;

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be written in C++ Display results and comments Write a program that sorts a vector...

    Must be written in C++ Display results and comments 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 <string> & v): sorts the vector using selection sort 2. void display (const vector <string> & v): displays the vector contents 3. int binSearch (const vector <string> & v, string key): searches...

  • Write a program with a function that accepts a string as an argument and returns a...

    Write a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. For instance, if the argument is “hello. my name is Joe. what is your name?” the function should return the string “Hello. My name is Joe. What is your name?” The program should let the user enter a string and then pass it to the function. The modified string should be displayed.

  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

  • (Use C programming language) 1-)Write a program that • Sorts a given integer array via Selection...

    (Use C programming language) 1-)Write a program that • Sorts a given integer array via Selection Sort • Rotates the sorted array around a random point, e.g., 1 2 3 4 5 à 3 4 5 1 2 is obtained by rotation around 3. • Searches for a key point in the rotated array in O(logn) time, where the rotation point is known in advance. • Repeats above by first finding the unknown rotation point via sequential search and binary...

  • Using c++ 1 of 2 Assignment 5 Lab Section 3 write a program create a vector...

    Using c++ 1 of 2 Assignment 5 Lab Section 3 write a program create a vector with random numbers. Use merge sort to reorder the vector Prompt user to enter a number to search in the vector. If there are more than one number in the vector equal to the search value, display the indices remove the repeated numbers. If found just one matching number, display the index. If no matching number, prompt user for 2 options: add to the...

  • *MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a loop,...

    *MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a loop, not the built-in sort function). 4)write a function that will receive a vector and will return two index vectors: one for ascending order and one for descending order. Check the function by writing a script that will call the function and then use the index vectors to print the original vector in ascending and descending order. **Please make sure they work. I have found...

  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

  • SortAndSearch.cpp – Objectives: Binary Search and Selection sort You are given a list of 20 names...

    SortAndSearch.cpp – Objectives: Binary Search and Selection sort You are given a list of 20 names as follows: {"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, Jim",                         "Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill",                         "Allison, Jeff", "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",                         "Moretti, Bella", "Wu, Hong", "Patel, Renee", "Harrison, Rose",                   "Smith, Cathy", "Conroy, Pat", "Kelly, Sean", "Holland, Beth"}; Write a program to sort and display the names in alphabet order (use selection sort). The program prompts...

  • Write a main function that declares the names, marks, number of elements as well as the...

    Write a main function that declares the names, marks, number of elements as well as the value to be searched for and the index of the returned function calls. Read and display the contents of names and marks. Ask the user for a name and using the linear search return the index to the user. If -1 is returned then the name is not in the file. Otherwise write out the name and mark for that student. Sort the arrays...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT