Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n).
import java.util.*;
class GFG
{
static void countSort(int[] arr)
{
int max = Arrays.stream(arr).max().getAsInt();
int min = Arrays.stream(arr).min().getAsInt();
int range = max - min + 1;
int count[] = new int[range];
int output[] = new int[arr.length];
for (int i = 0; i < arr.length; i++)
{
count[arr[i] - min]++;
}
for (int i = 1; i < count.length; i++)
{
count[i] += count[i - 1];
}
for (int i = arr.length - 1; i >= 0; i--)
{
output[count[arr[i] - min] - 1] = arr[i];
count[arr[i] - min]--;
}
for (int i = 0; i < arr.length; i++)
{
arr[i] = output[i];
}
}
static void printArray(int[] arr)
{
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println("Counting Sort");
}
// Driver code
public static void main(String[] args)
{
int[] arr = {10, 50, 100};
countSort(arr);
printArray(arr);
}
}
Points to be noted:
1. Counting sort is efficient if the range of input data is
not significantly greater than the
number of objects to be sorted. Consider the situation where the input sequence is between
range 1 to 10 and the data is 10, 50, 100,
2. It is not a comparison based sorting. It running time
complexity is O(n) with space
proportional to the range of data.
Counting Sort
Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects
having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output
sequence
Write a Java program to implement Counting Sort and write a driver to test it. Note:...
1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...
2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In this assignment you will investigate three different variants of algorithms that we have discussed in the class to solve the selection problem. Version 1. “Simultaneous minimum and maximum” selection algorithm. Version 2. Randomized selection algorithm. Version 3. “Median of...
need help!! java eclipse
Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) algorithms. a) Compute the CPU processing time for all the algorithms for varying input sizes as follows: N-10, 103, 10, 10, and 106 b) Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 10, 1 -10, 1 - 10, 1 12 10 c) Write down your results...
Write a program in Java that performs the Counting Sort algorithm. a) Create the countingSort method. Use the pseudocode shown in the lecture. b) Test your codes by writing a program that uses the following input array: int[] array_A = {6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2}; c) Your output should display the following versions of the arrays: I. The original input array A II. The counting array C after the counting (lines 4-5 in pseudocode)...
This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...
DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them in three arrays, then sorts the arrays with Insertion Sort, Merge Sort, and Quick Sort, respectively. Augment the three sorting algorithms with counters and report the number of characteristic operations each performs in sorting the (same) random values. INPUT The program reads from the terminal the number of random integers to generate, a seed value for the pseudo-random number generator, and a character that...
This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...
In java create a program to test and measure an actual running time of Insertion sort and Merge sort on your machine. Each sorting algorithm must be a separate method: a. Randomly generate 100 integers between 0 and 500. b. Store those integers in an array and display them on screen. c. Using each sorting algorithm, sort numbers and display the sorted numbers. The array must be passed as an argument to call a method. d. For each sorting algorithm,...
Write a program in Java that obtains the execution time of selection sort, insertion sort, bubble sort, merge sort, quick sort, and radix sort. Your program should test all sort methods for input sizes of 10000, 20000, 30000, 40000, 50000, and 60000. The selection sort, bubble sort, and radix sort should also be tested for input sizes 100000 and 200000. Your program should create the data that is sorted from randomly generated integers and should output the results in a...
Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...