【发布时间】:2013-09-09 16:08:17
【问题描述】:
好的,我今天已经为此工作了一段时间,我知道它非常接近完成。我一直在试图找到一个能够最终返回列表的转义子句。这是我处理过的所有代码。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package algorithm;
import java.util.Arrays;
import java.util.Collections;
/**
*
*
*/
public class Algorithm {
/**
* @param args the command line arguments
*/
private static int list[] = {10, 9, 8, 7, 6};
public static void main(String[] args) {
Algorithm alg = new Algorithm();
alg.bubblesort(list);
}
public Algorithm() {
}
public int[] bubblesort(int[] i) {
for (int a = 0; a < i.length;) {
for (int b = 1; b < i.length + 1;) {
int currentNumber = i[a];
if (b < i.length) {
if (currentNumber > i[b]) {
i[a] = i[b];
i[b] = currentNumber;
a++;
b++;
} else if (currentNumber < i[b]) {
a++;
b++;
}
} else if (b == i.length) {
a = 0;
b = 1;
}
}
}
return i;
}
public void isSorted(int[] i) {
for (int x = 0; x < i.length;) {
if (i[x + 1] < i[i.length - 1]) {
if (i[x] < i[x + 1]) {
x++;
}
}
}
System.out.println(Arrays.toString(i));
}
}
好的,我的问题是如何才能返回最终的排序列表?
【问题讨论】:
-
@StephenD 只要努力表现出功课,他就没有错。
-
你的问题是......
-
这看起来不像冒泡排序;冒泡排序应该只比较和交换彼此相邻的项目。你能用文字解释一下你的算法吗?
-
哦该死我的问题没有显示:哦,我的错
-
@Joni 我正在比较列表中彼此相邻的项目。如果 (currentNumber > i[b]) { i[a] = i[b]; i[b] = 当前编号;一个++; b++;
标签: java arrays int bubble-sort