Bubble Sort Study and Guide| 氣泡排序研究以及教學指引

更新 發佈閱讀 13 分鐘

Why to Study Bubble Sort?

I teach senior high students bubble sort every semster. However one day I talking to my friend, it turned out that there's so many versions, so I delve into it to explore the history of bubble sort.

The Bubble Sort We Know

Bubble sort is a very common and easy-to-learn algorithm. Assuming you're going to sort an array of numbers, and you want the order ascending, from the smallest to the largest. Then, you start from the left and compare the adjacent numbers, if the order is incorrect (in this case, the left is greater than the right), exchange them. We call it one stage. We can continue the same process for at most n-1 stages; then, the array would be sorted.

We can write the pseudocode as follows:

Repeat n-1 times:
for index i from first to the last two:
if array[i] > array[i + 1]:
exchange the position of the nubmers.

I know, you may have heard many other version, especially a little refinment of the above. That is in the k-th stage we start from the first one to the n-k one.

So the pseudocode is revised to:

Repeat n-1 times:
for index i from first to the n-k index:
if array[i] > array[i + 1]:
exchange the position of the nubmers.

History of Bubble Sort

However, one of my friends told me that the very original version is "backward" instead of the "upward" version, which we generally learned.

As a result I went to find the history of bubble sort and its original paper. Here's two key papers:

  • [1] Sorting on Electronic Commputer Systems. Edward Harry Friend, 1955.
  • [2] A Programming Language. Kenneth E. Iverson, 1962.

Friend [1] used "Exchanging" to describe bubble sort. Plus, he stated that the sort start from the beginning, which is "upward".

At the beginning of each pass the first abbreviated item is compared with the second and the smaller assumes first position.

Iverson [2] on the other hand first deliver the word "bubble sort" and stated that the comparsion begins from the buttom of the numbers, and the smaller goes up, which is "backward".

The basic operation of the bubble sort is the comparison and possible transposition of a pair of adjacent items so as to place the smaller of the two keys earlier in the sequence. The first stage consists of such operations performed in sequence on the item pairs (xᵥ₋₁, xᵥ), (xᵥ₋₂, xᵥ₋₁), ⋯, (x₁, x₂) .
bubble sort example from Iverson, 1956

bubble sort example from Iverson, 1956

Some facts:

  • Considering the efficiency, no difference between upward and backward.
  • The original "bubble sort" [2] is backward instead of upward that we commonly know.
  • Both papers have the termination condition if the vector is already sorted during the process, terminate the process. It's usually excluded in text book when first introduce the bubble sort.

How to Introduce Bubble Sort Correctly

So, how to introduce bubble sort correctly based on these two papers? Here's the suggestion on my point of view.

First, explain the core concept of bubble sort, that is we sort the vector by comparing adjacent numbers, if the order is not what we want, then we exchange it. This concept is from Friend [1], the exchanging thought.

Secondly, we tell the history about Iverson [2], who first deliver the word "bubble sort". Imagine there are many bubbles under the sea, from the buttom, the lighter goes up and the heavier goes down. It means that we compare the numbers from the last one and eventually the vector becomes ascending. In this moment, if you are not going to write the code or explain how to implement it, I think Iverson's version is better to understand why it's named "bubble sort".

Third, if you want to continue to implementation, you can chose upward or backward. Here I often teach my student in upward way.

Teaching Guide

Here's the example that how I gradually teach my students bubble sort concept.

The first one is a trad-off version for efficiency, which is very easy to understand meanwhile wasting time. (However the CPU runs very fast, so it doesn't matter.)

nums = [1, 2, 9, 8, 3]
n = len(nums)
t = 0
for k in range(n - 1):
for i in range(n - 1):
t += 1
if (nums[i] > nums[i + 1]):
nums[i], nums[i + 1] = nums[i + 1], nums[i]
print(f'{nums = }, compare {t} times.')
# nums = [1, 2, 3, 8, 9], compare 16 times.

Next I will say that as we can see in the k-th stage, the biggest k-1 numbers are fixed, so we don't need to compare them.

nums = [1, 2, 9, 8, 3]
n = len(nums)
t = 0
for k in range(n - 1):
for i in range(n - k - 1):
t += 1
if (nums[i] > nums[i + 1]):
nums[i], nums[i + 1] = nums[i + 1], nums[i]
print(f'{nums = }, compare {t} times.')
# nums = [1, 2, 3, 8, 9], compare 10 times.

Finally, in some situation the vector has already sorted, no exchange during a stage, then we can terminate it, which optimize it more.

nums = [1, 2, 9, 8, 3]
n = len(nums)
t = 0
for k in range(n-1):
hasSwapped = False
for i in range(n-k-1):
t += 1
if (nums[i] > nums[i + 1]):
hasSwapped = True
nums[i], nums[i + 1] = nums[i + 1], nums[i]
if not hasSwapped:
break
print(f'{nums = }, compare {t} times.')
# nums = [1, 2, 7, 8, 9], compare 9 times.

Feel free to use these as material when teaching. But for commercial use please contact me.




留言
avatar-img
留言分享你的想法!
avatar-img
冠曄的資訊小教室
0會員
6內容數
I'm a senior high school teacher, teaching computer science. Here I will share my concept, thoughts and material for technology education.
2025/04/09
我出了一本書,書名是:Get A++ in Your C++ Course: A Practical Guide for Beginner 雖然書名是英文,但其實內容是中文。之所以寫這本,是因為我教資訊快要滿四年了,但廠商給的配套本一直以來都不盡人意。
Thumbnail
2025/04/09
我出了一本書,書名是:Get A++ in Your C++ Course: A Practical Guide for Beginner 雖然書名是英文,但其實內容是中文。之所以寫這本,是因為我教資訊快要滿四年了,但廠商給的配套本一直以來都不盡人意。
Thumbnail
2025/03/29
幾天前在國中當數學老師的同學問我:冠曄,我有一個學生錯題統計、也把每個題目變成獨立 PDF 了,你有辦法根據錯題幫每個人合併一個錯題本嗎? 很有趣!我馬上跟他要了範例檔案,然後當天下課就用 AI 做出來了,以網頁形式呈現,有需要的人歡迎自由使用!連結在最文章最下方。
Thumbnail
2025/03/29
幾天前在國中當數學老師的同學問我:冠曄,我有一個學生錯題統計、也把每個題目變成獨立 PDF 了,你有辦法根據錯題幫每個人合併一個錯題本嗎? 很有趣!我馬上跟他要了範例檔案,然後當天下課就用 AI 做出來了,以網頁形式呈現,有需要的人歡迎自由使用!連結在最文章最下方。
Thumbnail
2025/03/28
近期 ChatGPT 更新圖片生成功能,原本用 DALL.E 進行生成,現在改為直接從 GPT-4o 生成。而且可以調整得更細緻。 What's New? 圖片生成雖然很酷,但一直以來遇到一些問題,例如,文字會亂掉、繼續補充說明會跟原圖迥異等。這些在這次的更新都解決了。
Thumbnail
2025/03/28
近期 ChatGPT 更新圖片生成功能,原本用 DALL.E 進行生成,現在改為直接從 GPT-4o 生成。而且可以調整得更細緻。 What's New? 圖片生成雖然很酷,但一直以來遇到一些問題,例如,文字會亂掉、繼續補充說明會跟原圖迥異等。這些在這次的更新都解決了。
Thumbnail
看更多
你可能也想看
Thumbnail
最近整理了一下蝦皮的訂單紀錄,發現自己這一年默默也回購了不少好用的東西~從生活療癒系的小物、實用的除臭保養、到打掃家裡必備的清潔用品都有。這次也順便一起整理了「準備在雙12補貨的清單」,如果剛好你也有在找類似的品項,希望這篇可以給你一些參考。 秘魯進口 高油版聖木 這款聖木我覺得很棒,就是聞起來
Thumbnail
最近整理了一下蝦皮的訂單紀錄,發現自己這一年默默也回購了不少好用的東西~從生活療癒系的小物、實用的除臭保養、到打掃家裡必備的清潔用品都有。這次也順便一起整理了「準備在雙12補貨的清單」,如果剛好你也有在找類似的品項,希望這篇可以給你一些參考。 秘魯進口 高油版聖木 這款聖木我覺得很棒,就是聞起來
Thumbnail
在這個網購紅海時代,你是否也跟作者一樣,最後總是點開蝦皮?從拯救文字工作者的老腰的護脊椅墊,到深夜腦力激盪必備的韓國不倒翁泡麵,作者分享了雙12購物清單中的兩樣「生存」與「靈魂」好物。更重要的是,文章揭露了「蝦皮分潤計畫」如何讓創作者低門檻變現,填滿你的小荷包,實現數位遊牧的夢想。
Thumbnail
在這個網購紅海時代,你是否也跟作者一樣,最後總是點開蝦皮?從拯救文字工作者的老腰的護脊椅墊,到深夜腦力激盪必備的韓國不倒翁泡麵,作者分享了雙12購物清單中的兩樣「生存」與「靈魂」好物。更重要的是,文章揭露了「蝦皮分潤計畫」如何讓創作者低門檻變現,填滿你的小荷包,實現數位遊牧的夢想。
Thumbnail
生完寶寶後 我真的深刻感受到一句話: 「睡不好,皮膚就先離家出走」 常常半夜起來哄寶寶、睡眠不規律 膚況也跟著黯淡、失去彈性>< 身為剛生完孩子的新手媽媽,我現在最在意的兩件事就是: 皮膚要彈潤、睡眠要穩定!! 這陣子我開始嘗試日夜搭配的膠原蛋白: 🌞 TIMESEAL 日間款
Thumbnail
生完寶寶後 我真的深刻感受到一句話: 「睡不好,皮膚就先離家出走」 常常半夜起來哄寶寶、睡眠不規律 膚況也跟著黯淡、失去彈性>< 身為剛生完孩子的新手媽媽,我現在最在意的兩件事就是: 皮膚要彈潤、睡眠要穩定!! 這陣子我開始嘗試日夜搭配的膠原蛋白: 🌞 TIMESEAL 日間款
Thumbnail
發現每天固定一個小動作,肌膚整體狀態真的會更穩定,照鏡子的心情也跟著好起來。 早上我習慣吃一包 TIMESEAL 早安膠原蛋白,粉末狀、很好入口,使用小分子技術,搭配維生素C與專利原料 ( 雙胜肽膠原、PANMOL® NADH)。對我來說,就是先把一天的彈潤感打好底,也讓整天狀態更有精神。 晚
Thumbnail
發現每天固定一個小動作,肌膚整體狀態真的會更穩定,照鏡子的心情也跟著好起來。 早上我習慣吃一包 TIMESEAL 早安膠原蛋白,粉末狀、很好入口,使用小分子技術,搭配維生素C與專利原料 ( 雙胜肽膠原、PANMOL® NADH)。對我來說,就是先把一天的彈潤感打好底,也讓整天狀態更有精神。 晚
Thumbnail
在進行SQL查詢邏輯更改時,需要適當地使用SubQuery和join來達到新的排序需求。本文將介紹原本的撈取邏輯、需求以及如何使用SubQuery來解決新的排序需求。
Thumbnail
在進行SQL查詢邏輯更改時,需要適當地使用SubQuery和join來達到新的排序需求。本文將介紹原本的撈取邏輯、需求以及如何使用SubQuery來解決新的排序需求。
Thumbnail
題目敘述 Sort Colors 給定一個色彩陣列,裡面的顏色包含0紅色,1白色,2藍色。 要求我們透過in-place操作,把色彩陣列依序從左到右排好, 依序出現的是紅色、白色、藍色。
Thumbnail
題目敘述 Sort Colors 給定一個色彩陣列,裡面的顏色包含0紅色,1白色,2藍色。 要求我們透過in-place操作,把色彩陣列依序從左到右排好, 依序出現的是紅色、白色、藍色。
Thumbnail
這篇文章介紹了排列和組閤中的錯位排列和排容原理,並提供了一種相對樸實的解題方法。透過例子詳細解釋了選擇情況下的數學原理,讓讀者能夠理解並吸收。文章通過課堂上難以推敲的題目,提出了一個相對簡單的方式來解題。 圖片選自@pngtree
Thumbnail
這篇文章介紹了排列和組閤中的錯位排列和排容原理,並提供了一種相對樸實的解題方法。透過例子詳細解釋了選擇情況下的數學原理,讓讀者能夠理解並吸收。文章通過課堂上難以推敲的題目,提出了一個相對簡單的方式來解題。 圖片選自@pngtree
Thumbnail
很明顯可以看到 parallelSort(T[], Comparator<T> 大概可以帶來 2.5 倍到接近 3 倍的效能增益 (和數量無關)。所以,結論是當需要處理大量資料的排序時,真的可以考慮使用 parallelSort(T[], Comparator<T>。
Thumbnail
很明顯可以看到 parallelSort(T[], Comparator<T> 大概可以帶來 2.5 倍到接近 3 倍的效能增益 (和數量無關)。所以,結論是當需要處理大量資料的排序時,真的可以考慮使用 parallelSort(T[], Comparator<T>。
Thumbnail
推薦《數學之前人人平等》這本書,在這篇文章會講到此書談到「對數學天分的看法」與「建構學習支架的教學方法」,最後分享對我教學上的一些影響。
Thumbnail
推薦《數學之前人人平等》這本書,在這篇文章會講到此書談到「對數學天分的看法」與「建構學習支架的教學方法」,最後分享對我教學上的一些影響。
Thumbnail
排序是EXCEL很常用很基礎的一個功能,他可以幫我們把資料依照指定的順序排列。 但通常我們使用都是以欄(直)的方向進行排序,其實EXCEL也可以依據列(橫)的方向進行排續哦😁 下圖是LINE社群網友提出的問題,想要把上圖的原始資料變成下圖。(相關問題可以加入LINE社群唷) 這時候用排序(尋
Thumbnail
排序是EXCEL很常用很基礎的一個功能,他可以幫我們把資料依照指定的順序排列。 但通常我們使用都是以欄(直)的方向進行排序,其實EXCEL也可以依據列(橫)的方向進行排續哦😁 下圖是LINE社群網友提出的問題,想要把上圖的原始資料變成下圖。(相關問題可以加入LINE社群唷) 這時候用排序(尋
Thumbnail
不同的運算子一起出現時,會根據其優先性(Precedence)來決定誰先誰後。MDN也很貼心的整理成表格了。 雖然有表格可以供我們查找,但是總不可能每一次用到運算子的時候,我們都去查找吧?我們最好對這張表有直觀上的理解與記憶。 那麼就讓我來分享我如何理解與記憶這張表吧! 運算子在做甚麼
Thumbnail
不同的運算子一起出現時,會根據其優先性(Precedence)來決定誰先誰後。MDN也很貼心的整理成表格了。 雖然有表格可以供我們查找,但是總不可能每一次用到運算子的時候,我們都去查找吧?我們最好對這張表有直觀上的理解與記憶。 那麼就讓我來分享我如何理解與記憶這張表吧! 運算子在做甚麼
Thumbnail
排版微調 VOL.1-詳細解說版 排版微調是我社群的一個系列內容,這篇則是提供給訂閱會員的詳細解說版,會說明為何調整的原因跟我的看法,以及原本設計可能有的問題,如果你是設計初學者那這份內容會很適合你,因為會很細節的去講解排版原因。
Thumbnail
排版微調 VOL.1-詳細解說版 排版微調是我社群的一個系列內容,這篇則是提供給訂閱會員的詳細解說版,會說明為何調整的原因跟我的看法,以及原本設計可能有的問題,如果你是設計初學者那這份內容會很適合你,因為會很細節的去講解排版原因。
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News