0

I'm preparing for an interview and while looking across candidate experiences i came across the following question -

Given an array of strings : { “Sdjksajd”, “Skjdasjj”, “Bjsdakja”, “Xhudhau”, “Eeeeggg”, “Chrome”}. Display them in alphabetical order without using library functions .

I'm not very good at coding so i tried doing this using my limited knowledge in the following way:-

#include<iostream>
#include<string>
using namespace std;
string selsort(string s);

string selsort(string s)
{
int i=0,j,k=0;
string min=&s[i];
for(i=0;i<6;i++)
{
    for(j=i+1;j<6;j++)
    {
        if(s[j][k]<s[i][k])
        {
            string *temp;
            min=s[j];
            *temp=s[i];
            s[i]=s[j];
            s[j]=*temp;
        }
        else if(s[j][k]=s[i][k])
        {
                while(k<=s[j].length())
                {
                k++;
                selsort(string s);
                }
             }

        }
   }
     return s;
}

 int main()
 {
     int i;
     string s[6]={"Sdjksajd","Skjdasjj","Bjsdakja","Xhudhau","Eeeeggg","Chrome"};

  s=selsort(s);
  for(i=0;i<6;i++)
  cout<<s[i];
  return 0;
  }

However, i am getting "Error: invalid types 'char[int]' for array subscript" and i think my program needs many more modifications to make it work. How exactly can i solve this question?

2
  • The correct answer is "it is not possible to display anything without using library functions". Commented Nov 8, 2017 at 15:08
  • While that's true, i think they meant for us to solve it without using library functions to sort or compare the string elements. Commented Nov 8, 2017 at 20:09

1 Answer 1

1

Whilst the problem specifies that you can't just use a library sort, it doesn't mean that you can't be inspired by a library sort.

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

Also note the extra restrictions on RandomIt: it must be ValueSwappable (and dereference to a movable type). Note that a pointer into an array is a RandomIt, and strings are Swappable

So we have two parts:

  • Compare things
  • Re-arrange things

For the moment, let's assume that we have a Compare (we will define one later)

Now an obvious property of a sorted collection is that the first element is less than every other element. We can imagine a function RandomIt min_element(RandomIt first, RandomIt last, Compare comp) that finds the smallest element of a range. Thus we can swap the first element with the smallest, then carry on with the rest of the range. Thus sort becomes:

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp )
{
    for (RandomIt it = first; it != last; ++it)
    {
        RandomIt min = min_element(it, last, comp);
        swap(*it, *min);
    }
}

Now we need to implement min_element. We can just go through the range, using Compare to hold onto the current min.

template< class RandomIt, class Compare >
RandomIt min_element( RandomIt first, RandomIt last, Compare comp )
{
    RandomIt min = first;
    for (RandomIt it = first; it != last; ++it)
    {
        if (comp(*it, *min)) { min = it; }
    }
    return min;
}

And also we need swap

template< typename T >
void swap(T & lhs, T & rhs)
{
    T temp = static_cast<T&&>(lhs);
    lhs = static_cast<T&&>(rhs);
    rhs = static_cast<T&&>(temp);
}

Now we have our sorting function, we just need to be able to compare std::strings alphabetically. Luckily for us < is defined for strings to do this, so we just have:

bool string_less(const std::string & lhs, const std::string & rhs)
{
    return lhs < rhs;
}

We can put this all together into

void string_sort(std::string * values, std::size_t count)
{
    sort(values, values + count, string_less);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! I solved it with a friend's help. It seems we were missing the string comparison function "bool string_less" which would help in comparing the strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.