Answered step by step
Verified Expert Solution
Question
1 Approved Answer
A counting sort is a technique to sort an array of n positive integers that lie between 0 and m, inclusive. You need m +
A counting sort is a technique to sort an array of n positive integers that lie between 0 and m, inclusive. You need m + 1 counters. Then, making only one pass through the array, you count the number of times each integer occurs in the array. For example, the figure below shows an array of integers that lie between 0 and 4 and the five counters after a counting sort has made its pass through the array. From the counters, you can see that the array contains one 0, three 1s, two 2s, one 3, and three 4s. These counts enable you to determine that the sorted array should contain [0, 1, 1, 1, 2, 2, 3, 4, 4, 4]. public class Question4 { /** Sorts an array of postive integers that lie within the range 0 to max. @param a The array. @param max The largest value in the array. */ public static void question4(int[] a, int max) { } /* * Do not write any code inside the main method and expect it to get marked. * Any code that you write inside the main method will be ignored. However, * you are free to edit the main function in any way you like for * your own testing purposes. */ public static void main(String[] args) { int[] array = {2, 8, 4, 10, 15, 0, 4, 8, 2, 2, 0, 15, 10}; System.out.println("The array before sorting:"); System.out.println(Arrays.toString(array)); //must print [2 8 4 10 15 0 4 8 2 2 0 15 10] question4(array, 15); System.out.println(" The array after sorting:"); System.out.println(Arrays.toString(array)); //must print [0 0 2 2 2 4 4 8 8 10 10 15 15] } }
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