Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <string> using namespace std; // Function Prototype

ID: 663851 • Letter: #

Question

#include <iostream>

#include <string>

using namespace std;

// Function Prototype

void selectionSort(string[], int);

int main()

{

  

// Define the array with unsorted values

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", };

  

// Display the names

cout << "THE NAMES IN THE ELEMENTS ARE: " << endl;

for (int index = 0; index < NUM_NAMES; index++)

cout << names[index] << endl;

cout << endl;

  

  

// sort the names in order

selectionSort(names, NUM_NAMES);

cout << "AFTER THE SELECTION SORT: " << endl;

for (int index = 0; index < NUM_NAMES; index++)

cout << names[index] << endl;

cout << endl;

return 0;

}

// Definition of function selectionSort.

void selectionSort (string names[], int size)

{

int startScan;

int minIndex;

string minValue;

  

for(startScan = 0; startScan < (size - 1); startScan++)

{

minValue = startScan;

minValue = names[startScan];

  

for(int index = startScan + 1; index < size; index++)

{

if(names[index] < minValue)

  

{

minValue = names[index];

minIndex = index;

  

}

  

}

  

  

names[minIndex] = names[startScan];

names[startScan] = minValue;

  

  

}

  

}

QUESTION: The output of this program is displaying the name Collins and Rutherford twice. How can we fix it?

Explanation / Answer

//In the iteration when Collins needs to be at it's correct position, it is already at it's correct position.
       //Initialize minIndex to starting index so that if nothing is changed, nothing will be swapped. Otherwise it will take minIndex of last iteration and swap it.

#include "stdafx.h"

#include <iostream>
#include <string>

using namespace std;

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

int main()
{

   // Define the array with unsorted values
   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", };

   // Display the names
   cout << "THE NAMES IN THE ELEMENTS ARE: " << endl;
   for (int index = 0; index < NUM_NAMES; index++)
       cout << names[index] << endl;
   cout << endl;


   // sort the names in order
   selectionSort(names, NUM_NAMES);
   cout << "AFTER THE SELECTION SORT: " << endl;
   for (int index = 0; index < NUM_NAMES; index++)
       cout << names[index] << endl;
   cout << endl;
   system("pause");
   return 0;
}

// Definition of function selectionSort.

void selectionSort(string names[], int size)
{
   int startScan;
   int minIndex;
   string minValue;

   for (startScan = 0; startScan < (size - 1); startScan++)
   {
       minValue = startScan;
       minValue = names[startScan];
       minIndex = startScan; //In the iteration when Collins needs to be at it's correct position, it is already at it's correct position.
       //Initialize minIndex to starting index so that if nothing is changed, nothing will be swapped. Otherwise it will take minIndex of last iteration and swap it.

       for (int index = startScan + 1; index < size; index++)
       {
           if (names[index] < minValue)

           {
               minValue = names[index];
               minIndex = index;

           }

       }


       names[minIndex] = names[startScan];
       names[startScan] = minValue;


   }

}