- 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.
推薦閱讀
- ASP.NET Core 5.0開發入門與實戰
- Mastering Ember.js
- Windows系統管理與服務配置
- 趣學Python算法100例
- 編寫高質量代碼:改善C程序代碼的125個建議
- SAS數據統計分析與編程實踐
- NoSQL數據庫原理
- Mastering Linux Security and Hardening
- Unity&VR游戲美術設計實戰
- 深度學習原理與PyTorch實戰(第2版)
- Distributed Computing in Java 9
- PyQt編程快速上手
- 深度學習入門:基于Python的理論與實現
- Java設計模式深入研究
- Python量子計算實踐:基于Qiskit和IBM Quantum Experience平臺