- Java 9 Programming By Example
- Peter Verhas
- 154字
- 2021-07-02 23:37:32
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.
推薦閱讀
- Unity 2020 By Example
- Learning LibGDX Game Development(Second Edition)
- DevOps with Kubernetes
- TensorFlow Lite移動端深度學習
- Python程序設計(第3版)
- C語言程序設計基礎與實驗指導
- R語言數據可視化之美:專業圖表繪制指南
- 看透JavaScript:原理、方法與實踐
- 新手學Visual C# 2008程序設計
- JSP開發案例教程
- Mastering KnockoutJS
- Android底層接口與驅動開發技術詳解
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- Python Essentials
- Java編程從入門到精通