Skip to content

Commit 1a4583b

Browse files
Julianberquist
authored andcommitted
bubblesort in v (algorithm-archivists#631)
1 parent 88a7963 commit 1a4583b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

contents/bubble_sort/bubble_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
6868
[import:2-14, lang:"emojicode"](code/emojicode/bubble_sort.emojic)
6969
{% sample lang="bash" %}
7070
[import:2-21, lang:"bash"](code/bash/bubble_sort.bash)
71+
{% sample lang="v" %}
72+
[import:1-11, lang:"v"](code/v/bubble_sort.v)
7173
{% sample lang="scratch" %}
7274
<p>
7375
<img class="center" src="code/scratch/bubble_sort.svg" width="400" />
@@ -147,6 +149,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the
147149
[import, lang:"emojicode"](code/emojicode/bubble_sort.emojic)
148150
{% sample lang="bash" %}
149151
[import, lang:"bash"](code/bash/bubble_sort.bash)
152+
{% sample lang="v" %}
153+
[import, lang:"v"](code/v/bubble_sort.v)
150154
{% sample lang="scratch" %}
151155
The code snippet was taken from this [Scratch project](https://scratch.mit.edu/projects/316483792)
152156
{% sample lang="coffeescript" %}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fn bubblesort(arr mut []int) {
2+
for i := 0; i < arr.len-1; i++ {
3+
for j := 0; j < arr.len-i-1; j++ {
4+
if arr[j] > arr[j+1] {
5+
tmp := arr[j]
6+
arr[j] = arr[j+1]
7+
arr[j+1] = tmp
8+
}
9+
}
10+
}
11+
}
12+
13+
fn main() {
14+
mut arr := [1, 45, 756, 4569, 56, 3, 8, 5, -10, -4]
15+
println('Array unsorted:')
16+
println(arr)
17+
bubblesort(mut arr)
18+
println('Array sorted:')
19+
println(arr)
20+
}

0 commit comments

Comments
 (0)