Corey Starbird
Corey Starbird

Reputation: 73

C++ Sorting characters rather than entire strings

void selectionSort(string [], int);
void showArray(string [], int);

int main()
{
   const int SIZE = 20;

   string name[SIZE] = 
   {"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"};

    // Show initial order
    cout << "The unsorted values are\n";
    showArray(name, SIZE);

    // Sort the strings
    selectionSort(name, SIZE);

    // Show ordered order
    cout << "The sorted values are\n";
    showArray(name, SIZE);



    return 0;
}




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

        for (startScan = 0; startScan < (size - 1); startScan++)
        {
            minIndex = startScan;
            minValue = name[startScan].at(0);
            for(int index = startScan + 1; index < size; index++)
            {
                if (name[index].at(0) < minValue)
                {
                    minValue = name[index].at(0);
                    minIndex = index;
                }
            }
            name[minIndex] = name[startScan];
            name[startScan].at(0) = minValue;
        }
    }


    void showArray(string name[], int size)
    {
        for (int count = 0; count < size; count++)
            cout << name[count] << endl;
            cout << endl;
    }

I would like this program to display the unsorted array of strings, sort the strings, the display the sorted array. Instead it is only sorting the first letter of the names. Like so:

The unsorted values are 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

The sorted values are Aollins, Bill Cmith, Bart Cichalski, Joe Griffin, Jim Hanchez, Manny Hubin, Sarah Jaylor, Tyrone Kaylor, Tyrone Mmith, Bart Mmith, Bart Molfe, Bill Phitman, Jean Rmith, Bart Su, Hong Shitman, Jean Su, Hong Thitman, Jean Wolfe, Bill Whitman, Jean Wu, Hong

I feel like I'm close... Any help is appreciated.

Upvotes: 1

Views: 1525

Answers (3)

Drop
Drop

Reputation: 13003

It's OK to write such code if you are learning algorithms.

However note that in "real C++" (i.e. production code) we tend to:

  • use STL containers instead of arrays
  • use STL algorithms instead of raw loops

Code:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>

int main(int argc, char** argv)
{

    std::vector<std::string> names = 
    { 
        "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" 
    };

    std::sort(names.begin(), names.end(), std::less<>());

    std::copy(names.begin(), names.end(), 
              std::ostream_iterator<std::string>(std::cout, "\n"));

}

Links:

Upvotes: 2

RichardPlunkett
RichardPlunkett

Reputation: 2988

Inside selectionSort you need to get rid of the three calls to .at(0)

when you write name[startScan].at(0), the varible name is an array of strings, so name[startScan] is a string, so name[startScan].at(0) is the first letter of that string.

You need to compare by and assign by strings, not by chars. Getting rid of the .at(0)'s will do that, tho it will leave you with type errors, needing a few adjustments, and thus char startScan, minIndex, minValue; should be

int startScan,minIndex;
string minValue;

Upvotes: 0

Daniel King
Daniel King

Reputation: 405

Use std::sort with customized compare function

  1. use std::vector to hold the Names array

  2. define a customized compare function

    bool myCompare(string i,string j) 
    { 
            if(i.length() >= 1 &&
                j.length() >=1)
            {
                return (i[0]<j[0]); 
            }
    
            return false;
    }
    
  3. use std::sort

    std::sort (vecName.begin(), vecName.end(), myCompare);
    

Upvotes: 0

Related Questions