Replies: 13 comments 4 replies
-
如果像冒泡排序那一样给出示例代码就好了 |
Beta Was this translation helpful? Give feedback.
-
我交了,还没有审核完? |
Beta Was this translation helpful? Give feedback.
-
@MrFoodinChina 请问您发 pull request 了吗,只有发 pull request 管理才能看到你的修改,并合入主线 |
Beta Was this translation helpful? Give feedback.
-
F**k,我TMD的没交!谢谢提醒,已经交上去了 |
Beta Was this translation helpful? Give feedback.
-
@ToHolyForYou 您好,如果你认为代码出现错误,可以开一个issue指出问题,或者开一个pull request 来修正错误的部分 |
Beta Was this translation helpful? Give feedback.
-
实例代码有问题,给一个能用的
|
Beta Was this translation helpful? Give feedback.
-
实际上可以先用二分找到要插入的位置,再进行插入,虽然复杂度与示例代码相同,但减少了许多比较,常数会更小。 |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
这个写法好像飞块。。 |
Beta Was this translation helpful? Give feedback.
-
为什么没有Java的示例代码呢 |
Beta Was this translation helpful? Give feedback.
-
java插入排序: class Solution {
// 插入排序
public int[] sortArray(int[] nums) {
for(int i = 1; i < nums.length; i++){
for(int j = i; j > 0; j--){
if(nums[j] < nums[j - 1]) {
int temp = nums[j];
nums[j] = nums[j - 1];
nums[j - 1] = temp;
}
}
}
return nums;
}
} 二分查找优化:注意,找到从右向左第一个小于等于【!!!确保稳定性】当前值的位置。 class Solution {
// 插入排序
public int[] sortArray(int[] nums) {
for(int i = 1; i < nums.length; i++){
int left = 0, right = i - 1;
// binary search
while(left <= right){
int mid = left + ((right - left) >> 1);
if(nums[mid] > nums[i]){
right = mid - 1;
}else {
left = mid + 1;
}
}
int temp = nums[i];
// 移动
for(int j = i; j > left; j--){
nums[j] = nums[j - 1];
}
nums[left] = temp;
}
return nums;
}
} 复杂度为什么不变? |
Beta Was this translation helpful? Give feedback.
-
这是我用while()循环写的,请点评: |
Beta Was this translation helpful? Give feedback.
-
https://oi-wiki.org/basic/insertion-sort/
OI Wiki 是一个编程竞赛知识整合站点,提供有趣又实用的编程竞赛知识以及其他有帮助的内容,帮助广大编程竞赛爱好者更快更深入地学习编程竞赛
Beta Was this translation helpful? Give feedback.
All reactions