据我所知,我在这里实现了基本的插入排序。输出是相同的数组,未排序。我是否正确使用了 compareTo ?我不确定大于或小于零的某个数字意味着什么。
import java.util.Arrays;
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int lineNumber = 5;
int i,j;
String key;
String[] inputArray = {"E","D","C","B","A","B"};
System.out.println(Arrays.toString(inputArray));
for (j = 1; j < lineNumber; j++) {
key = inputArray[j];
i = j - 1;
while (i >= 0) {
if (key.compareTo(inputArray[i]) < 0) {
break;
}
inputArray[i + 1] = inputArray[i];
i--;
}
inputArray[i + 1] = key;
System.out.println(Arrays.toString(inputArray));
}
System.out.println(Arrays.toString(inputArray));
}
跑:
[E, D, C, B, A, B]
[E, D, C, B, A, B]
[E, D, C, B, A, B]
[E, D, C, B, A, B]
[E, D, C, B, A, B]
[E, D, C, B, A, B]
BUILD SUCCESSFUL (total time: 0 seconds)