Question

In: Computer Science

Write code for A counting sort is a technique to sort an array of n positive...

Write code for 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 1’s, two 2’s, one 3, and three 4’s. These counts enable you to determine that the sorted array should contain [0, 1, 1, 1, 2, 2, 3, 4, 4, 4].

import java.util.Arrays;

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("\nThe array after sorting:");

System.out.println(Arrays.toString(array)); //must print [0 0 2 2 2 4 4 8 8 10 10 15 15]

}

}

Solutions

Expert Solution

For this function we pass the array and the max value in the array i.e., 15. In the function, we create a counter array of size 1 more than the max value.

Then we fill this counter with the frequency of the number which is the current index. For eg current number is 2, then it will increment the value at index 2.

Finally using this counter, we put the index value in the array: a. For eg, for index i=0, the counter value is 2, so the 0 will be placed in the array at 0th place. Then counter value at that index is reduced as one instance of 0 is already placed.

CODE:

import java.util.Arrays;

public class Main{

public static void main(String[] args) {

  
int[] a = {2, 8, 4, 10, 15, 0, 4, 8, 2, 2, 0, 15, 10 };

System.out.println("Array before sorting:");
System.out.println(Arrays.toString(a));

question4(a, 15); //calling function to sort

System.out.println("Array after sorting:");
System.out.println(Arrays.toString(a));

}

public static void question4(int[] a, int max) {
int counter[] = new int[max + 1]; //creating m+1 sized counter
  
//filling the counter with number frequency
for (int i : a) {
counter[i]++;
}
  
// sort array
int x = 0;
for (int i = 0; i < counter.length; i++) {
while (0 < counter[i]) {
a[x++] = i; //put the index to the array
counter[i]--; //reduce counter at that index
}
}
}

}

OUTPUT:


Related Solutions

(C++)Counting Sort: Write C++ codes for counting sort. The input array is A = {20, 18,...
(C++)Counting Sort: Write C++ codes for counting sort. The input array is A = {20, 18, 5, 7, 16, 10, 9, 3, 12, 14, 0}
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.
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...
/** * This program will sort an n by n array by the first value in...
/** * This program will sort an n by n array by the first value in each row. * Selection sort algorithm is modified to do the sorting. * For example: * <p/> * If the original array is: * 1 2 3 4 5 * 3 4 5 1 2 * 5 2 3 4 1 * 2 3 1 4 5 * 4 2 3 1 5 * <p/> * The array after sorting is: * 1 2...
5. (20 marks) Write a recursive Bubble Sort algorithm that takes an array A of n...
5. Write a recursive Bubble Sort algorithm that takes an array A of n numbers as input. Analyze its time complexity using a recursion tree. Implement your algorithm in Java
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).
(C++)Radix Sort: Write C++ codes for radix sort: use counting sort for decimal digits from the...
(C++)Radix Sort: Write C++ codes for radix sort: use counting sort for decimal digits from the low order to high order. The input array is A = {329, 457, 657, 839, 436, 720, 353}
(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)
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT