-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from yuehhua/master
Update bubble sort
- Loading branch information
Showing
3 changed files
with
34 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
# 氣泡排序法 | ||
|
||
請撰寫氣泡排序演算法。 | ||
[氣泡排序演算法](https://www.wikiwand.com/zh-tw/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F)氣泡排序演算法是非常基礎的排序演算法。排序演算法會接受一個陣列,當中包含任意排序的數字,輸出需要是由小到大排序好的陣列。氣泡排序演算法是兩兩比較,大小順序不同者交換,直到最後。這時候最後的元素就會是最大的,除了最後一個元素,再進一步考慮剩下的元素進行比較及交換,直到所有元素的順序是正確的為止。 | ||
|
||
請撰寫氣泡排序演算法,並且依照由小到大的順序排序。請將功能實作成 `bubble_sort!` 函式並接受一個陣列,結束時回傳排序好的陣列。 | ||
|
||
## 測試 | ||
|
||
```julia | ||
a = [3, 4, 7, 5, 8, 9, 6, 1, 2] | ||
bubble_sort!(a) | ||
|
||
@test a .== [1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
``` | ||
|
||
### 測試資料 | ||
|
||
```julia | ||
[3, 4, 7, 5, 8, 9, 6, 1, 2] | ||
[3.4, 5.2, 4.3, 1.0, 0.0] | ||
[3.4, 5.2, 4.3, 1.0, 0.0, -2.3, -10.3] | ||
[π, 3.4, 4.3, -1.0, -10.3] | ||
[missing, 3.12, -6.66, 7.5] | ||
[1//3, 1//6, -1//3, 1//0] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
module BubbleSort | ||
|
||
export bubble_sort! | ||
|
||
bubble_sort!(x::Vector) = [] | ||
src = [] | ||
|
||
for s = src | ||
include("$(s).jl") | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
using BubbleSort | ||
using Test | ||
|
||
a = [3, 4, 7, 5, 8, 9, 6, 1, 2] | ||
bubble_sort!(a) | ||
|
||
@test a .== [1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
tests = [] | ||
|
||
@testset "BubbleSort" begin | ||
for t in tests | ||
include("$(t).jl") | ||
end | ||
end |