Question

In: Computer Science

Counting SortShow the B and C arrays after Counting Sort finishes on the array A [19,...

Counting SortShow the B and C arrays after Counting Sort finishes on the array A [19, 6, 10, 7, 16, 17, 13, 14, 12, 9] if the input range is 0-19.

Solutions

Expert Solution

solution:

Given array A:

A

19 6 10 7 16 17 13 14 12 9
0 1 2 3 4 5 6 7 8 9

Range is given 0-19

Initialize an array C of length max+1 with all elements 0. This array is used for storing the count of the elements in the array.

C

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Store the count of each element at their respective index in array C.

C

0 0 0 0 0 0 1 1 0 1 1 0 1 1 1 0 1 1 0 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Store cumulative sum of the elements of the array C. It helps in placing the elements into the correct index of the sorted array.

C

0 0 0 0 0 0 1 2 2 3 4 4 5 6 7 7 8 9 9 10
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Add elements of array A to resultant array B.

B

0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9

Find the index of each element of the original array in the count array. This gives the cumulative count. Place the element at the index calculated.

For example: 19's count in the C array is 10 so it would be place at position 10-1 = 9 in the array B.

B

0 0 0 0 0 0 0 0 0 19
0 1 2 3 4 5 6 7 8 9

6's count in the C array is 1 so it would be place at position 1-1 = 0 in the array B.

6 0 0 0 0 0 0 0 0 19
0 1 2 3 4 5 6 7 8 9

Similarly, we can fill full array B as follows:

6 7 9 10 12 13 14 16 17 19
0 1 2 3 4 5 6 7 8 9

This the required sorted array.

Please give thumbsup, or do comment in case of any query. Thanks.


Related Solutions

In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople...
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople on a commission basis.  The salespeople each receive $250 per week plus 11 percent of their gross sales for the sales period.  For example, a salesperson who grosses $5000 in sales in the period receives $250 plus 11 percent of $5000, or a total of $812.21.  Write a program (using an array of counters) determines for each salesperson their total sales, their salary and additional data points.  There...
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
sing arrays please write a program to implement the STACK concept after creating the Array, the...
sing arrays please write a program to implement the STACK concept after creating the Array, the user is to be presented with a menu to choose from a number of options such as pop, push, top, etc... elements to be added on the stack are ints between 0 and 99 include a loop to re display the options (menu) and an outer loop to restart the program Write a C++ program to implement the Stacks concept. the stack implementation is...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. (Write a C# program)
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and a3 of type float as parameters. It then multiplies a1 and a2 and stored the result in a3. Assume that array multiplication is done by multiplying corresponding array elements, e.g. a3[i] = a1[i] * a2[i] where 0 <= i <= (array size – 1) Write a C++ main program to dynamically create three equal sized float arrays a1, a2 and a3. The size of...
C++ Given 2 int arrays that are related, sort them correspondingly. EXAMPLE: int arr1[] = {84,...
C++ Given 2 int arrays that are related, sort them correspondingly. EXAMPLE: int arr1[] = {84, 4, 30, 66, 15}; int arr2[] = {7, 5, 2, 9, 10}; SORTED ANSWER: 4 - 5 15 - 10 30 - 2 66 - 9 84 - 7 IN C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT