[Solved]Lab 41 Tagsort Sorting Techniques Ve Learned Manipulate Array Data Swapping Sliding Genera Q37169174
Lab 4.1 — Tagsort
The sorting techniques you’ve learned until now manipulate thearray of data — swapping, sliding, and in general, moving theelements around until they are in sorted order. This can beundesirable for two reasons:
- There are times we wish to preserve the original order of theelements, and this is lost as a result of the sort.
- If the elements are large, the data movement resulting from thesort can be time-consuming, and/or require additional space.
Tagsort is a sorting technique that avoids these two problems.The basic idea is to maintain a secondary array (sometimes calledan index or tag array) containing some sort of reference to thecorresponding elements of the actual data array. The tag are oftenimplemented as integers corresponding to the subscripts (locations)of the elements in the data array:
Before sorting:(index) 0 1 2 3 4data: 4 7 2 8 5tag: 0 1 2 3 4After sorting:(index) 0 1 2 3 4data: 4 7 1 8 5tag: 2 0 4 1 3
Iterating through the tag array produces a sorted enumeration ofthe data, while iterating through the data array produces theoriginal sequence. Furthermore, no data has been moved during thesort — only the relatively small tags. (Note that in Java, whereall access to data (except primitives) is via a reference, and thuswhen sorting an array, one is merely sorting the references, whichare, in essence, acting as tags, and thus all sorts (except forthose of primitives) are essentially tagsorts , in the sense of thereferences being moved around, and not the data. (Like all accessto object through references, this is completely transparent toyour code). f course, if one wished to preserve the original order,one could make a copy of the original array, and sort that.
In C/C++, we can use integers for the tag array (as shownabove), or alternatively pointers to the elements. For the sake ofpractice with pointers, this exercise hooses the latter. Write amodules tagsort.h/.cpp with a single function named sort in the .hfile. sort accepts an array of integer pointers, and a integerrepresenting the number of elements in the array, and performs atagsort on the data pointed at by the elements of the arrayparameter. You should also write a test driver in the form of amain method in its own file (tagsort_app.cpp) that declares twoarrays… one of integers and the other of pointers to integer.Populate the first with integers read from the file numbers.text,and the second initialized so that each element points to thecorresponding element of the integer array (i.e., element 0 of thepointer/tag array points at element 0 of the integer array, element1 to element 1, etc — just as shown in the above diagram.
In C++, thanks
Expert Answer
Answer to Lab 4.1 — Tagsort The sorting techniques you’ve learned until now manipulate the array of data — swapping, sliding, … . . .
OR

