- 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.
推薦閱讀
- Visual Basic程序設計教程
- SEO智慧
- Instant RubyMotion App Development
- 從零開始學Linux編程
- Learning AngularJS for .NET Developers
- Hands-On Full Stack Development with Spring Boot 2.0 and React
- CRYENGINE Game Development Blueprints
- 創意UI:Photoshop玩轉APP設計
- HTML+CSS+JavaScript網頁制作:從入門到精通(第4版)
- Practical Predictive Analytics
- INSTANT JQuery Flot Visual Data Analysis
- PHP Microservices
- 從零開始構建深度前饋神經網絡:Python+TensorFlow 2.x
- VMware vSphere Design Essentials
- 深入理解MySQL主從原理