How to Bubble Sort

复制链接 & 嵌入
链接
复制链接
嵌入网页
复制嵌入代码
进入全屏模式
载入作品
正在创建积木……
载入角色……
载入声音……
加载扩展……
正在创建积木……
呼唤小猫……
传送Nano……
给Gobo充气 …
准备表情……
还没有人打赏!
最后修改于2024年02月19日

指导教师

操作说明

WHEN THE ARROW APPEARS IN THE BOTTOM-RIGHT CORNER OF THE SCREEN, CLICK ANYWHERE TO ADVANCE. WHEN NARRATION IS ON, THE PRESENTATION WILL ADVANCE AUTOMATICALLY.

THIS EXPLAINS HOW YOU WOULD GO ABOUT SORTING A LIST OF NUMBERS IN ORDER FROM SMALLEST TO LARGEST, USING A SORTING ALGORITHM CALLED BUBBLE SORT.

I HAVE COVERED OTHER MORE EFFICIENT AND COMPLEX SORTING ALGORITHMS TOO, JUST CHECK OUT MY PROFILE!

THE NAME COMES FROM THE FACT THAT THE BIGGEST ITEMS FLOAT UP TO THE END OF THE LIST, KIND OF LIKE A BUBBLE.

THERE'S ALSO A VARIANT OF THIS ALGORITHM THAT IS BIDIRECTIONAL, CALLED SHAKER SORT.

SEE INSIDE FOR A PREBUILT BUBBLE SORT ALGORITHM. HOWEVER, I WOULD ENCOURAGE YOU TO SEE IF YOU CAN PROGRAM ONE ON YOUR OWN AFTER WATCHING THIS, FIRST!

...OR MAYBE YOU'RE JUST HERE TO SEE THE BUBBLES GRAPHIC, WHICH, BY THE WAY, YOU CAN FEEL FREE TO USE IN YOUR PROJECTS!

ALSO, THANKS SO MUCH FOR FEATURING THIS PROJECT, SCRATCH TEAM!

备注与谢志

https://scratch.mit.edu/projects/942534934/

开发日志

扩展

文字朗读

 

变量

visible [Caption]

 

角色

Caption

代码

当 绿旗 被点击

将 {visible} 设为 false

 

当接收到 next caption

将 {visible} 设为 true

 

当接收到 narrate

朗读 Bubble sort.

等待 ({visible} = true)

朗读 To sort a list of values from smallest to largest with bubble sort,

执行 auto advance

朗读 we start by looking at the first item in the list,

等待 0.4 秒

执行 auto advance

朗读 plus the item right after it.

等待 0.6 秒

执行 auto advance

朗读 If the item to the left is greater than the item to the right, we will swap them.

执行 auto advance

朗读 And if the item to the right is greater, we leave them.

等待 0.5 秒

执行 auto advance

朗读 Now, we move on to the next item in the list and repeat, until we reach the last item.

执行 auto advance

朗读 This will lead to the item with the greatest value being shifted over to the end of the list, its final position.

等待 0.4 秒

执行 auto advance

朗读 This item now exits the sorting process.

执行 auto advance

朗读 Now we just repeat this entire process starting from the beginning again,

朗读 but this time, up until the second to last item, which is currently the last item that isn't already in its final position.

等待 0.4 秒

执行 auto advance

朗读 Now the second greatest item is in its final position.

执行 auto advance

朗读 We'll rinse and repeat again, this time until the third to last item.

执行 auto advance

朗读 As you can see, this process just repeatedly loops over all the items that aren't already in their final position yet,

朗读 until there's nothing left to swap.

执行 auto advance

朗读 All done! The data is in order from smallest to largest.

等待 1 秒

执行 auto advance

 

定义 auto advance

将 {visible} 设为 false

广播 auto advance

等待 ({visible} = true)

 

Next

代码

当按下 空格 键

如果 (x坐标 < 240) 那么

停止 该角色的其他脚本

广播 advance

在 0.09 秒内滑行到 x:270 y:-151

如果 (Narration 的 造型编号 = 1) 那么

如果 (x坐标 < 240) 那么

停止 该角色的其他脚本

广播 advance

在 0.09 秒内滑行到 x:270 y:-151

 

当按下 → 键

如果 (x坐标 < 240) 那么

停止 该角色的其他脚本

广播 advance

在 0.09 秒内滑行到 x:270 y:-151

如果 (Narration 的 造型编号 = 1) 那么

如果 (x坐标 < 240) 那么

停止 该角色的其他脚本

广播 advance

在 0.09 秒内滑行到 x:270 y:-151

 

当接收到 auto advance

停止 该角色的其他脚本

广播 advance

在 0.09 秒内滑行到 x:270 y:-151

 

当接收到 advance

演奏音符 66 0.03 拍

如果 (Narration 的 造型编号 = 1) 那么

演奏音符 66 0.03 拍

 

当接收到 wait for input

显示

如果 (Narration 的 造型编号 = 1) 那么

显示

重复执行直到 按下鼠标?

重复执行直到 (按下鼠标? 与 (Narration 的 造型编号 = 1))

 

当接收到 advance

演奏音符 67 0.15 拍

演奏音符 74 0.25 拍

如果 (Narration 的 造型编号 = 1) 那么

演奏音符 67 0.15 拍

演奏音符 74 0.25 拍

 

Narration