Question
If you sort an array, you must physically move/swap the data from its original location to another location. Small arrays not a real problem, the
If you sort an array, you must physically move/swap the data from its original location to another location. Small arrays not a real problem, the larger the number of items, the more I/O overhead involved. What if we need to keep that array in its original order? Creating duplicate arrays is not a solution. How about using memory pointers?
What if we use a shadow array of pointers to each index location in the original array? The pointer addresses are swapped instead of the actual data values.
Load an integer array (called it numbers) of size 15 with random integer values ranging from 1 to 100
Create two additional arrays of pointers, aim the array index pointers to the integer array
Pass the first array of pointers to a sort function to sort ascending order the pointer values based on the value that these pointers are pointing to in the numbers array. The pointer values will be swapped within the pointer array.
Pass the second array of pointers to a function to sort descending order the pointer values based on the value that these pointers are pointing to in the numbers array. The pointer values will be swapped within the pointer array.
Display the contents of the numbers array
Display the numeric values that the ascending pointer array and the descending pointer array are pointing at in the numbers array.
You will need to code an ascending and descending sort (simplest is a bubble)
Output Example:
Display the contents of the Array:
Index # 0 18
Index # 1 11
Index # 2 55
Index # 3 50
Index # 4 45
Index # 5 40
Index # 6 76
Index # 7 80
Index # 8 75
Index # 9 79
Index # 10 85
Index # 11 96
Index # 12 12
Index # 13 42
Index # 14 11
Display the contents of the Array via the "shadow" pointer Array I:
Index # 0 18 @ Memory Location 0x24feb4
Index # 1 11 @ Memory Location 0x24feb8
Index # 2 55 @ Memory Location 0x24febc
Index # 3 50 @ Memory Location 0x24fec0
Index # 4 45 @ Memory Location 0x24fec4
Index # 5 40 @ Memory Location 0x24fec8
Index # 6 76 @ Memory Location 0x24fecc
Index # 7 80 @ Memory Location 0x24fed0
Index # 8 75 @ Memory Location 0x24fed4
Index # 9 79 @ Memory Location 0x24fed8
Index # 10 85 @ Memory Location 0x24fedc
Index # 11 96 @ Memory Location 0x24fee0
Index # 12 12 @ Memory Location 0x24fee4
Index # 13 42 @ Memory Location 0x24fee8
Index # 14 11 @ Memory Location 0x24feec
Sorting the shadow pointer array in the function (Ascending Order)
Display the Sorted Ascending contents of the Array
via the "shadow" pointer Array I:
Index # 0 11 @ Memory Location 0x24feec
Index # 1 11 @ Memory Location 0x24feb8
Index # 2 12 @ Memory Location 0x24fee4
Index # 3 18 @ Memory Location 0x24feb4
Index # 4 40 @ Memory Location 0x24fec8
Index # 5 42 @ Memory Location 0x24fee8
Index # 6 45 @ Memory Location 0x24fec4
Index # 7 50 @ Memory Location 0x24fec0
Index # 8 55 @ Memory Location 0x24febc
Index # 9 75 @ Memory Location 0x24fed4
Index # 10 76 @ Memory Location 0x24fecc
Index # 11 79 @ Memory Location 0x24fed8
Index # 12 80 @ Memory Location 0x24fed0
Index # 13 85 @ Memory Location 0x24fedc
Index # 14 96 @ Memory Location 0x24fee0
Sorting the shadow pointer array in the function (Descending)
Display the Sorted Descending contents of the Array
via the "shadow" pointer Array II:
Index # 0 96 @ Memory Location 0x24fee0
Index # 1 85 @ Memory Location 0x24fedc
Index # 2 80 @ Memory Location 0x24fed0
Index # 3 79 @ Memory Location 0x24fed8
Index # 4 76 @ Memory Location 0x24fecc
Index # 5 75 @ Memory Location 0x24fed4
Index # 6 55 @ Memory Location 0x24febc
Index # 7 50 @ Memory Location 0x24fec0
Index # 8 45 @ Memory Location 0x24fec4
Index # 9 42 @ Memory Location 0x24fee8
Index # 10 40 @ Memory Location 0x24fec8
Index # 11 18 @ Memory Location 0x24feb4
Index # 12 12 @ Memory Location 0x24fee4
Index # 13 11 @ Memory Location 0x24feb8
Index # 14 11 @ Memory Location 0x24feec
Display the contents of the Array:
Index # 0 18
Index # 1 11
Index # 2 55
Index # 3 50
Index # 4 45
Index # 5 40
Index # 6 76
Index # 7 80
Index # 8 75
Index # 9 79
Index # 10 85
Index # 11 96
Index # 12 12
Index # 13 42
Index # 14 11
Run this Again (Y or N):
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started