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
嶄新的台灣獨立調香師品牌Sunkronizo ,這個名稱源自希臘語「同步」的意思。讓香氛不單純只是氣味調製,更是個人風格的展現與靈魂意志延伸的一種溝通語言。 很適合接下來年底聖誕佳節送禮的試香組,以一星期中的日子來為全系列香氛產品命名, 是品牌創立後首個推出全系列概念作品...
Thumbnail
嶄新的台灣獨立調香師品牌Sunkronizo ,這個名稱源自希臘語「同步」的意思。讓香氛不單純只是氣味調製,更是個人風格的展現與靈魂意志延伸的一種溝通語言。 很適合接下來年底聖誕佳節送禮的試香組,以一星期中的日子來為全系列香氛產品命名, 是品牌創立後首個推出全系列概念作品...
Thumbnail
根據美國電影協會(MPA)主辦的「串流服務如何推動臺灣創意經濟」論壇內容,深入探討串流平臺對臺灣影視產業的影響、數據分析、政府政策建議、內容國際化策略,以及臺灣與「韓流」的差距。文章提出 awwrated 在串流生態系中的潛在角色,強調數據、策略與自信是臺灣影視產業發展的關鍵。
Thumbnail
根據美國電影協會(MPA)主辦的「串流服務如何推動臺灣創意經濟」論壇內容,深入探討串流平臺對臺灣影視產業的影響、數據分析、政府政策建議、內容國際化策略,以及臺灣與「韓流」的差距。文章提出 awwrated 在串流生態系中的潛在角色,強調數據、策略與自信是臺灣影視產業發展的關鍵。
Thumbnail
本文探討串流平臺(VOD)如何徹底改變好萊塢和臺灣影視產業的生態。從美國電影協會(MPA)的數據報告,揭示串流服務在臺灣的驚人普及率與在地內容的消費趨勢。文章分析國際作品如何透過在地化元素開拓新市場。同時,作者也擔憂政府過度監管可能扼殺臺灣影視創新自由,以越南為鑑,呼籲以開放態度擁抱串流時代的新機遇
Thumbnail
本文探討串流平臺(VOD)如何徹底改變好萊塢和臺灣影視產業的生態。從美國電影協會(MPA)的數據報告,揭示串流服務在臺灣的驚人普及率與在地內容的消費趨勢。文章分析國際作品如何透過在地化元素開拓新市場。同時,作者也擔憂政府過度監管可能扼殺臺灣影視創新自由,以越南為鑑,呼籲以開放態度擁抱串流時代的新機遇
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-詳細解說版 排版微調是我社群的一個系列內容,這篇則是提供給訂閱會員的詳細解說版,會說明為何調整的原因跟我的看法,以及原本設計可能有的問題,如果你是設計初學者那這份內容會很適合你,因為會很細節的去講解排版原因。
Thumbnail
魔法史的課程目錄。這個系列主要是關於學術的一般評論,或是期刊歷史引用研究,或其他有助於瞭解這個領域的討論。重點研究測量理論與應用、統計與研究方法,以及教育心理學相關的主題。
Thumbnail
魔法史的課程目錄。這個系列主要是關於學術的一般評論,或是期刊歷史引用研究,或其他有助於瞭解這個領域的討論。重點研究測量理論與應用、統計與研究方法,以及教育心理學相關的主題。
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News