Skip to content

Commit

Permalink
Merge pull request #8 from yuehhua/master
Browse files Browse the repository at this point in the history
Update bubble sort
  • Loading branch information
yuehhua authored Apr 13, 2020
2 parents 7174905 + 5ec4f67 commit 8104117
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
24 changes: 23 additions & 1 deletion BubbleSort/README.md
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]
```
7 changes: 5 additions & 2 deletions BubbleSort/src/BubbleSort.jl
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
10 changes: 6 additions & 4 deletions BubbleSort/test/runtests.jl
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

0 comments on commit 8104117

Please sign in to comment.