官术网_书友最值得收藏!

Understanding the algorithm and language constructs

The algorithm was explained at the start of the chapter. The implementation is in the Sort class inside the sort method, and it is only a few lines:

        int n = names.length; 
while (n > 1) {
for (int j = 0; j < n - 1; j++) {
if (names[j].compareTo(names[j + 1]) > 0) {
final String tmp = names[j + 1];
names[j + 1] = names[j];
names[j] = tmp;
}
}
n--;
}

The n variable holds the length of the array at the start of the sorting. Arrays in Java always have a property that gives the length and it is called length. When we start the sorting, we will go from the start of the array to the end of it and, as you may recall, the last element, Wilson, will walk up to the last position during this first iteration. Subsequent iterations will be shorter and, therefore, the variable n will be decreased.

主站蜘蛛池模板: 徐汇区| 巴林右旗| 香格里拉县| 高阳县| 墨玉县| 双城市| 调兵山市| 宁海县| 同仁县| 霸州市| 中阳县| 沙湾县| 察隅县| 桑日县| 永定县| 彩票| 清水县| 桃园市| 桐庐县| 东乌| 华池县| 兰坪| 通辽市| 兴业县| 贵州省| 孝感市| 灵石县| 会同县| 江津市| 乌海市| 河西区| 洛隆县| 抚宁县| 文化| 麦盖提县| 长子县| 马边| 高阳县| 贵南县| 阜城县| 西安市|