JAVA CODE can you change it by using Counting Sort please everything else dont n
ID: 3865476 • Letter: J
Question
JAVA CODE
can you change it by using Counting Sort please
everything else dont need to change
import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;
public class RandomCountingSort{
public static void main(String[] args){
int k, n;
int min = 0; // lower bound limit
Scanner keyBoard = new Scanner(System.in);
Random rand = new Random();
System.out.print("Enter upper bound of integers: ");
k = keyBoard.nextInt();
System.out.print("Enter number of integers: ");
n = keyBoard.nextInt();
int[] array = new int[n];
// random numbers
for (int i = 0; i < n; i++){
array[i] = rand.nextInt((k + min) + 1) + min;
}
// displaying the data in array
// unsort
System.out.print("The random generated data in array : ");
for (int i = 0; i < n; i++){
System.out.print(array[i] + " ");
}
// soring the array
Arrays.sort(array);
// displaying the data in array
System.out.print(" The data in array after sorting: ");
for (int i = 0; i < n; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}
Explanation / Answer
Hi,
If you want to implement the count sort algorithm in Java programming, I would suggest to implement the algorithm in a java method yourself as I have done below and you do not need to use the "sort()" method of java,util.Arrays class. The below code will help you write your own code for count sort algorithm.
Java code for implementing Count Sort-
import java.util.Scanner;
public class CountingSort
{
private static final int MAX_RANGE = 1000;
/** Counting Sort function **/
public static void sort( int[] arr )
{
int N = arr.length;
if (N == 0)
return;
/** find max and min values **/
int max = arr[0], min = arr[0];
for (int i = 1; i < N; i++)
{
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
int range = max - min + 1;
if (range > MAX_RANGE)
{
System.out.println(" Error : Range too large for sort");
return;
}
int[] count = new int[range];
/** make count/frequency array for each element **/
for (int i = 0; i < N; i++)
count[arr[i] - min]++;
/** modify count so that positions in final array is obtained **/
for (int i = 1; i < range; i++)
count[i] += count[i - 1];
/** modify original array **/
int j = 0;
for (int i = 0; i < range; i++)
while (j < count[i])
arr[j++] = i + min;
}
/** Main method **/
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
System.out.println("Counting Sort Test ");
int n, i;
System.out.println("Enter number of integer elements");
n = scan.nextInt();
int arr[] = new int[ n ];
/** Accept elements **/
System.out.println(" Enter "+ n +" integer elements");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
sort(arr);
System.out.println(" Elements after sorting ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.