chapter_sorting/bubble_sort/ #60
Replies: 23 comments 34 replies
-
冒泡的写法有不少,个人观点:可以想象冒泡的过程是从底部,慢慢的气泡变大直到浮出水面 。这样写是不是更合适一点呢。
|
Beta Was this translation helpful? Give feedback.
-
大佬,请问上面的图使用ppt画的吗? |
Beta Was this translation helpful? Give feedback.
-
我觉得外层 i 可以理解成“每轮要比较的次数”,这样内层 j 就能更好理解些 |
Beta Was this translation helpful? Give feedback.
-
我不知道在别的语言是啥样 |
Beta Was this translation helpful? Give feedback.
-
这次学习发现最后一次未交换位置的操作是待排序列中较大值和最大值碰面,基于这个优化了一点,不确定好不好
|
Beta Was this translation helpful? Give feedback.
-
您好,先不考虑算法优化增加标志的情况,我这种排序是什么算法?又像又不像冒泡排序
|
Beta Was this translation helpful? Give feedback.
-
想知道链式存储的冒泡排序 |
Beta Was this translation helpful? Give feedback.
-
冒泡排序的chunk里面是否少了一行return呢 |
Beta Was this translation helpful? Give feedback.
-
冒泡排序的C语言实现中,外循环初始化条件好像有点问题吧。 |
Beta Was this translation helpful? Give feedback.
-
//冒泡排序的改进版本2
//每次记录最后交换的位置(这里是最后交换的位置之前区间[0,lastSwap)都
//是有序的),只需要在从最后一个有序元素后一个元素继续循环交换即可
void bubbleSort2(int list[], int n){
int lastSwap=0;
int lastSwapTemp=0;
//最多冒泡n-1趟
for(int i = 0;i < n-1; i++){
lastSwap=lastSwapTemp;
//扩大有序区间
for (int j = n-1; j > lastSwap; j--) {
if (list[j-1]>list[j]) {
swap(list[j-1],list[j]);
lastSwapTemp = j;
}
}
if (lastSwap==lastSwapTemp) {
return;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
flag=False |
Beta Was this translation helpful? Give feedback.
-
C语言里面没有bool关键词,这是c++的 |
Beta Was this translation helpful? Give feedback.
-
请教一下大佬,“自适应”应该怎么理解。从搜索那一章开始提到这个概念,但是一直不大理解。 |
Beta Was this translation helpful? Give feedback.
-
大一的时候学数据结构的冒泡排序,学不明白;现在研一了,居然会有一种,为什么之前没看明白的感觉………… |
Beta Was this translation helpful? Give feedback.
-
选择排序如过改成每次选择个最大的,感觉就和这个类似了。 |
Beta Was this translation helpful? Give feedback.
-
为啥唯独没有Ruby版代码? |
Beta Was this translation helpful? Give feedback.
-
感觉加了flag,也得遍历一遍元素,为什么说能优化效率呢? |
Beta Was this translation helpful? Give feedback.
-
优化时间复杂度这一块,感觉外层循环用do while更好理解 function bubbleSort(arr: number[]): number[] {
let n = arr.length; //长度
let swapped: boolean = true;
do{
swapped = false;
for(let i = 0; i < n-1; i++){
if(arr[i]>arr[i+1]){
[arr[i], arr[i+1]] = [arr[i+1], arr[i]]
swapped = true;
n--;
}
}
}while(swapped)
return arr
} |
Beta Was this translation helpful? Give feedback.
-
void bubble_sort(int a[], int n){ |
Beta Was this translation helpful? Give feedback.
-
#include <stdio.h> int main() {
} void bubble_sort(int arr[], int len) {
} |
Beta Was this translation helpful? Give feedback.
-
def sort(nums):
for i in range(len(nums) - 1):
for j in range(i + 1, len(nums)):
if nums[i] > nums[j]:
nums[i], nums[j] = nums[j], 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.
-
public class BubbleSort {
} n 表示数组的长度。 创建一个待排序的数组 arr。 |
Beta Was this translation helpful? Give feedback.
-
def bubble_sort_with_lastswap(nums: list[int]):
nums = [1, 3, 32, 4, 1, -100, 23, 17, 23, 6, -7, 2, 9, -1000] |
Beta Was this translation helpful? Give feedback.
-
chapter_sorting/bubble_sort/
Your first book to learn Data Structure And Algorithm.
https://www.hello-algo.com/chapter_sorting/bubble_sort/
Beta Was this translation helpful? Give feedback.
All reactions