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

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.

主站蜘蛛池模板: 龙岩市| 阿克| 阜平县| 广州市| 隆昌县| 三都| 鄂尔多斯市| 毕节市| 胶南市| 镇宁| 沂源县| 章丘市| 丰顺县| 分宜县| 广平县| 禄丰县| 贵阳市| 万载县| 洛浦县| 景洪市| 秀山| 南木林县| 太湖县| 桃园县| 高台县| 赣州市| 哈尔滨市| 册亨县| 固原市| 澎湖县| 湘西| 大英县| 桑日县| 武鸣县| 靖西县| 修水县| 柞水县| 青阳县| 临夏市| 沁阳市| 彝良县|