gpt4 book ai didi

java - ComparisonCountingSort伪代码难度

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:37:36 25 4
gpt4 key购买 nike

类里面给我们的这个算法:

enter image description here

当输入是数组 A[60,35,81,98,14,47] 时,我在尝试确定输出时遇到了问题。

我想习惯伪代码语法,所以我尝试将其转换为 Java 语法,但生成的程序不会显示结果。这是我转换后的代码:

public class Driver
{
public static void main(String[] args)
{
int[] test = {60, 35, 81, 98, 14, 47};
int[] converse = new int[6];

converse = countSort(test);

for(int i : converse)
{
System.out.println(converse[i]);
}
}

public static int[] countSort(int[] a)
{
int[] s = new int[6];
int[] count = new int[6];
int n = a.length + 1;

for(int i = 0; i < (n-1);)
{
count[i] = 0;
}

for(int i = 0; i < (n-2);)
{
for(int j = (i+1); i < (n-1);)
{
if(a[i] < a[j])
{
count[j] = count[j]+1;//What
}
else
{
count[i] = count[i]+1;
}
}
}

for (int i = 0; i < (n-1);)
{
s[count[i]] = a[i];
}

return s;
}
}

最佳答案

int[] test = {60,35,81,98,14,47};从未使用过。

你是想通过 test进入countSort ?并对它的结果做一些事情。

虽然我没有真正看过算法,但这里还有其他问题:

int n = a.length + 1;
for (int i = 0; i < (n - 1);) {
count[i] = 0;
}

这将无限循环。 i从不鼓励。

关于java - ComparisonCountingSort伪代码难度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32567609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com