3 Replies - 7302 Views - Last Post: 27 July 2011 - 10:31 PM Rate Topic: -----

#1 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

Using select sort to sort an array of strings

Posted 27 July 2011 - 09:21 PM

Im doing exercises out of a c++ book that has end of chapter challenges. one of the challenges is to sort an array of predefined names. and then sort them in ascending alphabetical order. If this were an array of numbers like ints i could do it easy, but its not its strings. the part im stuck on is line 22. because if i was sorting integers i would set it equal to start which is 0 and then proceed with the sort. but obviously i cant set a string variable equal to 0. so my question is how would i proceed with sorting the names. here is the code i have so far. any advice is appreciated.



#include <iostream>
#include <string>
using namespace std;


int main()
{
    const int size = 20;

    string names[size] = {"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, Jim",
                         "Sanchez, Manny", "rubin, Sarah" , "Taylor, Tyrone", "Johnston, 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"};


    string first, last, something;


    for (int start = 0; start < size - 1; start++ )
    {
        something = "";
        last = names[start];
        
        for (int inner = start+1; inner < size; inner++)
        {
            
            if (names[inner] < names[start])
            {
                last = names[inner];
                something = inner;
            }
            
            
            
        }
        names[something] = names[start];
               
    }

    return 0;
}



This post has been edited by clickspiker23: 27 July 2011 - 09:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using select sort to sort an array of strings

#2 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

Re: Using select sort to sort an array of strings

Posted 27 July 2011 - 10:08 PM

ok so apparently i was wrong, after goggling and reading and searching it turns out its the same as sorting an integer in line 22 i can set it to 0. Here is the code i put together for justthe select sort and output of the sorted array.

    string sortName;
    int min;


    for (int start = 0; start < size - 1; start++ )
    {
        min = start;
        sortName = names[start];

        for (int inner = start+1; inner < size; inner++)
        {

            if (names[inner] < sortName)
            {
                sortName = names[inner];
                min = inner;
            }

        }
        names[min] = names[start];
        names[start] = sortName;
    }


    for (int output = 0; output < size; output++)
    {
        cout << names[output] << endl;
    }

This post has been edited by clickspiker23: 27 July 2011 - 10:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 dwisecup  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 22
  • Joined: 26-July 11

Re: Using select sort to sort an array of strings

Posted 27 July 2011 - 10:14 PM

There are a couple of different problems. Instead of keeping track of the actual first and last as strings use ints. Remember the basic steps of selection sort:

1. Set k = 1.
2. Select an index of the smallest object in the sequence of elements in positions from k to n.
3. Exchange the smallest object with the object in the k-th position.
4. Increase k by one.
5. Repeat the operations 24 until k == n.

int start, last;
    string something;


    for (start = 0; start < (size - 1); start++ )
    {
        last = start;
        something = names[start];
        
        for (int inner = start+1; inner < size; inner++)
        {
            
            if (names[inner] < something)
            {
                something = names[inner];
                last = inner;
            }
            
            
            
        }
        names[last] = names[start];
        names[start] = something;
    }



Was This Post Helpful? 0
  • +
  • -

#4 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Using select sort to sort an array of strings

Posted 27 July 2011 - 10:31 PM

Starting off with last names try calling sort()which is defined in the algorithm header. This may get you started as alphabetical characters are really no different to integers when it comes to ordering.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1