-4
\$\begingroup\$

Help me with Pyramidal sorting by D. Knuth algorithm in C++.
I would like to clarify if I followed the algorithm correctly. The code works.

I have the function, but I'm not sure if my code fully matches the algorithm's textual description and diagram.

I have a Knuth algorithm in text form:

Records R1,...,RN, N≥2 are rearranged in place; after sorting is complete, their keys will be in order, K1≤...≤KN. text alg

and Nassi-Shneiderman diagram:

Nassi-Shneiderman diagram

Tell me if the code is close to implementing the Knuth algorithm or/and the Nassi-Shneiderman diagram (it's my teacher's diagram)?

My code:

void sortH(int* arK, int N) {
    // H1:
    int I = N / 2 + 1;
    int r = N - 1; // r = N
    int K;
    // H2:
    while (I > 0) {
        I = I - 1;
        K = arK[I]; // R = arK[l], K = arK[l]


        
        while (true) {
            // H3:
            int l = I;
            int j = 2 * I + 1;
            // H4: 
            while (j <= r) {
                // H5: 
                if (j < r && arK[j] < arK[j + 1]) {
                    j = j + 1;
                }

                if (K < arK[j]) {
                    arK[l] = arK[j];
                }
                else {
                    break;
                }
                l = j;
                j = 2 * l + 1;
            }

            arK[l] = K; // R[i] = R
            break;
        }
    }
    // H4: 
    while (r > 0) {
        // H7:
        
        K = arK[r];
        arK[r] = arK[0];
        r = r - 1;

        if (r == 0) {
            arK[0] = K;
            break;
        }

        int l = 0, j = 1;
        // H5:
        while (j <= r) {
            if (j < r && arK[j] < arK[j + 1]) {
                j = j + 1;
            }
            // H6:
            if (K < arK[j]) {
                arK[l] = arK[j];
            }
            else
            {
                break;
            }
            
            l = j;
            j = 2 * l + 1;
        }
        // H8:
        arK[l] = K;
    }
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Your code is not like the Knuth algorithm nor is it close to the diagram. You code has too many loops. \$\endgroup\$ Commented Oct 31, 2024 at 1:26
  • 2
    \$\begingroup\$ This site is for helping you improve your coding skills, we generally don't review algorithms. \$\endgroup\$ Commented Oct 31, 2024 at 1:27
  • 2
    \$\begingroup\$ Try coding this once more. Start with Knuth's steps as comments. (I'm a bit doubtful about the diagram.) \$\endgroup\$ Commented Oct 31, 2024 at 6:50

1 Answer 1

3
\$\begingroup\$

You don't stick closely to Knuth's presentation: I recommend to try to.

The deviation I think most significant is changing the structure of the loops. I can easily see reasons to use separate loops/procedures to build and drain the heap - plainly, that's not how Knuth presents it.

There is a dire lack of documentation embedded in the source code. While not deviating from the printed text book, the chances of code getting separated from documentation are widely dissimilar.

Why/to what purpose introduce I?
Why arK instead of R? (I might name it Records for the opportunity of having a current record R.)
There is a while (true)-loop ending with an unconditional break - what is the idea here?

For similarity with Knuth's presentation I'd code H6

    // H6: Larger than K?
    if (K >= Key(j))
        break;
    Records[i] = Records[j];

with Key(j) resulting in the key of Records[j].

\$\endgroup\$
1
  • \$\begingroup\$ Belatedly, I noticed arK is used in the diagram. \$\endgroup\$ Commented Oct 31, 2024 at 17:49

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.