chapter_sorting/selection_sort/ #514
Replies: 16 comments 25 replies
-
k神好,分别包含n,n-1,...,2,2轮内循环,是不是多了一个2呀 |
Beta Was this translation helpful? Give feedback.
-
第三个特性 -- “非稳定排序”,在图例中显示了两个元素4的相对位置发生了改变。请问现实中什么时候会需要规避这种情况呢? |
Beta Was this translation helpful? Give feedback.
-
k神,我觉得最后一步交换元素可以有个小优化,在交换前判断 |
Beta Was this translation helpful? Give feedback.
-
// 将该最小元素与未排序区间的首个元素交换 如果未排序区间第一个值就是最小的,即最小元素与未排序区间首个元素是同一个,是不是就不用swap了? |
Beta Was this translation helpful? Give feedback.
-
代码里的注释感觉不是太准确。外层循环说是未排序区间内的,内层循环已经是从j=i+1开始了,前面这个i就不是未排序的区间吗? |
Beta Was this translation helpful? Give feedback.
-
这个非自适应排序要怎么理解?为什么是非自适应排序? |
Beta Was this translation helpful? Give feedback.
-
最后一个例子,是否可以更换一下呢?4 4 3 1 5 2,完整的排序后,4 4的相对位置没有发生改变。可以改为3 4 4 3 2例子。 |
Beta Was this translation helpful? Give feedback.
-
你好,关于java的例子,外层的循环,应该是 i<n,这样循环的区间就已经是[0, n-1],或者是 i<=n-1 |
Beta Was this translation helpful? Give feedback.
-
到这里我已经看得很吃力了,不是说看不懂就是感觉有一个屏障突破不了可能之前的一些代码没看,落下的债,先去写写题,看看视频,积累一些零碎的东西,过几天再来看,写blog的时候网站崩了,写的都清0我的心态也崩了,ok就这样,作者加油 |
Beta Was this translation helpful? Give feedback.
-
选择排序的不稳定性,这张示例图体现不出来,不稳定性应该是左边的数导致的,而不是因为后面的两个数。 |
Beta Was this translation helpful? Give feedback.
-
代码理解上看:复杂度求和应该是是,n(n-1)/2, 原句应改为:”即各轮外循环分别包含 n-1,n-2...........1轮内循环,求和为 n(n-1)/2 |
Beta Was this translation helpful? Give feedback.
-
python中,[0, n-1]不应该是range(n)吗? |
Beta Was this translation helpful? Give feedback.
-
感觉这个手动选择排序太麻烦,了解就够了 |
Beta Was this translation helpful? Give feedback.
-
def sort(nums):
for i in range(len(nums) - 1):
min_idx = i
for j in range(i + 1, len(nums)):
if nums[j] < nums[min_idx]:
min_idx = j
nums[i], nums[min_idx] = nums[min_idx], nums[i]
return nums
sort([1,3,32,4,1,-100,23,17,23,6,-7,2,9,-1000]) |
Beta Was this translation helpful? Give feedback.
-
// 选择排序
// 思想:每次循环记录最小值的下标,然后把它交换到前面去
void select_sort(int *arr, int length) {
int min, t;
for (int i = 0; i < length; i++) {
min = i;
for (int j = i + 1; j < length; j++) {
// 如果找到比 min 还小的数就交换
if (arr[j] < arr[min]) {
min = j;
}
}
t = arr[min], arr[min] = arr[i], arr[i] = t;
}
} |
Beta Was this translation helpful? Give feedback.
-
稍微提升了一点性能的选择排序def optimized_selection_sort(array: list[int]): |
Beta Was this translation helpful? Give feedback.
-
chapter_sorting/selection_sort/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_sorting/selection_sort/
Beta Was this translation helpful? Give feedback.
All reactions