Table of Contents

Corning Community College

CSCS2330 Discrete Structures

~~TOC~~

Project: Sorting/Manipulation Happiness (smh0)

Objective

We will be exploring the implementation of various sorting algorithms. This continues our ongoing algorithm theme. And like other projects, it offers an opportunity to gain a deeper appreciation for differences in approaches.

Sorting Algorithms

There are many different sorting algorithms in existence, which are the result of different approaches to arrive at the intended solution: to arrange your data in accordance with the prescribed order.

For this project, our sorting order will be from greatest to least.

The sorting algorithms I'd like for your to implement are:

Please do take note of the algorithmic complexity each algorithm offers, specifically:

Linked List

You are to use a linked list to accomplish your sorting algorithms, a simple implementation of which has been provided.

If you'd like to implementation additional list functionality (various list functions), that is also fine.

List implementation

Linked lists and arrays are peers… they both can hold multiple objects of the same type. Both have advantages and disadvantages, depending on the sort of operations you are performing.

Number unit

Here is a sample list implementation you are free to use (just place the code in your program):

1
struct number
{
    int            value;
    struct number *next;
};
typedef struct number Number;
 
Number *start;

Building a list

Let's say we have the following values provided on the command-line: 67, 3, 57, 73, 1, 24, 37, 96, 13, 39, 26, 31, 4

The following code will build a list (beginning at the “start” pointer), and ending with a NULL value.

1
int     input  = 0;
Number *tmp    = NULL;
start          = NULL;
 
for (input = 1; input < argc; input++)
{
    if (start == NULL) // start being NULL indicates an EMPTY list
    {
        start  = (Number *) malloc(sizeof(Number)); // allocate new Number
        start -> value = atoi(argv[input]); // assign input into Number
        start -> next  = NULL;  // assign next value as NULL
        tmp    = start;         // tmp is pointing to the end of our list
    }
    else // non-NULL start means a populated list
    {
        tmp -> next    = (Number *) malloc(sizeof(Number)); // allocate new Number, attached at end of list
        tmp            = tmp -> next; // advance tmp pointer to newly allocated Number
        tmp -> value   = atoi(argv[input]); // assign input to new Number in list
        tmp -> next    = NULL; // set next to NULL (should be nothing after end of list)
    }
}

This is just a sample implementation; feel free to alter it as desired (doubly-linked? sure!)

Bubble sort as linked list

Following will be a Bubble sort implementation (the one we did in class last week) utilizing the linked list implementation described above:

1
    int     frip    = 0;
    Number *outdex  = NULL;
    Number *index   = NULL;
 
    // display this from start to end
    tmp = start;
    fprintf(stdout, "BEFORE: ");
    while (tmp != NULL)
    {
        fprintf(stdout, "%2d ", tmp -> value);
        tmp = tmp -> next;
    }
    fprintf(stdout, "\n");
 
    outdex                              = start; // start outdex at start of list
    while (outdex                      != NULL) // keep looping until we're done with list
    {
        frip                            = 0; // our sort flag
        index                           = start; // start index at start of list
        while (index  -> next          != NULL) // loop through the list
        {
            if (index -> value         <  index -> next -> value)
            {
                index -> value          = index -> value ^ index -> next -> value;
                index -> next -> value  = index -> value ^ index -> next -> value;
                index -> value          = index -> value ^ index -> next -> value;
                frip                    = 1;
            }
            index                       = index -> next;
        }
 
        if (frip                       == 0)
            break;
 
        outdex                          = outdex -> next;
    }
 
    // display this from start to end
    tmp = start;
    fprintf(stdout, "AFTER:  ");
    while (tmp != NULL)
    {
        fprintf(stdout, "%2d ", tmp -> value);
        tmp = tmp -> next;
    }
    fprintf(stdout, "\n");
 
    return(0);

In this case we didn't actually manipulate Number pointers. That is okay for this project; if you would like to, then by all means, feel free :)

NOTE: This code is available as a fully working program in the project grabit, in a file called bubble.c

Sample output

Your program's output should be as follows:

lab46:~/src/discrete/smh0$ ./bubble 67 3 57 37 1 24 37 96 13 39 26 31 4
BEFORE: 67  3 57 37  1 24 37 96 13 39 26 31  4
AFTER:  96 67 57 39 37 37 31 26 24 13  4  3  1
lab46:~/src/discrete/smh0$ 

Program

Your task is to write 3 separate, standalone programs that each has linked-list implementations of the identified sorting algorithms.

Each will accept all their input values via the command-line (as numeric arguments), should load into a list, and then proceed with processing in accordance with the identified sorting algorithm.

Please display the list both BEFORE and AFTER the selected sorting algorithm takes place (and remember sorting order!). Please format your BEFORE/AFTER lines in accordance with output examples given. This will aid in verification.

If you'd like to also include more intermediate sorting steps (for display/debugging purposes), feel free to do so, but please disable them before your final project submission. Your programs should ONLY output the full BEFORE/AFTER lines.

Helpful hints

It shouldn't matter if we use an array or a linked list- but since most examples you'll find on the internet use arrays, this is a means of ensuring you really understand what is going on (ie reduce the temptation to just mindlessly copy and not understand). It shouldn't matter if you are in Data Structures now, have taken it before, or have not taken it… it is merely a connection of values in memory (JUST like an array).

Submission

Project Submission

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

lab46:~/src/discrete/smh0$ make submit
...
SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

Submission Criteria

To be successful in this project, the following criteria must be met: