I'm trying to understand how selection sorting works with strings.
This is what I have so far:
#include <iostream>
#include <string>
using namespace std;
// Prototypes
void selectionSort(string arr[], int size);
void showArray(string arr[], int size);
int main() {
const int NUM_NAMES = 20;
string names[NUM_NAMES] = {"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", "Rutherford, Greg",
"Javens, Renee", "Harrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth"};
// Insert your code to complete this program
cout << "The names on the list in no particlular order are: ";
showArray(names, NUM_NAMES);
// Calling the sorted array
selectionSort(names, NUM_NAMES);
// Displaying sorted array
cout << "The names in sorted order are: ";
showArray(names, NUM_NAMES);
return 0;
}
// Function to sort the string
void selectionSort(string arr[], int size) {
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++) {
minIndex = startScan;
minValue = arr[startScan];
for (int index = (startScan + 1); index < size; index++) {
if (arr[index] < minValue) {
minValue = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minValue;
}
}
// Function to display the array's conents
void showArray(const int arr[], int size) {
for (int count = 0; count < size; count++) {
cout << arr[count];
cout << "\t\n";
}
}
If I try building it it fails and I get an error message. "No matching function for call to 'selectionSort'."
However, if I try and use ints instead of strings I'm able to get it to compile and sort properly.
#include <iostream>
#include <string>
using namespace std;
// Prototypes
void selectionSort(int [], int);
void showArray(const int [], int);
int main() {
const int NUM_NAMES = 8;
int names[NUM_NAMES] = {1,3,5,7,3,5,7,9};
// Insert your code to complete this program
cout << "The names on the list in no particular order are: ";
showArray(names, NUM_NAMES);
// Calling the sorted array
selectionSort(names, NUM_NAMES);
// Displaying sorted array
cout << "The names in sorted order are: ";
showArray(names, NUM_NAMES);
return 0;
}
// Function to sort the string
void selectionSort(int array[], int size) {
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++) {
minIndex = startScan;
minValue = array[startScan];
for (int index = (startScan + 1); index < size; index++) {
if (array[index] < minValue) {
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
// Function to display the array's contents
void showArray(const int array[], int size) {
for (int count = 0; count < size; count++) {
cout << array[count];
cout << "\t\n";
}
}
I'm not quite sure where I'm going wrong with the string sorting. Image of c++ program with the portion that has an issue
The compile error is trivial. void selectionSort(int [], int);
accepts array of integers, it sorts that array. But you want to sort array of strings. You have declared:
string names[NUM_NAMES];
You have to change the sort function to accept array of strings. The modification is rather easy since std::string
supports =
, <
, >
, ==
operators.
void selectionSort(string arr[], int size)
{
int startScan, minIndex;
string minValue;
for(startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = arr[startScan];
for(int index = (startScan + 1); index < size; index++)
{
if(arr[index] < minValue)
{
minValue = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minValue;
}
}