Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions chapters/sorting_searching/bogo/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ In code, it looks something like this:
[import:9-15, lang:"csharp"](code/cs/BogoSort.cs)
{% sample lang="clj" %}
[import:6-10, lang:"clojure"](code/clojure/bogo.clj)
{% sample lang="js" %}
[import:1-15, lang:"javascript"](code/js/bogo.js)
{% endmethod %}

That's it.
Expand Down
24 changes: 24 additions & 0 deletions chapters/sorting_searching/bogo/code/js/bogo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function isSorted(arr) {
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
return false;
}
}

return true;
}

function bogoSort(arr) {
while (!isSorted(arr)) {
shuffle(arr);
}
}

function shuffle(arr) {
for (let r = arr.length -1; r > 0; r--) {
let i = Math.floor(Math.random() * r);
let tmp = arr[i];
arr[i] = arr[r];
arr[r] = tmp;
}
}
2 changes: 2 additions & 0 deletions chapters/sorting_searching/bubble/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:1-10, lang:"julia"](code/julia/bubble.jl)
{% sample lang="cs" %}
[import:9-27, lang:"csharp"](code/cs/BubbleSort.cs)
{% sample lang="js" %}
[import:1-11, lang:"javascript"](code/js/bubble.js)
{% endmethod %}

... And that's it for the simplest bubble sort method.
Expand Down
11 changes: 11 additions & 0 deletions chapters/sorting_searching/bubble/code/js/bubble.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function bubbleSort(arr) {
for (let r = arr.length -1; r > 0; r--) {
for (let i = 0; i < r; i++) {
if (arr[i] > arr[i + 1]) {
let tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
}
}