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

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.

主站蜘蛛池模板: 固安县| 金川县| 永顺县| 桑日县| 北海市| 陆良县| 沅陵县| 泰安市| 抚顺县| 班戈县| 汾阳市| 科尔| 开平市| 潞城市| 拉萨市| 六安市| 察哈| 永康市| 盐亭县| 彩票| 五河县| 大关县| 南昌县| 六枝特区| 青冈县| 苗栗市| 佳木斯市| 沈丘县| 淳化县| 大关县| 荔波县| 深圳市| 资源县| 陆河县| 芜湖市| 元氏县| 伊金霍洛旗| 庐江县| 商都县| 乌拉特中旗| 东源县|