Question

***PLEASE USE C++ LANGUAGE*** Binary Search of Strings 1. Write a version of the selection sort...

***PLEASE USE C++ LANGUAGE***

Binary Search of Strings

1. Write a version of the selection sort algorithm presented in the unit, which is used to search a list of strings.

2. Write a version of the binary search algorithm presented in the unit, which is used to search a list of strings. (Use the selection sort that you designed above to sort the list of strings.)

3. Create a test program that primes the list with a set of strings, sorts the list, and then prompts the user to enter a search string. Your program should then search the list using your binary search algorithm to determine if the string is in the list. Allow the user to continue to search for strings until they choose to exit the program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) Sorting a list of strings using selection sort algorithm

Program

//#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

//Constant globals
const int NUM_NAMES = 20;

//Function protoypes
void selectionSort(string [], int);


int main()
{
string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allet, Jim",
"Griffin, Jim", "Stamey, Marty", "Rose, Geri",
"Taylor, Terri", "Johnson, Jill",
"Aliison, Jeff", "Weaver, Jim", "Pore, Bob",
"Rutherford, Greg", "Javens, Renee",
"Harrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth"};


int i;

//Show array
cout << "The unsorted values are\n";
//showArray(names, NUM_NAMES);
for (i=0;i<NUM_NAMES;i++)
cout<<names[i]<<endl;
  
//Sort array
selectionSort(names, NUM_NAMES);

  
  
return 0;
}

void selectionSort(string array[], int NUM_NAMES)
{
int startScan, minIndex,i;
string minValue;

for(startScan = 0; startScan < (NUM_NAMES -1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan +1; index < NUM_NAMES; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
//Display sorted array
cout << "\nThe sorted values are\n";
  
for (i=0;i<NUM_NAMES;i++)
cout<<array[i]<<endl;
}

Output

The unsorted values are Collins, Bill Smith, Bart Allet, Jim Griffin, Jim Stamey, Marty Rose, Geri Taylor, Terri ohnson, Jill

The sorted values are Aliison, Jeff Allet, Jim Collins, Bill Griffin, Jim Harrison, Rose Holland, Beth Javens, Renee ohnson,

2) Binary Search on Strings

Binary search is performed on sorted arrays. Here we use selection sort to sort input array.

//#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

//Constant globals
const int NUM_NAMES = 20;

//Function protoypes
void selectionSort(string [], int);

int binarySearch(string [], int, string);
int main()
{
string names[NUM_NAMES] = {"Collin", "Smith", "Allet",
"Griffin", "Stamey", "Rose",
"Taylor", "Johnson",
"Aliison", "Weaver", "Pore",
"Rutherford", "Javens",
"Harrison", "Setzer",
"Pike", "Holland"};


int i,results;
string key;

//Show array
cout << "The unsorted values are\n";
//showArray(names, NUM_NAMES);
for (i=0;i<NUM_NAMES;i++)
cout<<names[i]<<endl;
  
//Sort array
selectionSort(names, NUM_NAMES);

cout << "\nPlease enter search string: ";
getline(cin, key);

// Search for name
results = binarySearch(names, NUM_NAMES, key);

// If results contains -1 the name was not found.
if (results == -1)
cout << "\nThat name does not exist in the array.\n";
else
{
// Otherwise results contains the subscript of
// the specified employee ID in the array.
cout << "\nThat name is found at element " << results;
cout << " in the array.\n";
}
  
return 0;
}

void selectionSort(string array[], int NUM_NAMES)
{
int startScan, minIndex,i;
string minValue;

for(startScan = 0; startScan < (NUM_NAMES -1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan +1; index < NUM_NAMES; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}

  
}
int binarySearch(string names[], int size, string value)
{
int first = 0, // First array element
last = size - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag

while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (names[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (names[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
}

Output

The unsorted values are Collin Smith Allet Griffir Stamey Rose Taylor Johnson Aliisor Weaver Pore Rutherford Javens Harrison

Program 3

//#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

//Constant globals
const int NUM_NAMES = 20;

//Function protoypes
void selectionSort(string [], int);

int binarySearch(string [], int, string);
int main()
{
string names[NUM_NAMES] = {"Collin", "Smith", "Allet",
"Griffin", "Stamey", "Rose",
"Taylor", "Johnson",
"Aliison", "Weaver", "Pore",
"Rutherford", "Javens",
"Harrison", "Setzer",
"Pike", "Holland"};


int i,results;
string key;
char again;

//Show array
cout << "The unsorted values are\n";
//showArray(names, NUM_NAMES);
for (i=0;i<NUM_NAMES;i++)
cout<<names[i]<<endl;
  
//Sort array
selectionSort(names, NUM_NAMES);
do
{
cout << "\nPlease enter search string: ";
getline(cin, key);

// Search for name
results = binarySearch(names, NUM_NAMES, key);

// If results contains -1 the name was not found.
if (results == -1)
cout << "\nThat name does not exist in the array.\n";
else
{
// Otherwise results contains the subscript of
// the specified employee ID in the array.
cout << "\nThat name is found at element " << results;
cout << " in the array.\n";
}
//Run program again?
cout << "Would you like to search another string? (Y/N): ";
cin >> again;
}while(again == 'y' || again == 'Y');
  
return 0;
}

void selectionSort(string array[], int NUM_NAMES)
{
int startScan, minIndex,i;
string minValue;

for(startScan = 0; startScan < (NUM_NAMES -1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan +1; index < NUM_NAMES; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}

  
}
int binarySearch(string names[], int size, string value)
{
int first = 0, // First array element
last = size - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag

while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (names[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (names[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
}

Add a comment
Know the answer?
Add Answer to:
***PLEASE USE C++ LANGUAGE*** Binary Search of Strings 1. Write a version of the selection sort...
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
  • 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...

  • Please Use C++ Language. And Note that please donot post the answer already there Because there...

    Please Use C++ Language. And Note that please donot post the answer already there Because there is similar answer code but I need with modified Quick sort algorithm ​. Update your program from ... Create an array that holds 1000 random integers between 1-1000. Allow the user to enter an integer to search. Create and implement modified Quick sort algorithm which will sort the array before the Binary Search algorithm is executed. Create and implement a Binary Search Algorithm ....

  • (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...

  • Language = c++ Write a program to find the number of comparisons using the binary search...

    Language = c++ Write a program to find the number of comparisons using the binary search and sequential search algorithms as follows: o Suppose list is an array of 1000 elements. o Use a random number generator to fill the list. o Use the function insertOrd to initially insert all the elements in the list. o You may use the following function to fill the list: void fill(orderedArrayListType& list) {       int seed = 47; int multiplier = 2743;                                ...

  • Write a program that meets the following criteria: Define a function that implements the selection sort...

    Write a program that meets the following criteria: Define a function that implements the selection sort algorithm to sort an integer array. Define a function that implements the binary search algorithm to search an array for a given integer. The function must return the location of the integer if it is found or a -1 if it is not found. The main function in your program must declare an integer array initialized with 10 unsorted values. The main function must...

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • Write and test a function in C++ that uses the binary search algorithm to search an...

    Write and test a function in C++ that uses the binary search algorithm to search an array of sorted strings – use a do..while loop to allow user to perform multiple searches w/o terminating the program – see sample output below. Use this name array: string names[SIZE] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",         "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",         "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",         "James, Jean", "Weaver, Jim", "Pore, Bob",...

  • C++ SORTING – BUBBLE SORT METHOD Use the below Text File and write code that sorts...

    C++ SORTING – BUBBLE SORT METHOD Use the below Text File and write code that sorts it based on the users sort method selection. Please provide a separate .cpp file wit hthe code containing the sort method. The sorting method uses the text file below and sorts it accordingly. Seperate the sorting method into an additional C++ file. ********************************* Text File Below is a text file called classes.txt. This text file lists, by course number and section number a series...

  • C# prograaming language 1. write a program that generates 10,000 random integers in the range of ...

    c# prograaming language 1. write a program that generates 10,000 random integers in the range of 0-9 and them in binary search tree. 2. use any sort algorithm (insertion, bubble, quick...) to display a list of each of the integers and how many times they appear. 3. at last, add ruction to the BinarySearchTree class that count the number of edges in a tree. Write a program that generates 10,000 random integers in the range of 0-9 and store them...

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