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

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.

主站蜘蛛池模板: 香格里拉县| 顺义区| 涟水县| 河北省| 太仆寺旗| 即墨市| 博乐市| 星座| 民丰县| 新龙县| 含山县| 墨脱县| 广德县| 榆林市| 仙游县| 新河县| 祁阳县| 福泉市| 阿巴嘎旗| 江山市| 安新县| 泗阳县| 武鸣县| 房产| 寻乌县| 加查县| 新宾| 祁门县| 扎鲁特旗| 四川省| 志丹县| 漠河县| 衡水市| 灵石县| 东山县| 兴宁市| 思南县| 和田县| 司法| 准格尔旗| 阿勒泰市|