lsgwr / algorithms Goto Github PK
View Code? Open in Web Editor NEW算法学习与总结,包括慕课网刘宇波老师、算法竞赛入门经典、挑战程序设计竞赛、算法竞赛宝典等
License: Other
算法学习与总结,包括慕课网刘宇波老师、算法竞赛入门经典、挑战程序设计竞赛、算法竞赛宝典等
License: Other
public static void sort(Comparable[] arr) {
int n = arr.length;
// 注意下标从1开始,不停往前比较,下标为0的元素默认有效
for (int i = 1; i < n; ++i) {
// 先把起始位置的元素暂存
Comparable tmp = arr[i];
// 从位置i开始,从前挨个比较(前面已经是有序地了,这个是前提),一直比较到前面的某个元素比自己小就停下
for (int j = i; j > 0; j--) {
if (tmp.compareTo(arr[j - 1]) < 0) {
// 起始位置元素比前面的小,就把前面的值附到后面来元素上
arr[j] = arr[j - 1];
} else {
// 一直到j前面的元素j-1小于tmp了,说明升序排列完成,把之前存的起始位置元素tmp插入到此处即可
arr[j] = tmp;
break;
}
}
}
当j=0时,不会进入for循环。arr[j] = tmp;应该写在内部循环的外面。
public static void sort(Comparable[] arr) {
int n = arr.length;
// 注意下标从1开始,不停往前比较,下标为0的元素默认有效
for (int i = 1; i < n; ++i) {
// 先把起始位置的元素暂存
Comparable tmp = arr[i];
int j = i;
// 从位置i开始,从前挨个比较(前面已经是有序地了,这个是前提),一直比较到前面的某个元素比自己小就停下
for (; j > 0; j--) {
if (tmp.compareTo(arr[j - 1]) < 0) {
// 起始位置元素比前面的小,就把前面的值附到后面来元素上
arr[j] = arr[j - 1];
} else {
// 一直到j前面的元素j-1小于tmp了,说明升序排列完成,把之前存的起始位置元素tmp插入到此处即可
break;
}
}
arr[j] = tmp;
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.