Question: Sorting with Pointers Program 2: Sometimes given an array of data that we need to able to view in sorted order while leaving the original




Sorting with Pointers Program 2: Sometimes given an array of data that we need to able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make a copy of one array into another, and then to sort the second array. This would allow us to view the data both in the original order and in the sorted order. This might be fine in many cases, but if the data set is large and memory limited (say, perhaps, in an embedded system), this solution might not be practicable. A more elegant solution is to sort the array indirectly, i.e., by using pointers. With this technique we wouldn't change the positions of the actual data items in the array, we would only change the values of the pointers that point into the array. Nevertheless, when this type of sorting is performed, we still will be able to access the data both in its original order, by using the original array, but also in sorted order, by using the array of pointers. Here are some diagrams that illustrate the idea
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
