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:
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):
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.
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.
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.