java中的字符比较
我有一个数组java中的字符比较,java,character,Java,Character,我有一个数组 char[] A = {'a','c','e','b','d'}; 我想按升序排列数组,即 A ={'a','b','c','d','e'}; 如何在java中实现这个简单的操作符不会给我正确的答案。 代码: for(int i=A.length-1;i>=1;i--) { if(A[i]提示:您只通过数组一次。这是不够的 如果您不理解我的意思,请尝试使用输入数组“手动执行”代码,该数组的字母顺序与所需的相反 通过整个数组是否可以得到一个正确排序的数组 它到底做了什么 那么你
char[] A = {'a','c','e','b','d'};
我想按升序排列数组,即
A ={'a','b','c','d','e'};
如何在java中实现这个简单的
操作符不会给我正确的答案。
代码:
for(int i=A.length-1;i>=1;i--)
{
if(A[i]提示:您只通过数组一次。这是不够的
如果您不理解我的意思,请尝试使用输入数组“手动执行”代码,该数组的字母顺序与所需的相反
- 通过整个数组是否可以得到一个正确排序的数组
- 它到底做了什么
- 那么你现在需要做什么
看来您正在尝试的是我们所称的泡泡排序的开始。您只对字符列表进行了一次遍历,并且只交换了部分字符。实际上,您必须重复此过程,直到没有交换发生
可以找到有关Bubblesort
ing的更多信息。特别是有一些伪代码概述了这一概念:
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
用代码提供你的尝试。我们不会解决你的练习/家庭作业。@Luigimendoza我有简单的循环,并使用
运算符。不要描述你的代码。你的问题并提供代码。@Luigimendoza我恐怕有人会做这个家庭作业-可能性与你的预测相反。现在我们需要更多的代码。什么是word
变量,如何声明、初始化和可能填充?
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure