Modify the following program to implement the selection sort algorithm, which wi
ID: 3622283 • Letter: M
Question
Modify the following program to implement the selection sort algorithm, which will sort a listof strings. Your function should sort the list of strings in alphabetical order. You only need to
provide the details for the selectionSort() function based on the following framework.
The printItems() and main() function will not need to change.
SelectionSort.cpp:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void selectionSort(string items[], int numberOfItems)
{
//
// Hints:
// (1) You will need two for loops.
//
// (2) Use the strcmp() function in your if-statement
// to determine when one string is less than another.
// You will need to call this function with the c_str()
// method. For example:
// strcmp(items[i].c_str(), items[j].c_str());
//
// (3) You will need a temporary string when swapping
// the two strings.
Page 2 of 3
CS 3330
Dr. Davault
//
return;
}
// Print the items in the list.
void printItems(string items[], int numberOfItems)
{
for (int i=0; i < numberOfItems; i++)
{
cout << items[i] << endl;
}
cout << endl;
return;
}
int main(int argc, char **argv)
{
string starTrekCharacters[] = {
"Picard", "Riker", "Data", "La Forge", "Worf", "Dr. Crusher",
"Dr. Pulaski", "Wesley", "Troi", "Tasha", "Sisko", "Odo",
"Dax", "O'Brien", "Quark", "Dr. Bashier", "Kira", "B'Elanna",
"Chakotay", "Janeway", "Neelix", "Seven of Nine", "Tuvok",
"Doctor", "Harry", "Tom", "Kes", "Archer", "T'Pol", "Tucker",
"Reed", "Travis", "Hoshi", "Dr. Phlox", "Kirk", "Spock",
"Bones", "Scotty", "Chekov", "Uhura", "Sulu", "Nurse Chapel"
};
int numberOfCharacters = 41;
// Print the unsorted items
cout << "Items unsorted:" << endl;
printItems(starTrekCharacters, numberOfCharacters);
// Sort the items
selectionSort(starTrekCharacters, numberOfCharacters);
// Print the sorted items
cout << "Items sorted:" << endl;
printItems(starTrekCharacters, numberOfCharacters);
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void selectionSort(string items[], int numberOfItems)
{
int startScan, minIndex;
string strName;
for(startScan = 0; startScan < (numberOfItems - 1); startScan++)
{
minIndex = startScan;
strName = items[startScan];
for(int index = startScan + 1; index < numberOfItems; index++)
{
if(strcmp(items[index].c_str(),strName.c_str())<0)
{
strName = items[index];
minIndex = index;
}
}
items[minIndex] = items[startScan];
items[startScan] = strName;
}
return;
}
//
// Hints:
// (1) You will need two for loops.
//
// (2) Use the strcmp() function in your if-statement
// to determine when one string is less than another.
// You will need to call this function with the c_str()
// method. For example:
// strcmp(items[i].c_str(), items[j].c_str());
//
// (3) You will need a temporary string when swapping
// the two strings.
// Print the items in the list.
void printItems(string items[], int numberOfItems)
{
for (int i=0; i < numberOfItems; i++)
{
cout << items[i] << endl;
}
cout << endl;
return;
}
int main(int argc, char **argv)
{
string starTrekCharacters[] = {
"Picard", "Riker", "Data", "La Forge", "Worf", "Dr. Crusher",
"Dr. Pulaski", "Wesley", "Troi", "Tasha", "Sisko", "Odo",
"Dax", "O'Brien", "Quark", "Dr. Bashier", "Kira", "B'Elanna",
"Chakotay", "Janeway", "Neelix", "Seven of Nine", "Tuvok",
"Doctor", "Harry", "Tom", "Kes", "Archer", "T'Pol", "Tucker",
"Reed", "Travis", "Hoshi", "Dr. Phlox", "Kirk", "Spock",
"Bones", "Scotty", "Chekov", "Uhura", "Sulu", "Nurse Chapel"
};
int numberOfCharacters = 41;
// Print the unsorted items
cout << "Items unsorted:" << endl;
printItems(starTrekCharacters, numberOfCharacters);
// Sort the items
selectionSort(starTrekCharacters, numberOfCharacters);
// Print the sorted items
cout << "Items sorted:" << endl;
printItems(starTrekCharacters, numberOfCharacters);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.