#ford-johnson algorithm

25 messages · Page 1 of 1 (latest)

hasty light
#

having a hard time implementing this sorting algo (c++98 cz my school still lives in the past):


void    sortVector(vec& vc)
{
    if (vc.size() == 1) return;

    vec bigs;
    vec smls;
    for (size_t i = 0; i < vc.size() - 1; i += 2)
    {
        if (vc[i] > vc[i + 1])
            std::swap(vc[i], vc[i + 1]);
        bigs.push_back(vc[i + 1]);
        smls.push_back(vc[i]);
    }
    int hasLeftover = vc.size() % 2;
    int leftover = hasLeftover ? vc.back() : 0;

    sortVector(bigs);

    vec S = bigs;
    if (smls.size())
    {
        std::vector<size_t> order = jacobSeq(smls.size());
        for (int i = 0; i < (int)smls.size(); ++i)
        {
            size_t idx = order[i];
            vec::iterator pos = std::lower_bound(S.begin(), S.end(), smls[idx]);
            S.insert(pos, smls[idx]);
        }
    }

    if (hasLeftover)
    {
        vec::iterator pos = std::lower_bound(S.begin(), S.end(), leftover);
        S.insert(pos, leftover);
    }

    vc = S;
}

some terminal output, the 5 is missing and 1 is doubled:

➜  ex02 git:(main) ✗ make && ./cursed 3 2 1 4 5 7 6
Before: 3 2 1 4 5 7 6 
After : 1 1 2 3 4 6 7 
expected: 1 2 3 4 5 6 7
bold pecanBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

blazing crag
#

does it just generate indices from 0 to n-1?

hasty light
blazing crag
#

ah its a jacobsthal number J(n)

#

okay

plain vale
#

I don't know what the jacobsthal numbers are doing but they contain the 1 twice, which means you're also inserting the element smls[1] twice

blazing crag
#

idk why they are here either

hasty light
#

!solved

bold pecanBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

plain vale
#

hmm also that should just go out of bounds very quickly

blazing crag
#

i still dont know why merge sort requires jacobsthal numbers

hasty light
# blazing crag i still dont know why merge sort requires jacobsthal numbers

In computer science, merge-insertion sort or the Ford–Johnson algorithm is a comparison sorting algorithm published in 1959 by L. R. Ford Jr. and Selmer M. Johnson. It uses fewer comparisons in the worst case than the best previously known algorithms, binary insertion sort and merge sort, and for 20 years it was the sorting algorithm with the ...

DEV Community

Ford-Johnson sorting algorithm, or merge-insertion sort, is not a very well known or popular...

plain vale
#

honestly I'd implement the simpler version without jacobsthal numbers first

#

since those seem to just be an optimization from what I understand

hasty light
plain vale
#

rejected from where?

hasty light
plain vale
#

but why was it rejected?

#

is it a requirement that you have to use those numbers?

plain vale
#

oh ok, if it's just too slow then there are far simpler things you can do