Question

In: Computer Science

1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1,...

1. Sort the given keys using Counting sort algorithm. Also write the algorithm.

         4, 1, 0, 2, 1, 5, 0, 4                                                                    

No code or programs, please. Manually solve the problem, please. Thanks

Solutions

Expert Solution

Input data (A[]) : 4, 1, 0, 2, 1, 5, 0, 4

Since maximum value is 5, we can take count array of length 5. However to be more general, let us take a count array of lenhth 10.

Step 1: Initialize count array with 0 values.

for(i=0; i<10; i++)

count[i] = 0;

Step 2: Store the count of each element in the count array.

for(i=0; i<n; i++)

count[A[i]]++;

Thus contents of count array = [2, 2, 1, 0, 2, 1, 0, 0, 0, 0]

Step 3: Modify the count array such that each element at each index stores the sum of previous counts.

for (int i = 1; i < 10; ++i)

count[i] += count[i - 1];

Thus contents of count array = [ 2, 4, 5, 5, 7, 8, 8, 8, 8 8]

Step 4: Output each item from the input sequence in current count index followed by decreasing its count by 1.

for(i=0; i<n; i++){

output[count[A[i]]-1] = A[i];

count[A[i]]--;

Thus output[6] = 4, output[3] = 1, output[1] = 0, output[4] = 2, output[2] = 1, output[7] = 5, output[0] = 0, output[5] = 4

Step 5: Copy output to original array.

for(i=0;i<n;i++)

A[i] = output[i];

Thus content of A = [0, 0, 1, 1, 2, 4, 4 5]


Related Solutions

Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Please write a python code which implements the counting sort algorithm by creating a CountingSort function...
Please write a python code which implements the counting sort algorithm by creating a CountingSort function with an array input in the form of a list. Then write code to sort 3 randomly generated arrays and the data array listed below, print the output clearly for all four test arrays, develop and comment on the growth function of your code. Comment on the Big O notation of the code. ( Please do not forget to comment on your code to...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using Max Heap.
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using Max Heap.
Please describe the reason(s) why we choose the counting sort algorithm to sort each digit in...
Please describe the reason(s) why we choose the counting sort algorithm to sort each digit in the Radix Sort?
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm....
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm. show work
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT