-1

I would need some help because I've been wondering about this algorithm for all day long now...So, basically I want to create a function that takes in input a string array and the number of elements and I want to arrange everything in order...but sometimes it works, sometimes not... It would be much appreciated if you can help me...I don't need to use any or something, just the basic string (NOT array of chars) and it's functions... Thank you!

#include <iostream>
#include <string>
#include <string.h>
#include <sstream>  
#include <ctype.h>

bool sortS(string a ,string b)
{
    for(int i=0;i<a.length() && i<b.length();i++)
    {
        if(a.substr(i,1)>b.substr(i,1))return false;
    }
    return true;
}

void selectionSort(string arr[], int n)  
{  
    int i, j, min_idx;   
    for (i = 0; i < n-1; i++)  
    {  
        min_idx = i;  
        for (j = i+1; j < n; j++)
        {  
            if (sortS(arr[j],arr[min_idx]))
            {
                min_idx = j;
            }
            swap(arr[min_idx], arr[i]);  
        }  
    }  
}

int main()
{
    string vett[]={"Junie","Andy","Laine","Buford","Kathrin","Slyvia","Earlean","Lakeshia","Marry","Arica"};
    int n=sizeof(vett)/sizeof(vett[0]);
    for(int i=0;i<n;i++)
    {
        cout<<vett[i]<<" ";
    }
    cout<<endl;
    selectionSort(vett,n);
    for(int i=0;i<n;i++)
    {
        cout<<vett[i]<<endl;
    }
    cout<<endl;

return 0;
}  

The output is this:

Arica
Andy
Laine
Buford
Kathrin
Slyvia
Earlean
Lakeshia
Marry
Junie
AestheticCode
  • 115
  • 1
  • 7
  • What's wrong with simply [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort)?? You could be done in one line of code. – Jesper Juhl Dec 08 '19 at 17:02
  • I know, in fact, that's what I hate most about my teacher... He said that we must use a sorting algorithm of our choice to do this, I've asked about the sort() function and he said that we must build our own function... – AestheticCode Dec 08 '19 at 17:03
  • 1
    Do you need your own function for comparison as well? Because `std::string` already has `operator<` and others for comparing them in lexicographical order. – Yksisarvinen Dec 08 '19 at 17:05
  • I think I can use operator< ...but can you tell me what needs to be changed? – AestheticCode Dec 08 '19 at 17:07
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Dec 08 '19 at 17:30

2 Answers2

2

Assuming you have to check the input strings character-by-character:

Your sortS only return false when a pair of chars are greater than, it doesn't test if they are less than. It also returns true if the input strings aren't the same length and have matched up to the smallest's length. It also uses substring to extract a char when string::operator[]() is clearer.

Try:

bool sortS(string a ,string b)
{
    for(int i=0; i < a.length() && i < b.length(); ++i)
    {
        if (a[i] > b[i])
           return false;
        else if (a[i] < b[i])
           return true;
    }
    return a.length() < b.length() ;
}

Otherwise it's trivially:

bool sortS(string a ,string b)
{
    return a < b;
} 
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • Hey, thank you for your help but maybe my algorithm confused you because it still doesn't work 100%: The output is: Andy Arica Earlean Buford Junie Kathrin Laine Marry Lakeshia Slyvia I can't understand why it takes the E before the B – AestheticCode Dec 08 '19 at 17:16
0

so I solved the problem...apparently there was something to do with my sorting algorithm(Selection Sort)...so, I solved it by changing the sorting algorithm to bubble sort, here is the entire program:

#include <iostream>
#include <string>
#include <string.h>
#include <sstream>  
#include <ctype.h>

using namespace std;

bool sortS(string a ,string b)
{
    for(int i=0; i < a.length() && i < b.length(); ++i)
    {
        if (a[i] > b[i])
           return false;
        else if (a[i] < b[i])
           return true;
    }
    return a.length() < b.length() ;
}

void bubbleSort(string arr[], int n)  
{  
    int pass=0;
    string temp;
    for(int i = 0; i<n; i++)
    {
       for(int j = i+1; j<n; j++)
       {
          if(sortS(arr[j],arr[i])) 
          {
             temp = arr[i];
             arr[i] = arr[j];
             arr[j] = temp;
          }
       }
        pass++;
    }
}


int main()
{

 string vett[]={"Junie","Andy","Laine","Buford","Kathrin","Slyvia","Earlean","Lakeshia","Marry","Arica"};
    int n=sizeof(vett)/sizeof(vett[0]);
    for(int i=0;i<n;i++)
    {
        cout<<vett[i]<<" ";
    }
    cout<<endl;
    bubbleSort(vett,n);
    for(int i=0;i<n;i++)
    {
        cout<<vett[i]<<endl;
    }
    cout<<endl;
return 0;
}
AestheticCode
  • 115
  • 1
  • 7