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
常常被朋友問「哪裡買的?」嗎?透過蝦皮分潤計畫,把日常購物的分享多加一個步驟,就能轉換成現金回饋。門檻低、申請簡單,特別適合學生與上班族,讓零碎時間也能創造小確幸。
Thumbnail
常常被朋友問「哪裡買的?」嗎?透過蝦皮分潤計畫,把日常購物的分享多加一個步驟,就能轉換成現金回饋。門檻低、申請簡單,特別適合學生與上班族,讓零碎時間也能創造小確幸。
Thumbnail
嗨!歡迎來到 vocus vocus 方格子是台灣最大的內容創作與知識變現平台,並且計畫持續拓展東南亞等等國際市場。我們致力於打造讓創作者能夠自由發表、累積影響力並獲得實質收益的創作生態圈!「創作至上」是我們的核心價值,我們致力於透過平台功能與服務,賦予創作者更多的可能。 vocus 平台匯聚了
Thumbnail
嗨!歡迎來到 vocus vocus 方格子是台灣最大的內容創作與知識變現平台,並且計畫持續拓展東南亞等等國際市場。我們致力於打造讓創作者能夠自由發表、累積影響力並獲得實質收益的創作生態圈!「創作至上」是我們的核心價值,我們致力於透過平台功能與服務,賦予創作者更多的可能。 vocus 平台匯聚了
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
魔法史的課程目錄。這個系列主要是關於學術的一般評論,或是期刊歷史引用研究,或其他有助於瞭解這個領域的討論。重點研究測量理論與應用、統計與研究方法,以及教育心理學相關的主題。
Thumbnail
關於符咒學的課程綱要。
Thumbnail
關於符咒學的課程綱要。
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News