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

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.

主站蜘蛛池模板: 湖州市| 凌云县| 长顺县| 中西区| 牟定县| 西盟| 佛学| 北辰区| 佳木斯市| 龙海市| 图们市| 博罗县| 莫力| 晋城| 松溪县| 湖南省| 昌邑市| 广安市| 长子县| 阳信县| 安多县| 许昌市| 绥棱县| 息烽县| 柏乡县| 镇巴县| 新密市| 浮山县| 五河县| 六枝特区| 涞水县| 方正县| 应用必备| 湟中县| 虹口区| 无极县| 安西县| 穆棱市| 福州市| 南溪县| 叙永县|