Computer Science 1622
Computer Science II

Programming Assignment 3
Sorting and Pointers (40 points)
Due Monday, January 25, 1998

Introduction

The purpose of a sorting algorithm is to reorder a set of elements so that they are in some logical order (smallest value to largest, largest to smallest, etc.). But what if we want to preserve the original order of the elements? Or what if we want to sort a set of elements in more than one way? A solution to these problems is to use a secondary index.

For an array (call this the primary array), a secondary index is a corresponding array where the secondary index gives us a way of looking at the primary array in a different order. We can implement a secondary index in two ways:

  1. The secondary index is an array of integers of the same length as the primary array and each element of the secondary index has a number of an element in the primary array. Each array index from the primary array occurs exactly once in the secondary index and the numbers are arranged to whatever order is desired.
  2. The secondary index is an array of pointers of the same length as the primary array and each element of the secondary index points to an element in the primary array. There is one pointer to each element of the primary array in the secondary index and the pointers are arranged in whatever order is desired.

For example, assume we have an array of 10 floating point values F whose order we do not want to change and we also want to have two secondary indices, one (IA) that indicates the order of the elements of F from smallest to largest and a second (ID) that indicates the order of the elements of F from largest to smallest. Furthermore, lets assume that IA is implemented as an array of integers and ID as an array of pointers. Then our three arrays might look like this (given the set of elements in F that are shown):

Sample Secondary Indices

The Problem

Implement a program that reads in an array of at most 200 floating point values that also implements the two secondary indices shown above. The original array of floating point values should not be changed. The first secondary index should be an array of integers giving the order of the primary array sorted from smallest to largest (as in IA above). You should build this index using a selection sort. The second secondary index should be an array of pointers giving the order of the primary array sorted from largest to smallest (as in ID above). You should build this index using an insertion sort.

Once you have built the two indices you should print out the array in: (1) its original order, (2) in order from smallest to largest value using the first secondary index, and (3) in order from largest to smallest value using the second secondary index.

What To Hand In

Hand in a full lab report for your program (consult the Programming Assignment Report Guidelines on what to include in this). Also include a listing of the data you used to test your program (you should discuss in your lab report what test data you used and whether this fully tests your solution). Finally you should attach output for your data showing that your program performs correctly.

Extra Credit - 8 points

Implement a third secondary index. In this index all of the non-negative elements should appear before any of the negative elements. Within the non-negative elements, the elements should be sorted from smallest to largest. Within the negative elements, the elements should be sorted from largest to smallest. For example, if we built this index for the array F above the resulting order should be: 0.2, 5.3, 5.4, 6.1, 7.9, 8.9, -1.2, -3.0, -3.4, -10.0. You should build this index as an array of pointers using a merge sort. In your program you should print out the array in this order in addition to the original order and the other secondary index orders.