Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be 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: arrPtr Array donations Array arrPtr Array donations Array 101 101 121 13] 141 [51 12] 131 14] 161 The book from which these diagrams were taken called the array containing the original data set the "donations Array," but that is not relevant to our current problem. What is depicted here is the original data set contained in the array on the right in each picture, and an array of pointers pointing at various elements of the original data set on the left in each picture. We first initialize the array of pointers so that each element in the pointer array points to the element in the data array that has the same index value (left hand picture). We then sort the pointers according to the values they point to, leaving the original data set untouched (right hand picture). After this Input For our input into this problem let's use the data in the file "Array Input Pointer Sort" found in the "Homework Input Files" folder on eLearning. Since we don't have file I/O in C yet, we can load those data into our programs through an initialization list as follows: int mainArr [ [ /7copy in array contents here Note that we should never copy anything from a non-text file (e.g., Word, PDF, etc.) directly into our programs. Instead, copy/paste the numbers first into NotePad (or a Notepad equivalent) and from there copy/paste them into your source code. Sorting There are many common sorting algorithms that could be used to sort these data. Most of these are what's known as "comparison based" sorting algorithms, since they make decisions by comparing each array element against others. Most often, comparison based sorting algorithms work by interchanging, or swapping, elements within the array One common and simple comparison based sorting algorithm is called "Bubble Sort." It works by making multiple passes through the data set and on each pass comparing two adjacent array elements and swapping them if the right hand element is less than the left hand element. In this way, the largest element remaining is "bubbled" to the top of the array on each pass. After all the passes are completed, the array is in sorted order. Here is the pseudo-code for one version of the Bubble Sort. This is an O(n2) algorithm that is based on two nested loops: * outer loop index "i" runs from n-1 down to 1 (inclusive) o inner loop index '"j" runs from 0 to i-1 inclusive) compare arrayi] and arrayLi+1] swap if array[j]> arrayLi+1] " Here, "n" refers to the size of the data set, 150 in our case. As written, this algorithm will swap the actual array elements until the data set is sorted. It does so by changing the values in the original array. In this assignment, however, we need to be able to sort the pointer array instead of the original int array. Although the sorting should be done according to the values the pointer array is pointing to, the only values actually changed are pointers. In short, we will swap the pointers that point to those array elements, while leaving the array elements themselves untouched. This swapping should be done with the swapIntPtr function as described below. We will need a swap function that can swap the values of two argument pointers. In this case, our original data set consists of all ints. Therefore, the swap function needs to be able to swap pointers to ints. The name of the function should be swapIntPtr . (Do not use the name "swap ." The reason is that there are built in swap functions on various compilers that can conflict with our functions.) What should the input parameters for the swapIntPtr of the internal temp variable? function be? What is the data type Displaving Data We will display the data both in unsorted form and in sorted form. (This means we will display the data using both the original array and the pointer array.) In each case, the data should be displayed 10 numbers per line, each number in a 6 byte field. Functions should be used to display the data Note that the display function that displays the data in the original array must be different than the display function that displays the data through the pointer array. Thus, this operation will require two different functions. Each will have a different set of input parameters. Proiect Requirements (Overview This is a high level outline of what needs to be accomplished in this assignment: 1) Initialize an int array of size 150 and load it with the data from eLearning ("Array Input 2) Create an array of int pointers of the same size. For purposes of discussion, call this Pointer Sort"). For purposes of discussion, call this array the "Data Array." array the "Pointer Array." Initialize it to point to the Data Array in such a way that, after being initialized, each element of the Pointer Array should point to the element in Data Array that has the same index. (This is illustrated in the first picture above.) 3)Sort the Pointer Array by using a modified version of the Bubble Sort algorithm provided. After sorting, pointerArr[0] should point to the smallest element in Data Array, pointerArr[1] should point to the second smallest element, and so fortlh 4)Your program should print out the data set three different times. First, print out the data set in its original order (by traversing the Data Array). Second, print it in sorted order (by traversing the Pointer Array). And finally, print the Data Array one more time to demonstrate the original order. 1)A swapIntPtr ) function. When called on two pointer arguments, this function should swap the values of the pointer arguments 2) A sorting function. This function should implement the Bubble Sort (or some other common sorting algorithm) on the Pointer Array. Note that we are not sorting the Data Array in this problem, only the Pointer Array. Therefore, the pseudocode for the Bubble Sort given above will have to be modified to work on pointers. Furthermore, the sorting will be done according to the values the pointers are pointing to. Referring back to Bubble Sort, if we are comparing the values pointed to in one adjacent pointer element to another, and the value pointed to is smaller in the right hand element than the left, then we swap the pointers, not the values in the Data Array. 3)A display function for the Data Array. This function should display the data in the Data Array, 10 numbers per line in a 6 byte field. 4)A display function for the Pointer Array. This function should display the data pointed to in the Pointer Array, 10 numbers per line in a 6 byte field. Note that this function does not display addresses. It should display the integers found in the data set, but in sorted order. I suggest implementing your solutions as follows: 1) First initialize your main array (Data Array) with the data from eLearning. This contains 2) 150 pseudo-random integers between 0 and 3000. Then implement the function that prints out the data in the Data Array. Set it up to print 10 numbers per line, each number in a 6 byte field. Use it to test that the Data Array was initialized properly. Don't do anything else until this function is written and debugged 3) Then set up the Pointer Array and initialize it as described above. 4)Then implement the display function that prints out the data from the Pointer Array. Use it to test the initialization of the pointer array. At this point, the data should display in the same order as in the original array. Don't do anything else until this function is written and debugged. 5) Then write the swapIntPtr) function that swaps two int pointers. Test it by using a small driver program as follows: int x=5, y= 10 ; int xPtr-&x; %p ", printf ("Address of x %p; Address of y swap (&xPtr, &yPtr) printf ("Address of x = %p; Address of y xPtr, yPtr) ; %p ", xPtr, yPtr) ; If your code is working well, the addresses should be swapped 6)Then implement the Bubble Sort algorithm using the modified sorting algorithm given above. It should use the swapIntPtr () function described above to do its swapping. As you work, use the display function for the pointer array - that you have already written in step 4 -- to check vour work. 7)Once everything is working, set up the main function to perform the tasks of the

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions