Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment is about pseudocode. Pseudocode Pseudocode is a high-level descr

ID: 3692852 • Letter: T

Question

This assignment is about pseudocode.

Pseudocode

Pseudocode is a high-level description of a computing process. It usually looks like code because pseudocode follows some of the common conventions of programming languages. It is not code, however, as it does not follow any particular programming language syntax. Instead, it is intended to be read by humans instead of computers.

Many common algorithms are described by pseudocode. This allows us to have a common descriptor for an algorithm that can then be implemented in any programming language. It provides an outline for the solution that a programmer can then fill in.

The Problem Statement and Program Setup

You have a collection of gnomes. Each gnome has a number so that they can be placed in numerical order. You wish to write a program to sort these gnomes.

Program Setup

A scaffold of the solution has been created for you:

Do not modify the code that is already present. Instead, fill in the GnomeSort function marked like this:    /*** ... ***/

This function will perform a gnome sort to order the gnomes based on their assigned values. Implement the pseudocode below by translating it to C syntax in your function.

Function Prototype

You must use this prototype to receive credit for the assignment.

// Pre-condition: length is the size of the array gnomes.

// Post-condition: gnomes will be sorted in numerical order, from

//                 smallest to largest.

void GnomeSort(int gnomes[], int length);

Gnome Sort Pseudocode

procedure gnomeSort(a[])
    pos := 1
    while pos < length(a)
        if (a[pos] >= a[pos-1])
            pos := pos + 1
        else
            swap a[pos] and a[pos-1]
            if (pos > 1)
                pos := pos - 1
            end if
        end if
    end while
end procedure

Explanation / Answer

// Sorts the items array[0] through array[length-1] using the Gnome Sort algorithm. void GnomeSort(int gnomes[], int length) { // we have declared array as gnomes so we use //gnomes instead of a[], you can also use a[] by declaring int a[], in function perameters /*** FILL IN YOUR CODE HERE ***/ int pos=1; //post integer initializing to 1 as in psuedocode while(pos==gnomes[pos-1]) // comparing array integers to sort pos++; // incrementing pos by 1 for the next array element else { swap(gnomes[pos],gnomes[pos-1]) // swaping for sorting by calling swaping function if(pos>1) // checking pos value as stated in psuedocode pos--; // decrementing pos by 1 } } }