1

I have to insert a string inside an already sorted list of strings, what is the best way to achieve this to find the exact position to insert without having to sort all the list and not adding the string upfront. I just need to find the position to insert.

I do have a function which Compares 2 strings compare(first-string ,second-string) this function gives me true if first string has to be placed ahead of second string false in other way around. It is expected to use this function.

The List used in my scenario is not a c++ std::list but a customized class which handles some functions of a list.

  • The best you can do with a list is to iterate over it and compare each element – Thomas Sablik Jan 12 '21 at 11:02
  • Just search the list until you find the first element which is compares "higher" than your insertion string, and put it before that element (i.e. after the previous element). – einpoklum Jan 12 '21 at 11:03
  • As we have no earthly idea how you manufactured your custom sorted list (though giving you benefit of the doubt to one attribute: it's probably "sorted"), there is no way we can answer this question. I.e. we would need to see exactly how your list is constructed and managed to have any helpful insight in how to navigate to the insertion point and do the deed. – WhozCraig Jan 12 '21 at 11:04
  • So you're asking how to use some class that we cannot even see? – Asteroids With Wings Jan 12 '21 at 11:07
  • 1
    I think this is more of an algorithm question and not a coding question. OP doesn't ask for code but for the abstract "how", so the actual implementation and code is not necessary to answer this question. – Thomas Sablik Jan 12 '21 at 11:08
  • 1
    Does this answer your question? [binary search on a single linked list](https://stackoverflow.com/questions/33929614/binary-search-on-a-single-linked-list) – Thomas Sablik Jan 12 '21 at 11:25

2 Answers2

0

If you arrived at a parking place where each spot was named with a letter of the alphabet in order from A to Z.

And then I tell you that you can park at letter P.

How would you find that spot ?

Drax
  • 12,682
  • 7
  • 45
  • 85
  • 1
    IMHO a parking place is a vector, not a list. You can't binary search a list but can binary search a parking place. – Thomas Sablik Jan 12 '21 at 11:05
  • 1
    @ThomasSablik I strongly disagree. That only holds for a massive, single-row car park. Not many of those about. – Asteroids With Wings Jan 12 '21 at 11:07
  • Though "looking for your car" would be a much better example – Asteroids With Wings Jan 12 '21 at 11:08
  • @AsteroidsWithWings Why single-row? Have you ever stored a 2D image (or 3D color image) in a 1D array or vector? You just need the size of the image. You can estimate the size of most parking places. I can binary search `{{1, 2, 3}, {4, 5, 6}, {7, 8, 9]}` and store these values in a 1D array or vector `{1, 2, 3, 4, 5, 6, 7, 8, 9}` with size `{3, 3}`. – Thomas Sablik Jan 12 '21 at 11:14
  • @ThomasSablik Okay, I'll give you that - I think what I mean is that Drax's idea of a car park where all spaces are labelled in strictly increasing order (layout notwithstanding) is unusual :P Car parks I know are pretty haphazard in this sense, so a binary search just doesn't really work in the real world. "Oh, space 131's over there? And then 140 starts in _that_ corner? Okay.." But I appreciate that the ordering was an axiom of the premise. – Asteroids With Wings Jan 12 '21 at 12:10
  • You're nitpicking my trivial image guys ! Made me laugh though :) – Drax Jan 12 '21 at 12:30
-1

Shouldn't you be able to loop through your list, and do a comparison for each item, where you grade each string on how close they are too each other. Say you got a list of strings like this: ["aa", "ab", "bc", "cc"] and you wanted to insert "bb". How similar is "bb" to "aa", answer is 0. Next iteration. How similar is "bb" to "ab": 1. Now we got an increase by one. Mark down the position and continue to loop. How similiar is "bb" to "bc": 1. Next loop. How similar is "bb" to cc: 0. So when we got a decrease we now know that the position of where "bb" should be placed has been passed. Which means that we can go back and search more thoroughly.

Here is a fully working function:

    void InsertAZ(list<string>& strings, string&& str) {
        std::list<string>::iterator it;
        it = strings.begin();
        for (auto s : strings) {
            if ((s > str) == 1) {
                strings.insert(it, str);
                break;
            }
            ++it;
        }
    }

    int main()
    {
        list<string> strings = { "aaa", "aab", "aac", "aba", "abb", "abc", "aca", "acb", "acc", "baa", "bab", "bac", "bba", "bbb", "bbc", "bca", "bcb", "bcc", "caa", "cab", "cac", "cba", "cbb", "cbc", "cca", "ccb", "ccc" };

        InsertAZ(strings, "bab");

        cin.get();
    }

FRINTSO
  • 81
  • 8
  • 1
    Strings are usually sorted by [lexicographic order](https://en.wikipedia.org/wiki/Lexicographic_order). You should stop at `"bc"` and insert `"bb"` before `"bc"`. OP said that the list is already sorted. How does the similarity help? Please describe _"go back and search more thoroughly"_ – Thomas Sablik Jan 12 '21 at 11:40
  • 1
    This is C++. Why would you use `strcmp` with C-strings instead of [compare operators](https://en.cppreference.com/w/cpp/string/basic_string/operator_cmp)? – Thomas Sablik Jan 12 '21 at 11:48
  • I am new to c++ and c. strcmp and strcoll is two functions that I came across and I found them helpful. My solution works as far as I know after all. I will read on compare operators regarding string tough. – FRINTSO Jan 12 '21 at 11:53
  • 1
    Please don't suggest to unnecessarily convert strings into C-string to be able to use C functions. You can compare the strings with `s < str`, `s <= str`, `s == str`, ... And your `InsertAZ` function doesn't even insert anything into the list. It makes no sense to return an unchanged copy of the first argument. – Thomas Sablik Jan 12 '21 at 11:59