Skip to content

Commit 1618007

Browse files
authored
Merge branch 'master' into jarvis_march_cpp
2 parents 6a637b0 + 43437da commit 1618007

File tree

69 files changed

+3191
-419
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3191
-419
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Contributing
2+
3+
A contribution guide on how to contribute to the Arcane Algorithm Archive (AAA) can be found on this Wiki page:
4+
https://github.com/algorithm-archivists/algorithm-archive/wiki/How-to-Contribute
5+
6+
The community member Buttercak3 also created a video series, explaining the contribution process of the AAA.
7+
You can find a playlist with all videos here:
8+
https://www.youtube.com/playlist?list=PL5NSPcN6fRq2vwgdb9noJacF945CeBk8x

CONTRIBUTORS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
This file lists everyone, who contributed to this repo and wanted to show up here. If you're looking for information on contribution, please check the `CONTRIBUTING.md` out.
2+
3+
# Contributors
4+
15
James Schloss
26
<br>
37
Nicole Mazzuca
@@ -46,3 +50,8 @@ Gibus Wearing Brony
4650
<br>
4751
Gorzoid
4852
<br>
53+
Arun Sahadeo
54+
<br>
55+
NIFR91
56+
<br>
57+
Michal Hanajik

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and live coded on Twitch: https://www.twitch.tv/simuleios.
1515
If you would like to communicate more directly, please feel free to go to our discord: https://discord.gg/Pr2E9S6.
1616

1717

18-
Note that the this project is is essentially a book about algorithms collaboratively written by an online community.
18+
Note that the this project is essentially a book about algorithms collaboratively written by an online community.
1919
Fortunately, there are a lot of algorithms out there, which means that there is a lot of content material available.
2020
Unfortunately, this means that we will probably never cover every algorithm ever created and instead need to focus on what the community sees as useful and necessary.
2121
That said, we'll still cover a few algorithms for fun that have very little, if any practical purpose.

book.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
"name": "Julia",
3434
"default": true
3535
},
36-
{
37-
"lang": "pseudo",
38-
"name": "Pseudocode"
39-
},
4036
{
4137
"lang": "cs",
4238
"name": "C#"
@@ -116,6 +112,18 @@
116112
{
117113
"lang": "ti83b",
118114
"name": "TI-83 Basic"
115+
},
116+
{
117+
"lang": "lua",
118+
"name": "Lua"
119+
},
120+
{
121+
"lang": "crystal",
122+
"name": "Crystal"
123+
},
124+
{
125+
"lang": "php",
126+
"name": "PHP"
119127
}
120128
]
121129
}

contents/bogo_sort/bogo_sort.md

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,55 @@ In code, it looks something like this:
1414

1515
{% method %}
1616
{% sample lang="jl" %}
17-
[import:1-14, lang:"julia"](code/julia/bogo.jl)
17+
[import:10-14, lang:"julia"](code/julia/bogo.jl)
1818
{% sample lang="cs" %}
1919
[import:9-15, lang:"csharp"](code/csharp/BogoSort.cs)
2020
{% sample lang="clj" %}
21-
[import:2-11, lang:"clojure"](code/clojure/bogo.clj)
21+
[import:7-11, lang:"clojure"](code/clojure/bogo.clj)
2222
{% sample lang="c" %}
23-
[import:4-29, lang:"c_cpp"](code/c/bogo_sort.c)
23+
[import:25-29, lang:"c_cpp"](code/c/bogo_sort.c)
2424
{% sample lang="java" %}
25-
[import:2-17, lang:"java"](code/java/bogo.java)
25+
[import:2-6, lang:"java"](code/java/bogo.java)
2626
{% sample lang="js" %}
27-
[import:1-16, lang:"javascript"](code/javascript/bogo.js)
27+
[import:11-15, lang:"javascript"](code/javascript/bogo.js)
2828
{% sample lang="py" %}
29-
[import:4-12, lang:"python"](code/python/bogo.py)
29+
[import:10-12, lang:"python"](code/python/bogo.py)
30+
{% sample lang="hs" %}
31+
[import:17-20, lang:"haskell"](code/haskell/bogoSort.hs)
32+
{% sample lang="m" %}
33+
[import:21-28, lang:"matlab"](code/matlab/bogosort.m)
34+
{% sample lang="cpp" %}
35+
[import:33-38, lang:"c_cpp"](code/c++/bogosort.cpp)
36+
{% sample lang="rs" %}
37+
[import:16-20, lang:"rust"](code/rust/bogosort.rs)
38+
{% sample lang="swift" %}
39+
[import:25-31, lang:"swift"](code/swift/bogosort.swift)
40+
{% endmethod %}
41+
42+
That's it.
43+
Ship it!
44+
We are done here!
45+
46+
## Example Code
47+
48+
{% method %}
49+
{% sample lang="jl" %}
50+
[import, lang:"julia"](code/julia/bogo.jl)
51+
{% sample lang="cs" %}
52+
BogoSort.cs
53+
[import, lang:"csharp"](code/csharp/BogoSort.cs)
54+
Program.cs
55+
[import, lang:"csharp"](code/csharp/Program.cs)
56+
{% sample lang="clj" %}
57+
[import, lang:"clojure"](code/clojure/bogo.clj)
58+
{% sample lang="c" %}
59+
[import, lang:"c_cpp"](code/c/bogo_sort.c)
60+
{% sample lang="java" %}
61+
[import, lang:"java"](code/java/bogo.java)
62+
{% sample lang="js" %}
63+
[import, lang:"javascript"](code/javascript/bogo.js)
64+
{% sample lang="py" %}
65+
[import, lang:"python"](code/python/bogo.py)
3066
{% sample lang="hs" %}
3167
[import, lang:"haskell"](code/haskell/bogoSort.hs)
3268
{% sample lang="m" %}
@@ -39,10 +75,6 @@ In code, it looks something like this:
3975
[import, lang:"swift"](code/swift/bogosort.swift)
4076
{% endmethod %}
4177

42-
That's it.
43-
Ship it!
44-
We are done here!
45-
4678

4779
<script>
4880
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);

contents/bogo_sort/code/java/bogo.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
public class Bogo {
2-
// The shuffle() function can be found in code/java/bogo.java
32
static void bogoSort(int[] arr) {
43
while(!isSorted(arr)) {
54
shuffle(arr);
@@ -26,7 +25,6 @@ static void shuffle(int[] arr) {
2625
}
2726

2827

29-
// main function (for testing)
3028
public static void main(String[] args) {
3129
int[] test = new int[]{20, -3, 50, 1, -6, 59};
3230

contents/bogo_sort/code/javascript/bogo.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ function isSorted(arr) {
88
return true;
99
}
1010

11-
// The shuffle() function can be found in code/javascript/bogo.js
1211
function bogoSort(arr) {
1312
while (!isSorted(arr)) {
1413
shuffle(arr);

contents/bogo_sort/code/swift/bogosort.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
11
import Foundation
22

3-
43
func isSorted(inputArray: [Int]) -> Bool {
5-
64
for i in 0..<inputArray.count-1 {
75
if inputArray[i] > inputArray[i+1] {
86
return false
97
}
108
}
11-
9+
1210
return true
1311
}
1412

15-
16-
1713
func shuffle(inputArray: inout [Int]) -> [Int] {
18-
1914
var shuffledArray = [Int]()
2015

2116
for _ in 0..<inputArray.count {
2217
let rand = Int(arc4random_uniform(UInt32(inputArray.count)))
2318
shuffledArray.append(inputArray[rand])
2419
inputArray.remove(at: rand)
2520
}
26-
21+
2722
return shuffledArray
2823
}
2924

30-
31-
3225
func bogoSort(sortArray: inout [Int]) -> [Int] {
33-
3426
while(!isSorted(inputArray: sortArray)) {
3527
sortArray = shuffle(inputArray: &sortArray)
3628
}
37-
29+
3830
return sortArray
3931
}
4032

contents/bubble_sort/bubble_sort.md

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
1313
{% sample lang="cs" %}
1414
[import:9-27, lang:"csharp"](code/csharp/BubbleSort.cs)
1515
{% sample lang="c" %}
16-
[import:11-21, lang:"c_cpp"](code/c/bubble_sort.c)
16+
[import:10-20, lang:"c_cpp"](code/c/bubble_sort.c)
1717
{% sample lang="java" %}
1818
[import:2-12, lang:"java"](code/java/bubble.java)
1919
{% sample lang="js" %}
@@ -25,26 +25,75 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
2525
{% sample lang="hs" %}
2626
[import, lang:"haskell"](code/haskell/bubbleSort.hs)
2727
{% sample lang="cpp" %}
28-
[import, lang:"c_cpp"](code/c++/bubblesort.cpp)
28+
[import:13-23, lang:"c_cpp"](code/c++/bubblesort.cpp)
2929
{% sample lang="rs" %}
30-
[import:6-19, lang:"rust"](code/rust/bubble_sort.rs)
30+
[import:6-16, lang:"rust"](code/rust/bubble_sort.rs)
3131
{% sample lang="d" %}
3232
[import:3-18, lang:"d"](code/d/bubble_sort.d)
3333
{% sample lang="go" %}
3434
[import:7-21, lang:"golang"](code/go/bubbleSort.go)
3535
{% sample lang="racket" %}
36-
[import:5-19, lang:"racket"](code/racket/bubbleSort.rkt)
36+
[import:6-19, lang:"racket"](code/racket/bubbleSort.rkt)
3737
{% sample lang="swift" %}
38-
[import, lang:"swift"](code/swift/bubblesort.swift)
38+
[import:1-13, lang:"swift"](code/swift/bubblesort.swift)
3939
{% sample lang="ti83b" %}
4040
[import:2-13, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt)
41+
{% sample lang="ruby" %}
42+
[import:3-13, lang:"ruby"](code/ruby/bubble.rb)
43+
{% sample lang="crystal" %}
44+
[import:1-11, lang:"crystal"](code/crystal/bubble.cr)
45+
{% sample lang="php" %}
46+
[import:3-15, lang:"php"](code/php/bubble_sort.php)
4147
{% endmethod %}
4248

4349
... And that's it for the simplest bubble sort method.
4450
Now, as you might imagine, computer scientists have optimized this to the fiery lakes of Michigan and back, so we'll come back to this in the future and talk about how to optimize it.
4551
For now, it's fine to just bask in the simplicity that is bubble sort.
4652
Trust me, there are plenty of more complicated algorithms that do precisely the same thing, only much, much better (for most cases).
4753

54+
## Example Code
55+
56+
{% method %}
57+
{% sample lang="jl" %}
58+
[import, lang:"julia"](code/julia/bubble.jl)
59+
{% sample lang="cs" %}
60+
BubbleSort.cs
61+
[import, lang:"csharp"](code/csharp/BubbleSort.cs)
62+
Program.cs
63+
[import, lang:"csharp"](code/csharp/Program.cs)
64+
{% sample lang="c" %}
65+
[import, lang:"c_cpp"](code/c/bubble_sort.c)
66+
{% sample lang="java" %}
67+
[import, lang:"java"](code/java/bubble.java)
68+
{% sample lang="js" %}
69+
[import, lang:"javascript"](code/javascript/bubble.js)
70+
{% sample lang="py" %}
71+
[import, lang:"python"](code/python/bubblesort.py)
72+
{% sample lang="m" %}
73+
[import, lang:"matlab"](code/matlab/bubblesort.m)
74+
{% sample lang="hs" %}
75+
[import, lang:"haskell"](code/haskell/bubbleSort.hs)
76+
{% sample lang="cpp" %}
77+
[import, lang:"c_cpp"](code/c++/bubblesort.cpp)
78+
{% sample lang="rs" %}
79+
[import, lang:"rust"](code/rust/bubble_sort.rs)
80+
{% sample lang="d" %}
81+
[import, lang:"d"](code/d/bubble_sort.d)
82+
{% sample lang="go" %}
83+
[import, lang:"golang"](code/go/bubbleSort.go)
84+
{% sample lang="racket" %}
85+
[import, lang:"racket"](code/racket/bubbleSort.rkt)
86+
{% sample lang="swift" %}
87+
[import, lang:"swift"](code/swift/bubblesort.swift)
88+
{% sample lang="ti83b" %}
89+
[import, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt)
90+
{% sample lang="ruby" %}
91+
[import, lang:ruby"](code/ruby/bubble.rb)
92+
{% sample lang="crystal" %}
93+
[import, lang:"crystal"](code/crystal/bubble.cr)
94+
{% sample lang="php" %}
95+
[import, lang:"php"](code/php/bubble_sort.php)
96+
{% endmethod %}
4897

4998
<script>
5099
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);

contents/bubble_sort/code/c++/bubblesort.cpp

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,34 @@
22
#include <cstddef>
33
#include <iostream>
44
#include <iterator>
5-
#include <random>
6-
7-
using std::begin;
8-
using std::end;
9-
10-
template <class Rng>
11-
std::vector<float> generate_input(std::size_t size, Rng& rng) {
12-
auto dist = std::uniform_real_distribution<>(0.0, 1.0);
13-
14-
auto ret = std::vector<float>();
15-
std::generate_n(std::back_inserter(ret), size,
16-
[&rng, &dist] { return dist(rng); });
17-
18-
return ret;
19-
}
205

216
template <class Iter>
22-
void print_range(std::ostream& os, Iter const first, Iter const last) {
23-
os << '{';
24-
25-
if (first != last) {
26-
os << *first;
27-
std::for_each(first + 1, last, [&os] (double d) { os << ", " << d; });
28-
}
29-
30-
os << "}\n";
7+
void print_range(Iter first, Iter last) {
8+
for (auto it = first; it != last; ++it)
9+
std::cout << *it << " ";
10+
std::cout << std::endl;
3111
}
3212

3313
template <class Iter>
34-
void bubble_sort(Iter const first, Iter const last) {
35-
if (first == last) {
36-
// the empty range is vacuously sorted
37-
return;
38-
}
39-
40-
for (;;) {
41-
bool is_sorted = true;
42-
43-
for (auto it = first; it + 1 != last; ++it) {
14+
void bubble_sort(Iter first, Iter last) {
15+
for (auto it1 = first; it1 != last; ++it1) {
16+
for (auto it2 = first; it2 + 1 != last; ++it2) {
4417
// these are unsorted! gotta swap 'em
45-
if (*(it + 1) < *it) {
46-
using std::swap;
47-
swap(*it, *(it + 1));
48-
is_sorted = false;
18+
if (*(it2 + 1) < *it2) {
19+
std::iter_swap(it2, it2 + 1);
4920
}
5021
}
51-
52-
if (is_sorted) {
53-
break;
54-
}
5522
}
5623
}
5724

5825
int main() {
59-
std::random_device random_device;
60-
auto rng = std::mt19937(random_device());
61-
62-
auto input = generate_input(10, rng);
26+
int input[] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
6327

64-
std::cout << "before sorting:\n";
65-
print_range(std::cout, begin(input), end(input));
28+
std::cout << "Unsorted array:\n";
29+
print_range(std::begin(input), std::end(input));
6630

67-
bubble_sort(begin(input), end(input));
31+
bubble_sort(std::begin(input), std::end(input));
6832

69-
std::cout << "\nafter sorting:\n";
70-
print_range(std::cout, begin(input), end(input));
33+
std::cout << "\nSorted array:\n";
34+
print_range(std::begin(input), std::end(input));
7135
}

0 commit comments

Comments
 (0)