[英]Counting Sort implementation
Hello I am having difficulty implementing a counting sort method in java.您好,我在 Java 中实现计数排序方法有困难。 I believe the problem comes from the last two loops I have in the method.
我相信问题来自我在方法中的最后两个循环。 I am getting an ArrayIndexOutOfBounds exception : 8. I believe this comes from my second to last for loop when at index 5 the value is 8 but I am not sure how to resolve this.
我收到一个 ArrayIndexOutOfBounds 异常:8。我相信这来自我倒数第二个 for 循环,当索引 5 的值为 8 时,但我不知道如何解决这个问题。 Any help is appreciated.
任何帮助表示赞赏。 Thank you!
谢谢!
In my code k is the highest value in the input array.在我的代码中,k 是输入数组中的最大值。
Code:代码:
public static void main(String[] args) {
int [] arrayOne = {0,1,1,3,4,5,3,0};
int [] output = Arrays.copyOf(arrayOne, arrayOne.length);
System.out.println(Arrays.toString(arrayOne));
countingSort(arrayOne, output, 5);
System.out.println(Arrays.toString(output));
}
public static void countingSort(int[] input, int[] output , int k){
int [] temp = Arrays.copyOf(input, k+1);
for (int i = 0; i <= k; i++){
temp[i] = 0;
}
for (int j = 0; j <= input.length - 1; j++){
temp[input[j]] = temp[input[j]] + 1;
}
for (int i = 1; i <= k; i++){
temp[i] = temp[i] + temp[i-1];
}
for (int j = input.length; j >= 1; j--){
output[temp[input[j]]] = input[j];
temp[input[j]] = temp[input[j]] - 1;
}
}
The problem is in the first loop because the array temp
lenght is 6 and you are doing 7 interations in there.问题出在第一个循环中,因为数组
temp
长度为 6,并且您在其中进行了 7 次交互。
So at the end of the for
it is trying to do temp[6]=0
and the last position of your array is temp[5]
.因此,在
for
它的末尾尝试执行temp[6]=0
并且数组的最后一个位置是temp[5]
。
To fix this change your first loop to:要解决此问题,请将您的第一个循环更改为:
for (int i = 0; i < k; i++){
In the last loop you will get the same exception cause input[8]
doesn't exist.在最后一个循环中,您将获得相同的异常原因
input[8]
不存在。
this may help but try using the Arraya.sort() method.这可能会有所帮助,但请尝试使用 Arraya.sort() 方法。 eg:
例如:
//A Java program to sort an array of integers in ascending order.
// A sample Java program to sort an array of integers
// using Arrays.sort(). It by default sorts in
// ascending order
import java.util.Arrays;
public class SortExample
{
public static void main(String[] args)
{
// Our arr contains 8 elements
int[] arr = {13, 7, 6, 45, 21, 9, 101, 102};
Arrays.sort(arr);
System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}
example is a snippet from https://www.geeksforgeeks.org/arrays-sort-in-java-with-examples/示例是来自https://www.geeksforgeeks.org/arrays-sort-in-java-with-examples/的片段
import java.util.Arrays;
public class CountingSort {
public static void main(String[] args) {
int[] input = {0,1,1,3,4,5,3,0};
int[] output = new int[input.length];
int k = 5; // k is the largest number in the input array
System.out.println("before sorting:");
System.out.println(Arrays.toString(input));
output = countingSort(input, output, k);
System.out.println("after sorting:");
System.out.println(Arrays.toString(output));
}
public static int[] countingSort(int[] input, int[] output, int k) {
int counter[] = new int[k + 1];
for (int i : input) { counter[i]++; }
int ndx = 0;
for (int i = 0; i < counter.length; i++) {
while (0 < counter[i]) {
output[ndx++] = i;
counter[i]--;
}
}
return output;
}
}
Above code is adapted from: http://www.java67.com/2017/06/counting-sort-in-java-example.html以上代码改编自: http : //www.java67.com/2017/06/counting-sort-in-java-example.html
As per algorithm following implementation, I have prepared for the count sort technique根据实现后的算法,我已经为计数排序技术做好了准备
public static int[] countSort(int elements[]) {
int[] sorted = new int[elements.length+1];
int[] range = new int[getMax(elements)+1];
for(int i=0;i<range.length;i++) {
range[i] = getCount(i, elements);
try {
range[i] = range[i]+range[i-1];
}catch(ArrayIndexOutOfBoundsException ae) {
continue;
}
}
for(int i=0;i<elements.length;i++) {
sorted[range[elements[i]]] = elements[i];
range[elements[i]] = range[elements[i]]-1;
}
return sorted;
}
public static int getCount(int value,int[] elements) {
int count = 0;
for(int element:elements) {
if(element==value) count++;
}
return count;
}
public static int getMax(int elements[]) {
int max = elements[0];
for(int i=0;i<elements.length;i++) {
if(max<elements[i]) {
max = elements[i];
}
}
return max;
}
Please review and let me know if any feedback and it is more helpful.请查看并让我知道是否有任何反馈,这会更有帮助。 Note : Non-negative no won't support in the above implementation.
注意:非负 no 在上述实现中不支持。 don't use 0th index of the sorted array.
不要使用排序数组的第 0 个索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.